Skip to content

Instantly share code, notes, and snippets.

@tosh
Created July 22, 2026 13:19
Show Gist options
  • Select an option

  • Save tosh/6e91a9dbf08dd630c535e7345ac7f0b5 to your computer and use it in GitHub Desktop.

Select an option

Save tosh/6e91a9dbf08dd630c535e7345ac7f0b5 to your computer and use it in GitHub Desktop.
agent in 9 lines python
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]
@gabrielsroka

Copy link
Copy Markdown

@gabrielsroka

gabrielsroka commented Jul 22, 2026

Copy link
Copy Markdown

Claude and I wrote this version but we didn't test it. It uses http.client (std lib) which keeps the connection open, so less overhead.

import json,sys,http.client,urllib.parse;from subprocess import getoutput as sh
url=urllib.parse.urlsplit(sys.argv[1]);c=http.client.HTTPSConnection(url.hostname);h=[];b={'model':'gpt-5.6','input':h,'tools':[{'type':'custom','name':'sh'}]}
while p:=input('> '):
  h.append({'role':'user','content':p})
  while True:
    c.request('POST',url.path,json.dumps(b).encode(),{'Content-Type':'application/json'})
    o=(r:=json.load(c.getresponse()))['output'];h.extend(o);t=[i for i in o if i['type']=='custom_tool_call']
    if not t:print(o[-1]['content'][0]['text'],f'\n[{r['usage']['total_tokens']/10500:06.3f}%]');break
    h.extend({'type':'custom_tool_call_output','call_id':i['call_id'],'output':sh(i['input'])} for i in t)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment