Agent Harnesses Explained
What sits between a model and the world.
An interactive guide to the agent harness: the loop, the tools, permissions and context management around a model, animated by a tested simulator.
- turn 1505
- turn 2561
- turn 3689
- turn 4777
- turn 5871
- turn 6939
- System prompt
- Tool definitions
- Task
- Reply header
- Model's messages
- Tool results
How this is checkedAgent_Loop_Sim's Python reference and the TypeScript port in your browser produce the same events, token for token and float for float, and three recorded runs of a small open-weights model are replayed token for token. How it is tested →
What you'll learn
A chat model only writes text. The harness around it turns that into an agent: it runs the loop, describes the tools, parses the calls, decides which ones may run, keeps the context inside the window and the prompt cache warm. To fix one failing test, a scripted agent here makes 6 model calls and sends the model 4,342 tokens to write 197 tokens.
Each chapter takes one of the harness's choices apart, built around an animation. Every frame comes from Agent_Loop_Sim, a deterministic simulator with a real tokenizer, tested against its Python reference. Nothing here calls a live model: the scripted runs are reproducible, and three runs of a small open-weights model were recorded once and are replayed token for token, failures and all.
01
The agent loop
Model, tool call, result, model again: the loop a harness runs, and the context window filling as it goes.
02
Tool calling
JSON Schema in, a structured call out: what the model actually sees and writes, natively and as ReAct text, and what happens when a call is malformed.
03
The context window as a budget
System prompt, tools, history and tool results share one window; when it fills, truncation, clipping or summarising decide what is kept and what is lost.
04
Prompt caching
A stable prefix is read from the cache at a tenth of the price; a timestamp or a reordered tool list breaks it.
05
Permissions and the human in the loop
Allow, ask and deny rules evaluated against every call; each question to the human adds their thinking time to the run.
06
Sub-agents
A parent hands a sub-task to a child with a fresh context and gets a short report back: a smaller context for the parent, paid for in the child's tokens and in time.
07
Hooks and sandboxing
Hooks intercept every call before and after it runs; a sandbox bounds what a shell command can reach, whatever the rules allowed.
08
Failure and recovery
Tools fail: retries with back-off, errors fed back to the model, and loop detection, with the success rate against the retry budget from seeded runs.
09
Harnesses compared
How Claude Code, Codex CLI, Aider, OpenHands, SWE-agent and mini-SWE-agent make each choice, from their docs, and the same task under a policy imitation of each.
10
Cost and latency of a task
Tokens per turn, the cache, output speed and human waits: the full arithmetic of a task, with a live calculator driven by the engine.
Part of a family of sixteen companion sites in four tracks, all in the site menu above: Foundations (Decoder, Training), Hardware (Kernels, Systolic, Accelerators, Racks), Inference (Serving, Architectures, Numerics, Trade-offs) and Agents (Harnesses, Protocols, Context, Orchestration, Evals, Security). It is the first of the six agent sites. How this one was built, and how to check it: about.