cd /news/large-language-models/agent-expanded-py · home topics large-language-models article
[ARTICLE · art-69316] src=gist.github.com ↗ pub= topic=large-language-models verified=true sentiment=· neutral

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.

read1 min views1 publishedJul 22, 2026

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

── more in #large-language-models 4 stories · sorted by recency
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/agent-expanded-py] indexed:0 read:1min 2026-07-22 ·