cd /news/artificial-intelligence/agent-in-9-lines-python · home topics artificial-intelligence article
[ARTICLE · art-69071] src=gist.github.com ↗ pub= topic=artificial-intelligence verified=true sentiment=· neutral

agent in 9 lines python

An engineer created a minimal AI agent in just 9 lines of Python code, using a custom tool to execute shell commands and a loop that processes user input through a language model. The agent, which calls a remote model endpoint, demonstrates a compact implementation of tool-use and conversation management.

read1 min views1 publishedJul 22, 2026

| 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] |
── more in #artificial-intelligence 4 stories · sorted by recency
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/agent-in-9-lines-pyt…] indexed:0 read:1min 2026-07-22 ·