agent_expanded.py An engineer shared a Python script, agent_expanded.py, that implements a simple agent loop for a large language model with a 1,050,000-token context window. The script uses a custom shell tool to execute commands and iterates until no tool calls remain, printing the final response and context usage percentage. | 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 | | | |