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 | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.