How Claude Code Works (Let’s Build a Tiny One)
From the publisher’s feed, collected in the ZeroLabs newsroom.
Tools, permission rules, memory and context limits in one Python file that runs on free models. Once you see it, AI coding agents stop being magic.Cover image generated by the author with ChatGPT.When you first watch a coding agent like Claude Code work, it feels like magic. You type “add a test for this function”, and it reads your files, writes code, runs the tests, notices a failure, fixes it and tells you it’s done.It isn’t magic. Underneath, it’s a surprisingly small idea, wrapped in a lot of careful engineering. In this post I’ll start from absolute zero (what a language model even is) and end with a working coding agent you can run on your own machine: one Python file, no libraries to install, working with Groq, OpenRouter, NVIDIA, or a model running locally with Ollama.If you remember one sentence from this post, make it this one:An agent is a while-loop around one API call. Everything else is guardrails and memory.Let’s earn that sentence.1. What a language model really isA very well-read autocompleteA large language model (LLM), like Claude, Gemini or GPT, does exactly one thing: given some text, it predicts what comes next. It does this one small piece at a time, very fast, after learning from an enormous amount of writing.Figure 1. A language model predicts the next piece of text, adds it, and repeats. Image by the author.Chat, code, reasoning, agents: all of it is built on that one ability.TokensModels don’t read letters or words. They read tokens: chunks of text, about ¾ of an English word on average. hello is one token; a long word might be two or three; 1,000 words of English come to roughly 1,300 tokens. A handy rule of thumb is about 4 characters per token, which is exactly what our agent will use later to measure its conversation.Tokens matter because everything is measured in them: how much the model can read at once, how fast it answers, and what you pay.The context window is a deskThe context window is how much text the model can look at in one go. Think of it as a desk. Everything the model needs for its next answer has to fit on that desk: its instructions, the conversation, the files it read, the output of the commands it ran.Figure 2. Everything the model uses for its next reply has to fit in the context window. Image by the author.If something isn’t on the desk, the model can’t see it. Hold on to that; it becomes important when conversations get long.The most important fact: models forget everythingHere’s the thing that surprises most people. An LLM remembers nothing between calls. Every request starts from a blank slate. When a chat app seems to remember what you said five messages ago, that’s because the app sends the entire conversation again, every single time.Figure 3. Models are stateless: the app re-sends the whole conversation on every call. Image by the author.Three consequences follow, and we’ll meet all of them:Memory is the app’s job, not the model’s.Long conversations cost more, because everything is re-sent every time.Long conversations eventually overflow the desk, so something has to be summarized or dropped.Messages and rolesA conversation is sent as a list of messages, each with a role:The four message roles. Image by the author.The system prompt is the first message: the model’s job description. It’s the main way to shape behavior without retraining anything.2. Talking to a modelAn API call is just a web requestYou talk to a hosted model by POSTing your messages as JSON and getting JSON back:POST https://api.groq.com/openai/v1/chat/completionsAuthorization: Bearer { "model": "openai/gpt-oss-120b", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Say hello in French"} ]}{"choices": [{"message": {"role": "assistant", "content": "Bonjour !"}}]}That’s it. Every chatbot and every agent, Claude Code included, is built on calls like this.One format, many providersA provider is whoever runs the model for you. A lot of them accept the same request format (the “OpenAI chat completions” format above), which is great news for us: one piece of code can talk to all of them. Switching changes only three things: the URL, the API key and the model name.Figure 4. One request format, four providers: only the URL, the key and the model name change. Image by the author.Cloud providers vs a model on your own machine. Image by the author.Open vs closed: open-weight models (Llama, Qwen, gpt-oss, Gemma) can be downloaded and run anywhere, including on your laptop with Ollama. Closed models (Claude, GPT-5) are only available through their maker’s API.Rate limitsFree tiers cap how much you can send per minute or per day. Go over and the provider answers 429 Too Many Requests, often with a Retry-After header saying how long to wait. A good client waits and tries again instead of crashing, and ours will.3. From chatbot to agentWhat’s the difference?What separates a chatbot from an agent. Image by the author.Tool calling: how a model “does” anythingA model can’t open a file or run a command. It can only write text. Tool calling is a neat trick built on that:You describe your tools to the model as JSON: “there’s a tool called write_file that takes a path and content".When the model wants to use one, instead of a normal answer it replies with a tool call: structured JSON naming the tool and its arguments.Your code runs the real function and sends the result back as a tool message.The model reads the result and decides what to do next.Figure 5. Tool calling: the model asks, your code checks and runs the tool, and the result goes back. Image by the author.Here’s what a tool call really looks like when it comes back from the model:{ "role": "assistant", "content": "", "tool_calls": [{ "id": "call_1", "type": "function", "function": {"name": "write_file", "arguments": "{\"path\": \"hello.py\", \"content\": \"print('hi')\"}"} }]}And the result goes back linked by the same id:{"role": "tool", "tool_call_id": "call_1", "content": "saved hello.py (12 characters)"}The key insight: the model only ever requests an action. Your code decides whether it happens. Hold on to this; all of the safety in this post rests on it.The agent loopPut tool calling inside a loop and you have an agent:Figure 6. The agent loop: keep calling the model until it answers without asking for a tool. Image by the author.This pattern is often called ReAct (reason, act, observe, repeat). What makes it robust is how it treats failure: a missing file, a failing command, a blocked request all go back to the model as text, and it adapts. Errors are information, not crashes.4. The harness: what Claude Code really isModel vs harnessThe model is the brain. The harness is everything around it that turns that brain into a useful worker: the loop, the tools, the safety rules, the memory and the interface you type into.Figure 7. The model is the brain; the harness is everything around it. Image by the author.Same model, different harness, very different results. Much of what makes Claude Code feel good lives in the harness: which tools it has, what goes into its context, and what it’s allowed to do.The layers of every coding agentFigure 8. The layers every coding agent has, from the interface down to the tools. Image by the author.Now let’s build every one of those layers ourselves.5. Building one, step by stepRunning itThe whole agent is one file, tiny_agent.py (it's in the appendix). You need Python 3.10 or newer and nothing else. Put a .env file next to it:# .env, next to tiny_agent.pyAGENT_PROVIDER=groqGROQ_API_KEY=your-key-hereOr skip keys entirely: ollama pull qwen3:4b once, then python tiny_agent.py -p ollama. A session looks something like this (the wording varies from model to model):$ python tiny_agent.py[groq | openai/gpt-oss-120b | ask mode | ~8/16,000 tokens]workspace: .../workspace (/help for commands, 'exit' to quit) > create fib.py that prints the first 10 Fibonacci numbers, then run it > write_file {"path": "fib.py", "content": "a, b = 0, 1\nfor _ in range(10): ..."} + write_file wants to run: | fib.py | a, b = 0, 1 | for _ in range(10): | print(a, end=" ") | a, b = b, a + b + allow? [y/N] y > shell {"command": "python fib.py"} + shell wants to run: | python fib.py + allow? [y/N] y fib.py prints the first ten: 0 1 1 2 3 5 8 13 21 34Free keys: Groq · OpenRouter · NVIDIA · or no key at all with Ollama.The shape of the fileFive small classes, each owning one idea, plus a thin command line on top:Figure 9. How tiny_agent.py is organized: five classes and a thin command line. Image by the author.The five classes in tiny_agent.py. Image by the author.Step 1 · A provider is three facts and one methodSince every provider speaks the same format, a provider is just a base URL, a model name and the environment variable holding its key, plus one method that makes the call:@dataclassclass Provider: """Anything that speaks the OpenAI chat-completions format.""" name: str base_url: str model: str key_env: str | None = None # None = no key needed (a local server) def complete(self, messages: list[dict], tools: list[dict]) -> dict: headers = {"Content-Type": "application/json", "User-Agent": "tiny-agent/1.0"} if self.key_env: key = os.getenv(self.key_env) if not key: raise RuntimeError(f"{self.key_env} is not set") headers["Authorization"] = f"Bearer {key}" payload = {"model": self.model, "messages": messages} if tools: payload["tools"] = tools request = urllib.request.Request(f"{self.base_url}/chat/completions", json.dumps(payload).encode(), headers) for attempt in range(5): try: with urllib.request.urlopen(request, timeout=300) as response: return standard_message(json.load(response)["choices"][0]["message"]) except urllib.error.HTTPError as err: if (err.code == 429 or err.code >= 500) and attempt delay = retry_delay(err, attempt) print(f" ... {self.name} is busy, retrying in {delay:.0f}s", file=sys.stderr) time.sleep(delay) continue body = err.read().decode(errors="replace")[:300] raise RuntimeError(f"{self.name}: HTTP {err.code} {body}") from None except urllib.error.URLError as err: raise RuntimeError(f"{self.name}: cannot connect to {self.base_url} ({err.reason})") from NoneReading it top to bottom:key_env=None means "no key needed", which is how a local Ollama server works.Tools are only sent if there are any. Some providers reject an empty list.429 or a 5xx means “try again later”. We wait as long as the server’s Retry-After header asks (or back off 2, 4, 8, 16 seconds) and retry, instead of crashing in the middle of your task.standard_message() keeps only the standard fields of the reply. Providers add extras of their own (some return the model's reasoning), and another provider might reject them. Stripping them is what lets you switch provider halfway through a conversation.Adding a provider is one line in the registry. Notice the same open model family, gpt-oss, runs on all three clouds:PROVIDERS = { "groq": Provider("groq", "https://api.groq.com/openai/v1", "openai/gpt-oss-120b", "GROQ_API_KEY"), "openrouter": Provider("openrouter", "https://openrouter.ai/api/v1", "openai/gpt-oss-20b:free", "OPENROUTER_API_KEY"), "nvidia": Provider("nvidia", "https://integrate.api.nvidia.com/v1", "openai/gpt-oss-120b", "NVIDIA_API_KEY"), "ollama": Provider("ollama", "http://localhost:11434/v1", "qwen3:4b"),}Step 2 · Tools are methods with good docstringsA tool is a Python method on the workspace, marked with a tiny decorator:def tool(method): """Mark a method as a tool. Its docstring is what the model reads: the first line describes the tool, and `name: help` lines describe arguments.""" method.is_tool = True return methodHere’s one of them. It returns the file with line numbers, the way a code editor shows it, so the model can talk about exact lines and copy exact text when it edits:@tool def read_file(self, path: str) -> str: """Read a text file. Lines come back numbered. path: file to read, relative to the workspace""" lines = self.path(path).read_text(encoding="utf-8", errors="replace").splitlines() shown = [f"{n:>4} {line}" for n, line in enumerate(lines[:400], 1)] if len(lines) > 400: shown.append(f"… {len(lines) - 400} more lines not shown") return "\n".join(shown) or "(empty file)"The model needs a JSON description of each tool. Instead of writing those by hand, we build them from the Python function itself: the parameters come from the signature (a default value makes an argument optional), and the descriptions come from the docstring.def tool_schema(name: str, fn) -> dict: """Build the JSON description the model sees, straight from the Python function.""" summary, *arg_lines = inspect.getdoc(fn).splitlines() helps = dict(line.split(":", 1) for line in arg_lines if ":" in line) params = inspect.signature(fn).parameters return {"type": "function", "function": { "name": name, "description": summary, "parameters": { "type": "object", "properties": {p: {"type": "string", "description": helps.get(p, "").strip()} for p in params}, "required": [p for p, info in params.items() if info.default is inspect.Parameter.empty], }}}So grep reaches the model as:{"name": "grep", "description": "Search file contents with a regular expression.", "parameters": {"type": "object", "properties": {"pattern": {"type": "string", "description": "the regex to look for"}, "path": {"type": "string", "description": "folder or file to search (default: the whole workspace)"}}, "required": ["pattern"]}}The agent gets seven tools, a small version of a real coding agent’s toolbox:The seven tools, and what each maps to in Claude Code. Image by the author.Two design choices are worth copying:Search before reading. find_files and grep let the model find the three relevant lines instead of reading twenty whole files, which saves a lot of context.Exact replacement. replace_in_file refuses unless old appears exactly once, so an edit can never land in the wrong place.Step 3 · The workspace: what the agent may touchEvery path the model gives us goes through one check before anything is read or written:class Workspace: """The folder the agent lives in. Every path is checked against it.""" def __init__(self, root: str | Path): self.root = Path(root).resolve() self.root.mkdir(parents=True, exist_ok=True) def path(self, relative: str) -> Path: full = (self.root / relative).resolve() if not full.is_relative_to(self.root): raise PermissionError(f"{relative} is outside the workspace") return fullresolve() turns chains of .. steps, absolute paths and symlinked shortcuts into a real location first, and then we check it's still inside the workspace. Checking the raw string instead is a classic mistake.Commands can’t be path-checked the same way, so they get their own protections:@tool def shell(self, command: str) -> str: """Run a shell command in the workspace and return its output (2-minute limit). command: the command line to run""" env = {k: v for k, v in os.environ.items() if k.upper() in SAFE_ENV} done = subprocess.run(command, shell=True, cwd=self.root, env=env, timeout=120, capture_output=True, text=True, errors="replace") output = (done.stdout + done.stderr).strip() if len(output) > 8000: output = output[:3000] + "\n… (middle cut) …\n" + output[-4000:] return f"[exit {done.returncode}]\n{output}"They run inside the workspace, and are stopped after two minutes.They get an allowlist environment. Instead of trying to remove every secret by name, we pass only the variables a shell actually needs (PATH, HOME, TEMP and a few more). Your API keys, tokens and cloud credentials simply aren't there, so echo $GROQ_API_KEY prints nothing.Long output keeps its start and end: the start shows what ran, and the end usually holds the error.Step 4 · The loop, as a stream of eventsHere’s the heart of the agent. run() is a Python generator: instead of printing, it yields events (a tool call, a thought, the answer) and lets whoever is driving it decide how to show them. The command line prints them; a web page or a test could use the same agent unchanged.def run(self, task: str): """Work on one task. Yields (kind, text) events as it goes.""" self.convo.messages[0]["content"] = SYSTEM.format( root=self.workspace.root, os=platform.system(), mode=self.mode, notes=self.workspace.project_notes()) report = self.convo.summarize_old_tasks(self.provider) if report: yield "info", report mark = len(self.convo.messages) self.convo.messages.append({"role": "user", "content": task}) try: for _ in range(self.MAX_STEPS): self.convo.clear_old_outputs() reply = self.provider.complete(self.convo.messages, self.schemas) self.convo.messages.append(reply) if "tool_calls" not in reply: yield "answer", reply["content"] return if reply["content"]: yield "thought", reply["content"] for call in reply["tool_calls"]: yield "tool", f"{call['function']['name']} {call['function']['arguments'][:120]}" result = self.use_tool(call) self.convo.messages.append( {"role": "tool", "tool_call_id": call["id"], "content": result}) yield "answer", f"(gave up after {self.MAX_STEPS} steps)" except (Exception, KeyboardInterrupt) as err: del self.convo.messages[mark:] # a half-finished turn would break the next request yield "error", str(err) or "interrupted"What happens on each task:Refresh the system prompt, so the model always sees the current mode and project notes.If the conversation is over budget, summarize old tasks (Step 8).Remember where we are (mark), add the task, then loop: call the model; if it answered in plain text we're done; otherwise run each tool and add the results.If anything blows up halfway (network error, Ctrl-C), cut the conversation back to the mark. The API requires every tool call to have a matching result, and one half-finished turn would make every later request fail.Every tool call goes through use_tool, and every way it can fail becomes a sentence the model reads:def use_tool(self, call: dict) -> str: """Every way this can fail becomes a message the model can read.""" name = call["function"]["name"] try: args = json.loads(call["function"]["arguments"]) except json.JSONDecodeError: return "error: the arguments were not valid JSON" if name not in self.tools: return f"error: no tool named {name}" veto = self.hooks.fire("before_tool", name=name, args=args) if veto: return f"denied by a hook: {veto}" decision, reason = decide(name, args, self.mode) if decision == "ask" and not self.approve(name, args): decision, reason = "deny", "the user declined" if decision == "deny": return f"denied: {reason}" try: result = str(self.tools[name](**args)) except Exception as err: result = f"error: {type(err).__name__}: {err}" self.hooks.fire("after_tool", name=name, args=args, result=result) return resultFigure 10. The path of one tool call. Every failure goes back to the model as text. Image by the author.Nothing the model does can crash the agent. A bad argument, a missing file or a refusal comes back as text, and the model tries something else.Step 5 · Permissions: allow, ask, denyThis is where “the model requests, the code decides” becomes real. Claude Code uses permission rules: allow this, ask about that, never do the other. We do the same with three small pieces: a Rule, a list of them, and a decide() function.@dataclassclass Rule: decision: str # "allow", "ask" or "deny" tool: str # tool name, wildcards allowed target: str = "*" # pattern for the command or path, wildcards allowed RULES = [ Rule("deny", "shell", "*git push*--force*"), # rewrites history your team already pulled Rule("deny", "shell", "*git reset --hard*"), # throws away uncommitted work Rule("deny", "shell", "*git clean -*f*"), # deletes untracked files for good Rule("deny", "shell", "*npm publish*"), # ships your code to the world Rule("deny", "shell", "*docker system prune*"), # wipes every image and container Rule("deny", "shell", "*shutdown*"), # turns the machine off Rule("deny", "*", "*.env*"), # secrets stay secret Rule("allow", "read_file"), Rule("allow", "find_files"), Rule("allow", "grep"), Rule("allow", "note"), Rule("allow", "shell", "git status*"), Rule("allow", "shell", "git diff*"),]READ_ONLY = {"read_file", "find_files", "grep"} def decide(name: str, args: dict, mode: str) -> tuple[str, str]: """Return (decision, reason). Deny beats everything; then the mode; then allow rules.""" target = str(args.get("command") or args.get("path") or args.get("pattern") or "").lower() matches = [r for r in RULES if fnmatch.fnmatch(name, r.tool) and fnmatch.fnmatch(target, r.target.lower())] for rule in matches: if rule.decision == "deny": return "deny", f"matches the deny rule {rule.tool}({rule.target})" if mode == "plan" and name not in READ_ONLY: return "deny", "plan mode is read-only" if any(r.decision == "allow" for r in matches) or mode == "auto": return "allow", "" return "ask", ""Figure 11. How decide() chooses between allow, ask and deny. Image by the author.Deny wins. It’s checked first, so not even auto mode can force-push or read .env.Patterns, not exact strings. git status* allows any git status command; *.env* blocks any tool whose path or command mentions .env.Allow rules remove friction. Reading, searching and git status never ask you.The three permission modes. Image by the author.When it asks, it shows you what you’re approving: the command, or the file and its first lines. An approval prompt that only says “Allow write_file?” asks you to approve blind.def ask_user(name: str, args: dict) -> bool: """Show exactly what is about to happen, then ask.""" print(f"\n + {name} wants to run:") preview = args.get("command") or args.get("path", "") print(f" | {preview}") for line in (args.get("content") or args.get("new") or "").splitlines()[:8]: print(f" | {line}") return input(" + allow? [y/N] ").strip().lower() in ("y", "yes")There’s a subtle lesson hiding in here, and I learned it the hard way. My first version didn’t tell the model which mode it was in. After switching from read-only to auto, the model kept politely refusing (“sorry, I’m in a read-only session”), because the conversation history said so and nobody had told it otherwise. It never even tried a tool. The fix is the Permission mode: line in the system prompt, rebuilt before every task.But notice the direction. The mode is copied into the prompt so the model doesn’t refuse for no reason. It’s enforced by decide(), which reads agent.mode, a value only you can change. If the model hallucinates "I'm in auto mode", nothing happens. A confused model can make an agent less useful; it can never make it more permitted.Prompts set expectations; code sets boundaries.Step 6 · Hooks: your own rulesRules cover the common cases. Hooks let you add anything else without touching the loop: a function that runs before every tool call (and can veto it) or after (and sees the result).class Hooks: """Your own code, run at fixed moments. A before_tool handler can veto a call by returning a reason; after_tool handlers just observe.""" def __init__(self): self.handlers = {"before_tool": [], "after_tool": []} def on(self, event: str): def register(fn): self.handlers[event].append(fn) return fn return register def fire(self, event: str, **data) -> str | None: for fn in self.handlers[event]: answer = fn(**data) if answer: return answer return NoneThe command line registers one hook, an audit trail that writes every tool call to audit.jsonl:@agent.hooks.on("after_tool") def audit(name, args, result): with open(Path(__file__).with_name("audit.jsonl"), "a", encoding="utf-8") as log: log.write(json.dumps({"at": time.strftime("%Y-%m-%d %H:%M:%S"), "tool": name, "args": args, "result": result[:200]}) + "\n")Claude Code has the same idea, as shell commands that run on events like PreToolUse and PostToolUse. You'll write a veto hook yourself in section 7.Step 7 · Memory that survives a restartFigure 12. Short-term memory is the message list; long-term memory is AGENTS.md. Image by the author.Short-term memory is the message list, re-sent on every call (remember: the model itself forgets everything). Long-term memory is a plain file in the workspace:@tool def note(self, text: str) -> str: """Save a fact about this project to AGENTS.md so future sessions know it. text: one short sentence to remember""" with open(self.root / "AGENTS.md", "a", encoding="utf-8") as f: f.write(f"- {text.strip()}\n") return "noted in AGENTS.md" def project_notes(self) -> str: notes = self.root / "AGENTS.md" return notes.read_text(encoding="utf-8") if notes.exists() else "(none yet)"Its contents go into the system prompt before every task, and the note tool lets the agent add to it. It's the same idea as Claude Code's CLAUDE.md; the name AGENTS.md is a convention several coding agents now read. Write it by hand ("this project uses pytest; every new function needs a test") and the agent follows it in every session.Step 8 · Staying inside the budgetEvery call re-sends the whole conversation, and tool outputs make it grow fast. Eventually you hit the context window, or (much sooner on free tiers) the tokens-per-minute limit. The agent handles this in two stages, cheapest first:Figure 13. Two stages keep the conversation inside its token budget, cheapest first. Image by the author.Stage 1 is free. Old tool outputs are the bulkiest part of the conversation, and the model has already used them. If it needs one again, it can simply run the tool again.def clear_old_outputs(self, keep_last: int = 6): """Stage 1, cheap: blank out tool outputs the model has already used.""" if self.tokens() return for message in self.messages[:-keep_last]: if message["role"] == "tool" and len(message["content"]) > 200: message["content"] = "[output cleared to save space; run the tool again if needed]"Stage 2 costs one extra model call. Between tasks, everything except the last few tasks is replaced by a short summary written by the model:def summarize_old_tasks(self, provider: Provider, force: bool = False) -> str | None: """Stage 2, between tasks: fold everything but the last few tasks into a summary.""" if not force and self.tokens() return None starts = [i for i, m in enumerate(self.messages) if m["role"] == "user"] if len(starts) return None cut = starts[-self.keep_tasks] old, before = self.messages[1:cut], self.tokens() transcript = "\n".join(f"{m['role']}: {(m.get('content') or '')[:1500]}" + "".join(f" " for c in m.get("tool_calls", [])) for m in old) try: summary = provider.complete([ {"role": "system", "content": "Summarize this coding session in short bullets: goals, " "decisions, files touched, results, open items."}, {"role": "user", "content": transcript}], [])["content"] except Exception as err: summary = f"(summary unavailable: {err})" self.messages[1:cut] = [{"role": "user", "content": f"[Earlier in this session]\n{summary}"}, {"role": "assistant", "content": "Understood."}] return f"summarized {len(old)} old messages: ~{before:,} -> ~{self.tokens():,} tokens"Two details keep this safe. Stage 2 only cuts at the start of a task (a user message), so a tool call is never separated from its result. And if the summary call itself fails, the agent leaves a short note instead of crashing.Try --budget 2000 to watch it happen: after a few tasks you'll see a line like i summarized 14 old messages: ~2,400 -> ~900 tokens. Ask about your first task and the agent still knows, from the summary. Claude Code does the same automatically, and lets you trigger it with /compact, as this agent does.6. Safety: the part that matters mostWhat could go wrong?A coding agent runs real commands with your permissions. The dangers come from three places:Where the danger comes from, and what defends against it. Image by the author.Prompt injectionThe model can’t reliably tell your instructions apart from text it happens to read. If your agent opens a README that contains…a model might try to obey it. No prompt can fully fix this. What you can do is make sure a fooled model still can't do damage. Here, a deny rule blocks git push --force, and ask mode shows you any other command before it runs.Defense in depthNo single layer is perfect, so stack them:Figure 14. Defense in depth: six layers between the model’s request and the action. Image by the author.Be honest about the sandboxLevels of sandboxing, from a path check to an OS-level sandbox. Image by the author.Our shell tool can still read files elsewhere with an absolute path, and a determined rewording can slip past wildcard deny rules. That's fine for learning. For real work on a machine you care about, stay in ask mode, or run the agent inside Docker.7. Try it yourselfEach experiment takes a few minutes and teaches one idea.1. First task. create hello.py that prints hello, then run it. Watch the > lines: that's the loop, one event at a time.2. Swap the brain. /provider nvidia (or /provider ollama), then the same task. The status line in brackets shows who's answering.3. Search instead of reading. Point it at a real project in plan mode (python tiny_agent.py -w path/to/project --mode plan) and ask where is the database connection set up?. Watch it reach for grep and find_files before reading anything.4. Poke the workspace. read ../tiny_agent.py comes back as outside the workspace. run: echo %GROQ_API_KEY% ($GROQ_API_KEY on Mac/Linux) prints nothing.5. Test the rules. /mode plan, then create notes.txt is denied. /mode auto, then force-push this branch to main is denied by a rule even in auto. (A well-behaved model may refuse on its own first. If it hadn't, the rule would have stopped it.) Then what does git status say? runs without asking, thanks to an allow rule.6. Write a veto hook. Add this inside main(), next to the audit hook, then ask show me .git/config:@agent.hooks.on("before_tool")def keep_out_of_git(name, args): if ".git" in str(args.get("path", "")) + str(args.get("command", "")): return "the .git folder is off limits"7. Add an allow rule. Put Rule("allow", "shell", "python -m pytest*") in RULES, and running the tests stops asking for permission.8. Memory across restarts. remember that this project uses pytest and every function needs a test, exit, restart, then create a function that reverses a string. It writes the test unprompted. Open workspace/AGENTS.md.9. Watch the budget. Start with --budget 2000, do a few tasks, check /status, then ask about your first task.10. Add a tool. Add this method to Workspace, and the model can use it straight away:@tool def count_lines(self, path: str) -> str: """Count the lines in a file. path: file to count""" return f"{len(self.path(path).read_text(encoding='utf-8').splitlines())} lines"There’s no schema to write: the docstring is the schema. Since it only reads, add Rule("allow", "count_lines") so it doesn't ask, and add it to READ_ONLY so plan mode allows it too.11. Add a provider. Any OpenAI-compatible service is one line in PROVIDERS: "together": Provider("together", "https://api.together.xyz/v1", "", "TOGETHER_API_KEY").8. From toy to productionOur agent has every idea Claude Code has. What a production harness adds is the engineering that makes those ideas fast, safe and pleasant at scale:Our agent vs Claude Code, area by area. Image by the author.If you keep building, I’d add them in this order: streaming (the biggest quality-of-life win), parallel tool calls, a real container sandbox, then MCP.9. When things go wrongGROQ_API_KEY is not set: No key for that provider. Fix: Add it to .env, or use -p ollamaHTTP 401: Wrong or expired key. Fix: Create a new key... groq is busy, retrying in Ns: Rate limited. Fix: Just wait. A smaller --budget sends less per calldoes not support tools: The model can't call tools (e.g. gemma3). Fix: Use qwen3, llama3.1, gpt-oss or the defaultcannot connect to http://localhost:11434/v1: Ollama isn't running. Fix: Start the Ollama app, or ollama serveHTTP 404 … not found on Ollama: Model not downloaded. Fix: ollama pull qwen3:4bEvery write is denied: You're in plan mode. Fix: /mode askIt forgets early details: Old tasks were summarized. Fix: Raise --budget, or put key facts in AGENTS.md(gave up after 30 steps): The model went in circles. Fix: Give it a smaller, clearer task10. GlossaryAgent: A program that uses a model in a loop, with tools, to finish tasksAgent loop: Call model → run requested tools → add results → repeatAudit log: A record of every action the agent tookCompaction: Summarizing old conversation so it fits in the context windowContext window: The most text a model can consider at once, in tokensDefense in depth: Several safety layers, so one failure isn’t fatalDeny rule: A pattern that is always blocked, like git push --forceGateway: The one piece of code that talks to the model APIGuardrails: Code that decides whether a requested action may runHallucination: A model confidently stating something falseHarness: Everything around the model: loop, tools, rules, memory, interfaceHook: Your own code that runs on an event, like before a tool callLLM: Large language model: predicts the next tokens of textMCP: Model Context Protocol, a standard for plugging tools into agentsOllama: A program that runs open models on your own computerOpenAI-compatible: Accepts the OpenAI chat-completions request formatPermission mode: plan / ask / auto: how much the agent may do unaskedPrompt caching: Re-using unchanged context between calls to save time and moneyPrompt injection: Hidden instructions in data the agent reads, trying to hijack itProvider: A service that runs models: Groq, OpenRouter, NVIDIA, Anthropic…Rate limit: A cap on requests or tokens per minute or day (HTTP 429)ReAct: Reason + act: the think → act → observe pattern behind agentsSandbox: A boundary that limits what the agent can touchStateless: Keeps no memory between calls, as every LLM isStreaming: Receiving the reply piece by piece as it’s generatedSub-agent: A helper agent with its own fresh contextSystem prompt: The first message: the agent’s rules and job descriptionToken: A chunk of text (about ¾ of a word); what models read and bill inTool call: The model’s structured request to run a toolTurn: One task you typed, plus everything the agent did for it11. Key takeawaysModels are stateless. The harness owns memory and context, and re-sends the conversation on every call.Models request; code decides. Never put a security rule only in a prompt.Context is a budget. Keep what matters, summarize the rest.Errors are information. Feed them back and the agent fixes its own mistakes.The harness matters as much as the model. Tools, context and guardrails are where a good agent is made.Further readingClaude Code documentation (see the Claude Code section)OpenAI’s function calling guide, the tool format used hereReAct: Synergizing Reasoning and Acting in Language ModelsModel Context Protocol, the standard for plugging tools into agentsOllama’s OpenAI compatibility, for running the agent locally12. Appendix: the complete codeSave this as tiny_agent.py and run python tiny_agent.py --help. Settings come from command-line flags, or from environment variables / a .env file next to it: AGENT_PROVIDER, AGENT_MODEL, AGENT_MODE, AGENT_WORKSPACE, AGENT_BUDGET, plus your provider's key (GROQ_API_KEY, OPENROUTER_API_KEY or NVIDIA_API_KEY; none needed for Ollama)."""tiny_agent.py: a coding agent you can read in one sitting. One file, standard library only. Talks to any OpenAI-compatible provider(Groq, OpenRouter, NVIDIA, or Ollama on your own machine), works inside aworkspace folder, asks before it does anything risky, and keeps long sessionsinside a token budget. python tiny_agent.py # chat python tiny_agent.py --mode plan # read-only exploring python tiny_agent.py -p ollama "add tests for utils.py"""" from __future__ import annotations import argparseimport fnmatchimport inspectimport jsonimport osimport platformimport reimport subprocessimport sysimport timeimport urllib.errorimport urllib.requestfrom dataclasses import dataclassfrom pathlib import Path # ─── Providers ──────────────────────────────────────────────────────────────── @dataclassclass Provider: """Anything that speaks the OpenAI chat-completions format.""" name: str base_url: str model: str key_env: str | None = None # None = no key needed (a local server) def complete(self, messages: list[dict], tools: list[dict]) -> dict: headers = {"Content-Type": "application/json", "User-Agent": "tiny-agent/1.0"} if self.key_env: key = os.getenv(self.key_env) if not key: raise RuntimeError(f"{self.key_env} is not set") headers["Authorization"] = f"Bearer {key}" payload = {"model": self.model, "messages": messages} if tools: payload["tools"] = tools request = urllib.request.Request(f"{self.base_url}/chat/completions", json.dumps(payload).encode(), headers) for attempt in range(5): try: with urllib.request.urlopen(request, timeout=300) as response: return standard_message(json.load(response)["choices"][0]["message"]) except urllib.error.HTTPError as err: if (err.code == 429 or err.code >= 500) and attempt delay = retry_delay(err, attempt) print(f" ... {self.name} is busy, retrying in {delay:.0f}s", file=sys.stderr) time.sleep(delay) continue body = err.read().decode(errors="replace")[:300] raise RuntimeError(f"{self.name}: HTTP {err.code} {body}") from None except urllib.error.URLError as err: raise RuntimeError(f"{self.name}: cannot connect to {self.base_url} ({err.reason})") from None def retry_delay(err: urllib.error.HTTPError, attempt: int) -> float: """Use the server's Retry-After if it sent one, otherwise back off: 2, 4, 8, 16s.""" try: return min(float(err.headers.get("Retry-After")), 60) except (TypeError, ValueError): return 2 ** (attempt + 1) def standard_message(raw: dict) -> dict: """Keep only the standard fields, so the history works with any provider.""" message = {"role": "assistant", "content": raw.get("content") or ""} calls = [] for call in raw.get("tool_calls") or []: args = call["function"].get("arguments") or "{}" calls.append({"id": call["id"], "type": "function", "function": {"name": call["function"]["name"], "arguments": args if isinstance(args, str) else json.dumps(args)}}) if calls: message["tool_calls"] = calls return message PROVIDERS = { "groq": Provider("groq", "https://api.groq.com/openai/v1", "openai/gpt-oss-120b", "GROQ_API_KEY"), "openrouter": Provider("openrouter", "https://openrouter.ai/api/v1", "openai/gpt-oss-20b:free", "OPENROUTER_API_KEY"), "nvidia": Provider("nvidia", "https://integrate.api.nvidia.com/v1", "openai/gpt-oss-120b", "NVIDIA_API_KEY"), "ollama": Provider("ollama", "http://localhost:11434/v1", "qwen3:4b"),} # ─── Tools ──────────────────────────────────────────────────────────────────── def tool(method): """Mark a method as a tool. Its docstring is what the model reads: the first line describes the tool, and `name: help` lines describe arguments.""" method.is_tool = True return method SAFE_ENV = {"PATH", "PATHEXT", "SYSTEMROOT", "SYSTEMDRIVE", "WINDIR", "COMSPEC", "TEMP", "TMP", "HOME", "USERPROFILE", "APPDATA", "LOCALAPPDATA", "LANG", "TERM", "SHELL", "USER"} class Workspace: """The folder the agent lives in. Every path is checked against it.""" def __init__(self, root: str | Path): self.root = Path(root).resolve() self.root.mkdir(parents=True, exist_ok=True) def path(self, relative: str) -> Path: full = (self.root / relative).resolve() if not full.is_relative_to(self.root): raise PermissionError(f"{relative} is outside the workspace") return full @tool def read_file(self, path: str) -> str: """Read a text file. Lines come back numbered. path: file to read, relative to the workspace""" lines = self.path(path).read_text(encoding="utf-8", errors="replace").splitlines() shown = [f"{n:>4} {line}" for n, line in enumerate(lines[:400], 1)] if len(lines) > 400: shown.append(f"… {len(lines) - 400} more lines not shown") return "\n".join(shown) or "(empty file)" @tool def write_file(self, path: str, content: str) -> str: """Create a file, or replace everything in it. path: file to write, relative to the workspace content: the complete new content""" target = self.path(path) target.parent.mkdir(parents=True, exist_ok=True) target.write_text(content, encoding="utf-8") return f"saved {path} ({len(content)} characters)" @tool def replace_in_file(self, path: str, old: str, new: str) -> str: """Replace one exact piece of text in a file. `old` must appear exactly once. path: file to change old: the exact text to replace, copied from the file new: the replacement text""" target = self.path(path) text = target.read_text(encoding="utf-8") found = text.count(old) if found != 1: return f"error: expected `old` exactly once in {path}, found it {found} times" target.write_text(text.replace(old, new), encoding="utf-8") return f"updated {path}" @tool def find_files(self, pattern: str) -> str: """Find files by name pattern, e.g. **/*.py or src/*.js. pattern: a glob pattern, relative to the workspace""" if ".." in Path(pattern).parts or Path(pattern).is_absolute(): raise PermissionError("patterns must stay inside the workspace") hits = [str(p.relative_to(self.root)) for p in sorted(self.root.glob(pattern)) if p.is_file() and ".git" not in p.parts] return "\n".join(hits[:200]) or "no files match" @tool def grep(self, pattern: str, path: str = ".") -> str: """Search file contents with a regular expression. pattern: the regex to look for path: folder or file to search (default: the whole workspace)""" start = self.path(path) files = [start] if start.is_file() else [p for p in start.rglob("*") if p.is_file() and ".git" not in p.parts] regex, hits = re.compile(pattern), [] for file in files: try: for n, line in enumerate(file.read_text(encoding="utf-8").splitlines(), 1): if regex.search(line): hits.append(f"{file.relative_to(self.root)}:{n}: {line.strip()[:160]}") except (UnicodeDecodeError, OSError): continue # skip binary and unreadable files return "\n".join(hits[:100]) or "no matches" @tool def shell(self, command: str) -> str: """Run a shell command in the workspace and return its output (2-minute limit). command: the command line to run""" env = {k: v for k, v in os.environ.items() if k.upper() in SAFE_ENV} done = subprocess.run(command, shell=True, cwd=self.root, env=env, timeout=120, capture_output=True, text=True, errors="replace") output = (done.stdout + done.stderr).strip() if len(output) > 8000: output = output[:3000] + "\n… (middle cut) …\n" + output[-4000:] return f"[exit {done.returncode}]\n{output}" @tool def note(self, text: str) -> str: """Save a fact about this project to AGENTS.md so future sessions know it. text: one short sentence to remember""" with open(self.root / "AGENTS.md", "a", encoding="utf-8") as f: f.write(f"- {text.strip()}\n") return "noted in AGENTS.md" def project_notes(self) -> str: notes = self.root / "AGENTS.md" return notes.read_text(encoding="utf-8") if notes.exists() else "(none yet)" def tools(self) -> dict: return {name: fn for name, fn in inspect.getmembers(self, inspect.ismethod) if getattr(fn, "is_tool", False)} def tool_schema(name: str, fn) -> dict: """Build the JSON description the model sees, straight from the Python function.""" summary, *arg_lines = inspect.getdoc(fn).splitlines() helps = dict(line.split(":", 1) for line in arg_lines if ":" in line) params = inspect.signature(fn).parameters return {"type": "function", "function": { "name": name, "description": summary, "parameters": { "type": "object", "properties": {p: {"type": "string", "description": helps.get(p, "").strip()} for p in params}, "required": [p for p, info in params.items() if info.default is inspect.Parameter.empty], }}} # ─── Permissions ────────────────────────────────────────────────────────────── @dataclassclass Rule: decision: str # "allow", "ask" or "deny" tool: str # tool name, wildcards allowed target: str = "*" # pattern for the command or path, wildcards allowed RULES = [ Rule("deny", "shell", "*git push*--force*"), # rewrites history your team already pulled Rule("deny", "shell", "*git reset --hard*"), # throws away uncommitted work Rule("deny", "shell", "*git clean -*f*"), # deletes untracked files for good Rule("deny", "shell", "*npm publish*"), # ships your code to the world Rule("deny", "shell", "*docker system prune*"), # wipes every image and container Rule("deny", "shell", "*shutdown*"), # turns the machine off Rule("deny", "*", "*.env*"), # secrets stay secret Rule("allow", "read_file"), Rule("allow", "find_files"), Rule("allow", "grep"), Rule("allow", "note"), Rule("allow", "shell", "git status*"), Rule("allow", "shell", "git diff*"),]READ_ONLY = {"read_file", "find_files", "grep"} def decide(name: str, args: dict, mode: str) -> tuple[str, str]: """Return (decision, reason). Deny beats everything; then the mode; then allow rules.""" target = str(args.get("command") or args.get("path") or args.get("pattern") or "").lower() matches = [r for r in RULES if fnmatch.fnmatch(name, r.tool) and fnmatch.fnmatch(target, r.target.lower())] for rule in matches: if rule.decision == "deny": return "deny", f"matches the deny rule {rule.tool}({rule.target})" if mode == "plan" and name not in READ_ONLY: return "deny", "plan mode is read-only" if any(r.decision == "allow" for r in matches) or mode == "auto": return "allow", "" return "ask", "" # ─── Hooks ──────────────────────────────────────────────────────────────────── class Hooks: """Your own code, run at fixed moments. A before_tool handler can veto a call by returning a reason; after_tool handlers just observe.""" def __init__(self): self.handlers = {"before_tool": [], "after_tool": []} def on(self, event: str): def register(fn): self.handlers[event].append(fn) return fn return register def fire(self, event: str, **data) -> str | None: for fn in self.handlers[event]: answer = fn(**data) if answer: return answer return None # ─── Conversation (short-term memory) ───────────────────────────────────────── class Conversation: """The message list, plus the two ways we keep it inside the token budget.""" def __init__(self, budget: int = 16_000, keep_tasks: int = 3): self.messages = [{"role": "system", "content": ""}] self.budget, self.keep_tasks = budget, keep_tasks def tokens(self) -> int: return len(json.dumps(self.messages)) // 4 # rough: ~4 characters per token def clear_old_outputs(self, keep_last: int = 6): """Stage 1, cheap: blank out tool outputs the model has already used.""" if self.tokens() return for message in self.messages[:-keep_last]: if message["role"] == "tool" and len(message["content"]) > 200: message["content"] = "[output cleared to save space; run the tool again if needed]" def summarize_old_tasks(self, provider: Provider, force: bool = False) -> str | None: """Stage 2, between tasks: fold everything but the last few tasks into a summary.""" if not force and self.tokens() return None starts = [i for i, m in enumerate(self.messages) if m["role"] == "user"] if len(starts) return None cut = starts[-self.keep_tasks] old, before = self.messages[1:cut], self.tokens() transcript = "\n".join(f"{m['role']}: {(m.get('content') or '')[:1500]}" + "".join(f" " for c in m.get("tool_calls", [])) for m in old) try: summary = provider.complete([ {"role": "system", "content": "Summarize this coding session in short bullets: goals, " "decisions, files touched, results, open items."}, {"role": "user", "content": transcript}], [])["content"] except Exception as err: summary = f"(summary unavailable: {err})" self.messages[1:cut] = [{"role": "user", "content": f"[Earlier in this session]\n{summary}"}, {"role": "assistant", "content": "Understood."}] return f"summarized {len(old)} old messages: ~{before:,} -> ~{self.tokens():,} tokens" # ─── The agent ──────────────────────────────────────────────────────────────── SYSTEM = """You are tiny-agent, a careful coding assistant working in {root} on {os}.Before editing a file, read it. After changing code, run it or its tests.Permission mode: {mode}. plan = read-only, ask = the user approves risky actions,auto = no approvals. Always attempt the tool you need; the harness enforces the rules.If a result says "denied", explain it to the user instead of working around it. Project notes (AGENTS.md):{notes}""" class Agent: MAX_STEPS = 30 def __init__(self, provider: Provider, workspace: Workspace, mode: str = "ask", approve=None, budget: int = 16_000, keep_tasks: int = 3): self.provider, self.workspace, self.mode = provider, workspace, mode self.approve = approve or (lambda name, args: False) self.tools = workspace.tools() self.schemas = [tool_schema(n, fn) for n, fn in self.tools.items()] self.hooks = Hooks() self.convo = Conversation(budget, keep_tasks) def run(self, task: str): """Work on one task. Yields (kind, text) events as it goes.""" self.convo.messages[0]["content"] = SYSTEM.format( root=self.workspace.root, os=platform.system(), mode=self.mode, notes=self.workspace.project_notes()) report = self.convo.summarize_old_tasks(self.provider) if report: yield "info", report mark = len(self.convo.messages) self.convo.messages.append({"role": "user", "content": task}) try: for _ in range(self.MAX_STEPS): self.convo.clear_old_outputs() reply = self.provider.complete(self.convo.messages, self.schemas) self.convo.messages.append(reply) if "tool_calls" not in reply: yield "answer", reply["content"] return if reply["content"]: yield "thought", reply["content"] for call in reply["tool_calls"]: yield "tool", f"{call['function']['name']} {call['function']['arguments'][:120]}" result = self.use_tool(call) self.convo.messages.append( {"role": "tool", "tool_call_id": call["id"], "content": result}) yield "answer", f"(gave up after {self.MAX_STEPS} steps)" except (Exception, KeyboardInterrupt) as err: del self.convo.messages[mark:] # a half-finished turn would break the next request yield "error", str(err) or "interrupted" def use_tool(self, call: dict) -> str: """Every way this can fail becomes a message the model can read.""" name = call["function"]["name"] try: args = json.loads(call["function"]["arguments"]) except json.JSONDecodeError: return "error: the arguments were not valid JSON" if name not in self.tools: return f"error: no tool named {name}" veto = self.hooks.fire("before_tool", name=name, args=args) if veto: return f"denied by a hook: {veto}" decision, reason = decide(name, args, self.mode) if decision == "ask" and not self.approve(name, args): decision, reason = "deny", "the user declined" if decision == "deny": return f"denied: {reason}" try: result = str(self.tools[name](**args)) except Exception as err: result = f"error: {type(err).__name__}: {err}" self.hooks.fire("after_tool", name=name, args=args, result=result) return result # ─── Command line ───────────────────────────────────────────────────────────── def ask_user(name: str, args: dict) -> bool: """Show exactly what is about to happen, then ask.""" print(f"\n + {name} wants to run:") preview = args.get("command") or args.get("path", "") print(f" | {preview}") for line in (args.get("content") or args.get("new") or "").splitlines()[:8]: print(f" | {line}") return input(" + allow? [y/N] ").strip().lower() in ("y", "yes") def load_env_file(path: Path): if path.exists(): for line in path.read_text(encoding="utf-8").splitlines(): if "=" in line and not line.lstrip().startswith("#"): key, value = line.split("=", 1) os.environ.setdefault(key.strip(), value.strip().strip("'\"")) def main(): load_env_file(Path(__file__).with_name(".env")) cli = argparse.ArgumentParser(description="tiny-agent: a minimal coding agent") cli.add_argument("task", nargs="*", help="run one task and exit") cli.add_argument("-p", "--provider", default=os.getenv("AGENT_PROVIDER", "groq"), choices=PROVIDERS) cli.add_argument("-m", "--model", default=os.getenv("AGENT_MODEL")) cli.add_argument("--mode", default=os.getenv("AGENT_MODE", "ask"), choices=["plan", "ask", "auto"]) cli.add_argument("-w", "--workspace", default=os.getenv("AGENT_WORKSPACE", "workspace")) cli.add_argument("--budget", type=int, default=int(os.getenv("AGENT_BUDGET", "16000"))) opts = cli.parse_args() provider = PROVIDERS[opts.provider] if opts.model: provider.model = opts.model agent = Agent(provider, Workspace(opts.workspace), opts.mode, ask_user, opts.budget) @agent.hooks.on("after_tool") def audit(name, args, result): with open(Path(__file__).with_name("audit.jsonl"), "a", encoding="utf-8") as log: log.write(json.dumps({"at": time.strftime("%Y-%m-%d %H:%M:%S"), "tool": name, "args": args, "result": result[:200]}) + "\n") def status(): print(f"[{agent.provider.name} | {agent.provider.model} | {agent.mode} mode | " f"~{agent.convo.tokens():,}/{agent.convo.budget:,} tokens]") def handle(task): for kind, text in agent.run(task): prefix = {"tool": " > ", "thought": " - ", "info": " i ", "error": " x "}.get(kind, "\n") print(prefix + text) if opts.task: handle(" ".join(opts.task)) return status() print(f"workspace: {agent.workspace.root} (/help for commands, 'exit' to quit)") while True: try: line = input("\n> ").strip() except (EOFError, KeyboardInterrupt): break if line in ("exit", "quit"): break if not line: continue if not line.startswith("/"): handle(line) continue command, _, value = line.partition(" ") if command == "/mode" and value in ("plan", "ask", "auto"): agent.mode = value elif command == "/provider" and value in PROVIDERS: agent.provider = PROVIDERS[value] elif command == "/model" and value: agent.provider.model = value elif command == "/compact": print(" i", agent.convo.summarize_old_tasks(agent.provider, force=True) or "nothing to compact") elif command != "/status": print(" /mode plan|ask|auto /provider " + "|".join(PROVIDERS) + " /model NAME /compact /status") status() if __name__ == "__main__": main()How Claude Code Works (Let’s Build a Tiny One) was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.