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

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)

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