Agent in 9 Lines Python
Details
- External ID
- 49006862
- Source
- HN
- Company
- —
- Product
- Agent in 9 Lines Python
- Website domain
- github.com
- Launched
- July 22, 2026
- Cohort
- —
- Upvotes
- 21
- Upvotes percentile
- 0.7264038231780168
- Tags
- —
- Fetched at
- Sept. 7, 2026, 9:26 p.m.
- Updated at
- Sept. 7, 2026, 9:26 p.m.
Description
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 usedNoteworthy:no dependencies other than python stdlib (!)- less startup time- less dependency churn- less supply chain attack vector surface- less code to verify and understandno mcp, no plugins, no security theater- if you want to add something specific: add it explicitly- adapt the environment to give the agent access or restrict access to tools, resources, network etc (the env is the security boundary, not the harness)no system prompt- every token in context window is precious- current strong models do fine without steering via system prompt (or are even harmed by long overly specific system prompts designed for models from months ago)- system prompt or agents.md context can easily be added if needed (agent can also discover it or get prompted to read from environment as is)how to run/deploy the agent- design the environment you want to give the agent (container, docker, sandbox of your choice)- start an inference api endpoint that is openai-like (support the request/response shape used in agent.py above)- inference api endpoint can be as simple as a proxy to openai api that adds credentials/api key- adapt as you want/need it, change the model, remove/alter context window behaviour, add tools, etc etcLooking for any feedback you have to make it more clear or even simpler!
Enrichment
- Theme
- developer tools for AI agents
- Vertical
- Horizontal
- Function
- Agent / copilot
- Audience
- Developer
- AI stance
- AI-native
- Project type
- Hobby / open-source project
- Normalized one-liner
- minimal python implementation of an ai agent
- Manually corrected
- False
Could you build this?
Yes A minimal 9-line agent loop invoking an LLM provider's tool-calling endpoint can be written directly in minutes.
Discussion
9 comments analyzed.
Concerns raised: Tool description impact on agent behavior and performance, Code readability vs. compactness tradeoff
Feature requests: Ungolfed/expanded version for clarity, Move headers outside loop, Move context_usage inside conditional, Hard-code static values
Competitors
Other products that read as similar to this one — 129 launches clear the similarity bar, closest 8 shown.
Attention rank: #38 of 130 (itself plus its competitors, highest first — normalized so YC and Product Hunt are compared fairly).
Launched 263 days after the earliest competitor.
- OpenClaw-class agents on ESP32 (and the IDE that makes it possible) · hn · 2026-03-12 · 31 upvotes · similarity 0.43
- AgentGraph Studio · ph · 2026-09-18 · 1 upvotes · similarity 0.42
- MockAgent · ph · 2026-09-30 · 1 upvotes · similarity 0.41
- Pi Coding Agent · ph · 2026-05-25 · 177 upvotes · similarity 0.41
- Desktop app to run Python agents over TCP with live server geolocation · hn · 2026-03-06 · 5 upvotes · similarity 0.40
- AgentRun: DSL to turn agents into workflows · hn · 2026-09-23 · 51 upvotes · similarity 0.40
- Calling tools w/ Python improves LLM perf. vs MCP (77.1% on BrowseComp) · hn · 2025-12-16 · 10 upvotes · similarity 0.40
- Azdaja · ph · 2026-09-23 · 9 upvotes · similarity 0.39
Other launches for this product
- No other launches for this product.
Same idea, different domain
Nobody's really built a agent / copilot tool for Agriculture yet.