Building an LLM agent doesn't actually require a massive

Riley82 Advanced 10h ago 92 views 12 likes 1 min read

I've been looking at a minimal implementation that strips everything away—no MCP, no plugins, and no heavy libraries. It's essentially a "code-golfed" version of an agent that handles the loop of: User Input → Model Response → Tool Execution → Model Response.

The core logic here is that the environment (Docker, a sandbox, etc.) should be your security boundary, not the code harness itself. By avoiding a system prompt, you also save precious context window tokens, which modern high-reasoning models handle just fine without.

Here is the complete implementation for a lightweight AI workflow:

import json,sys;from subprocess import getoutput as sh;from urllib.request import Request as R,urlopen
url=sys.argv[1];h=[];b=dict(model="gpt-5.6",input=h,tools=[dict(type="custom",name="sh")])
while p:=input("> "):
  h+=[dict(role="user",content=p)];H={"Content-Type":"application/json"}
  while True:
    o=(r:=json.load(urlopen(R(url,json.dumps(b).encode(),H))))["output"]
    h+=o;c=[i for i in o if i["type"]=="custom_tool_call"];z=r["usage"]["total_tokens"]/10500
    if not c:print(o[-1]["content"][0]["text"],f'\n[{z:06.3f}%]');break
    h+=[dict(type="custom_tool_call_output",call_id=i["call_id"],output=sh(i["input"])) for i in c]

Why this works as a practical tutorial for agents

If you want to deploy this from scratch, here is the breakdown of the logic:

1. Zero Dependencies: It uses urllib and subprocess from the stdlib. This means zero supply chain risk and near-instant startup time.
2. The Tool Loop: The while True loop is the "brain." It checks if the model's output contains a custom_tool_call. If it does, it executes the shell command (sh) and feeds the result back into the history (h).
3. Open-Ended Control: By giving the agent access to sh, you've essentially given it the keys to the environment. It can read files, list directories, or run scripts.
4. Context Tracking: The z variable calculates the percentage of the context window used, which is critical for long-running sessions.

To run this, you just need an OpenAI-compatible inference endpoint. You can pass the URL as a command-line argument: python agent.py http://your-api-endpoint.

This is a great starting point for anyone wanting a deep dive into how LLM agents actually function without the abstraction layers of LangChain or AutoGPT.

Prompt

All Replies (4)

D
Drew36 Advanced 10h ago
Nice. Do you have an ungolfed version? I was thinking about asking my LLM to write one if not.
0 Reply
P
PatFounder Advanced 10h ago
It's impressive how they kept it so compact without sacrificing readability. But wouldn't the LLM need a tool description passed along to actually know how to use it properly?
0 Reply
L
Leo37 Novice 10h ago
Yeah, but if you use a system prompt with a clear schema, it usually figures it out fine.
0 Reply
C
Casey51 Novice 10h ago
Just make sure you handle the timeout logic so it doesn't loop forever.
0 Reply

Write a Reply

Markdown supported