Skip to content

Instantly share code, notes, and snippets.

@tosh
Created July 22, 2026 17:16
Show Gist options
  • Select an option

  • Save tosh/61aca9ffa9ea115fa4df332407d7a9a9 to your computer and use it in GitHub Desktop.

Select an option

Save tosh/61aca9ffa9ea115fa4df332407d7a9a9 to your computer and use it in GitHub Desktop.
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
]
@gabrielsroka

gabrielsroka commented Jul 22, 2026

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
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)

@swyxio

swyxio commented Aug 1, 2026

Copy link
Copy Markdown

also make sure to reuse thread id's for repsonses api

@99991

99991 commented Aug 1, 2026

Copy link
Copy Markdown

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.

@tosh

tosh commented Aug 2, 2026

Copy link
Copy Markdown
Author

@swyxio ty re thread id!

@99991 sure, you can consider the code apache 2.0, take what you need

interesting @ big-pickle

@99991

99991 commented Aug 2, 2026

Copy link
Copy Markdown

Nice, thanks!

@tosh

tosh commented Aug 5, 2026

Copy link
Copy Markdown
Author

a go version of the minimal agent (and continued development as "smol") lives here now:

https://github.com/smol-env/smol

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