{"slug": "agent-in-9-lines-python", "title": "agent in 9 lines python", "summary": "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.", "body_md": "| import json,sys;from subprocess import getoutput as sh;from urllib.request import Request as R,urlopen | |\n| url=sys.argv[1];h=[];b=dict(model=\"gpt-5.6\",input=h,tools=[dict(type=\"custom\",name=\"sh\")]) | |\n| while p:=input(\"> \"): | |\n| h+=[dict(role=\"user\",content=p)];H={\"Content-Type\":\"application/json\"} | |\n| while True: | |\n| o=(r:=json.load(urlopen(R(url,json.dumps(b).encode(),H))))[\"output\"] | |\n| h+=o;c=[i for i in o if i[\"type\"]==\"custom_tool_call\"];z=r[\"usage\"][\"total_tokens\"]/10500 | |\n| if not c:print(o[-1][\"content\"][0][\"text\"],f'\\n[{z:06.3f}%]');break | |\n| h+=[dict(type=\"custom_tool_call_output\",call_id=i[\"call_id\"],output=sh(i[\"input\"])) for i in c] |", "url": "https://wpnews.pro/news/agent-in-9-lines-python", "canonical_source": "https://gist.github.com/tosh/6e91a9dbf08dd630c535e7345ac7f0b5", "published_at": "2026-07-22 13:19:55+00:00", "updated_at": "2026-07-22 18:57:22.948065+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-agents", "developer-tools"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/agent-in-9-lines-python", "markdown": "https://wpnews.pro/news/agent-in-9-lines-python.md", "text": "https://wpnews.pro/news/agent-in-9-lines-python.txt", "jsonld": "https://wpnews.pro/news/agent-in-9-lines-python.jsonld"}}