今日已更新 35 条资讯 | 累计 23992 条内容
关于我们

Show HN: Agent in 9 Lines Python

tosh 2026年07月22日 21:52 1 次阅读 来源:HackerNews

I asked myself: what would a minimal implementation of an agent look like? Something that works out of the box, is a real agent with tool calling, but without 1000s of lines of code, without dozens or hundreds of npm or pypi dependencies. Something with just a few 'essential' features (not a whole kitchen sink that most agent harnesses come with nowadays). An implementation close to pseudocode that you can look at in one page, everything there at a glance, no scrolling. This is the agent.py I en

I asked myself: what would a minimal implementation of an agent look like? Something that works out of the box, is a real agent with tool calling, but without 1000s of lines of code, without dozens or hundreds of npm or pypi dependencies. Something with just a few 'essential' features (not a whole kitchen sink that most agent harnesses come with nowadays). An implementation close to pseudocode that you can look at in one page, everything there at a glance, no scrolling. This is the agent.py I ended up with so far: import json,sys;from subprocess import getoutput as sh;from urllib.request import Request as R,urlopen url=sys.argv[1];h=[];b=dict(model="gpt-5.6",input=h,tools=[dict(type="custom",name="sh")]) while p:=input("> "): h+=[dict(role="user",content=p)];H={"Content-Type":"application/json"} while True: o=(r:=json.load(urlopen(R(url,json.dumps(b).encode(),H))))["output"] h+=o;c=[i for i in o if i["type"]=="custom_tool_call"];z=r["usage"]["total_tokens"]/10500 if not c:print(o[-1]["content"][0]["text"],f'\n[{z:06.3f}%]');break h+=[dict(type="custom_tool_call_output",call_id=i["call_id"],output=sh(i["input"])) for i in c] It is a bit code golfed but I think it is fairly readable - imports are all from stdlib (0 external dependencies!) - assumes there is an inference api endpoint running somewhere - assumes the inference api endpoint is openai-like - model hardcoded to "gpt 5.6" (=> Sol), can easily be changed to e.g. open weight (kimi k3, glm 5.2 etc) - api endpoint url is passed as arg to the python script - configures only 1 custom tool: 'sh' - 'sh' is sufficient for interacting with the environment in an open ended way - new api output gets added to history ("h") - if api output contains tool calls the tool calls get executed - agent gives control back to user when the last model response is without tool calls - agent message to user shows % of context window used Noteworthy: no dependencies other than python stdlib (!) - less startup time - less dependency chu
本文内容来源于互联网,版权归原作者所有
查看原文