Created
July 22, 2026 17:16
-
-
Save tosh/61aca9ffa9ea115fa4df332407d7a9a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import sys | |
| from subprocess import getoutput as run_shell | |
| from urllib.request import Request, urlopen | |
| MODEL = "gpt-5.6" | |
| CONTEXT_WINDOW_TOKENS = 1_050_000 | |
| endpoint_url = sys.argv[1] | |
| history = [] | |
| request_body = dict( | |
| model=MODEL, | |
| input=history, | |
| tools=[dict(type="custom", name="sh")], | |
| ) | |
| while user_prompt := input("> "): | |
| history += [dict(role="user", content=user_prompt)] | |
| headers = {"Content-Type": "application/json"} | |
| while True: | |
| output_items = ( | |
| response := json.load( | |
| urlopen( | |
| Request( | |
| endpoint_url, | |
| json.dumps(request_body).encode(), | |
| headers, | |
| ) | |
| ) | |
| ) | |
| )["output"] | |
| history += output_items | |
| tool_calls = [ | |
| item | |
| for item in output_items | |
| if item["type"] == "custom_tool_call" | |
| ] | |
| context_usage_percent = ( | |
| response["usage"]["total_tokens"] / CONTEXT_WINDOW_TOKENS * 100 | |
| ) | |
| if not tool_calls: | |
| print( | |
| output_items[-1]["content"][0]["text"], | |
| f"\n[{context_usage_percent:06.3f}%]", | |
| ) | |
| break | |
| history += [ | |
| dict( | |
| type="custom_tool_call_output", | |
| call_id=tool_call["call_id"], | |
| output=run_shell(tool_call["input"]), | |
| ) | |
| for tool_call in tool_calls | |
| ] |
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
import sys
from subprocess import getoutput as run_shell
import http.client
from urllib.parse import urlsplit
url = urlsplit(sys.argv[1])
conn = http.client.HTTPSConnection(url.hostname)
history = []
body = {'model': 'gpt-5.6', 'input': history, 'tools': [{'type': 'custom', 'name': 'sh'}]}
while prompt := input('> '):
history.append({'role': 'user', 'content': prompt})
while True:
conn.request('POST', url.path, json.dumps(body).encode(), {'Content-Type': 'application/json'})
result = json.load(conn.getresponse())
output = result['output']
history.extend(output)
tool_calls = [item for item in output if item['type'] == 'custom_tool_call']
if not tool_calls:
usage_pct = result['usage']['total_tokens'] / 10500
print(output[-1]['content'][0]['text'], f'\n[{usage_pct:06.3f}%]')
break
history.extend({'type': 'custom_tool_call_output', 'call_id': item['call_id'], 'output': run_shell(item['input'])} for item in tool_calls)also make sure to reuse thread id's for repsonses api
Wow, this is using a lot of clever tricks. I might steal some of them for my surprisingly similar small agent if you don't mind.
I adapted OpenCode's big-pickle model so it runs without having to configure anything. Maybe that could work for you as well.
Author
Nice, thanks!
Author
a go version of the minimal agent (and continued development as "smol") lives here now:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see https://news.ycombinator.com/item?id=49006862 and https://gist.github.com/tosh/6e91a9dbf08dd630c535e7345ac7f0b5