Lab 2 — Build Your Own Agent Harness
5 minute read
Goal
Using Claude Code every day teaches you what an agent does. This lab teaches you why, by building the same machine yourself. Six ideas carry the whole field:
- Tool use: how a model acts on files, terminals and other systems.
- The agentic loop: reason, act, observe, repeat.
- Context management: keeping the window useful as it fills.
- Subagents: delegating independent work to a fresh context.
- Model selection: trading capability against speed and cost.
- The harness: the runtime, permissions and controls around all of it.
They are not six separate topics. They are six views of one loop, and each one exists because of a problem the previous one creates:
flowchart LR
S1[1 Stateless call] --> S2[2 One tool] --> S3[3 The loop] --> S4[4 Context management]
S4 --> S5[5 Subagents] --> S6[6 A model per role] --> S7[7 Harness controls]
S7 --> S8[8 MCP and the Agent SDK]You build one Python file, agent.py, in that order. It ends at about 200 lines. After every
step you look at the same mechanism inside Claude Code, using its own traces, so the tool you
use daily becomes something you can read instead of a black box.
The finished code, with offline tests, is in labs/agent-harness/ in this vault’s
repository. Read it after you have written your own version, not before.
Prerequisites:
- Python 3.11+ and an Anthropic API key.
- Claude Code installed.
- In a virtual environment:
pip install anthropic "mcp>=2.3" claude-agent-sdk pytest. Keep it activated, so the agent’s shell findspytesttoo. - The concepts from the Agents thread. This lab links the theory instead of repeating it.
Pace: one step per week is a good rhythm, which makes the whole lab two months of evenings. The point is not to finish. The point is to break each step on purpose before you move on.
How each step works
Each step is one page with the same shape:
- The question at the top. You should be able to answer it by the end of the page.
- Build: only what is new in
agent.py. - Check: what you should see when it works.
- See it in Claude Code: where the same mechanism shows up in the tool you already use.
- Break it: one deliberate failure. Most of the learning happens here.
- Answer: try your own answer before you read it.
The project
The agent works on a tiny project with two files and two bugs:
# sandbox/calc.py
def average(xs):
return sum(xs) / len(xs) - 1
# sandbox/test_calc.py
from calc import average
def test_average():
assert average([2, 4]) == 3
def test_empty_list_is_zero():
assert average([]) == 0
The eight steps
| Step | You build | You see in Claude Code |
|---|---|---|
| 1 — A stateless call | A chat loop over a message list you keep | Session transcripts in ~/.claude/projects/ |
| 2 — One tool | One tool_use → tool_result round trip by hand | /context and the cost of tool definitions |
| 3 — The loop | Four tools, parallel calls, errors as results, max_turns | A PostToolUse hook log; stream-json output |
| 4 — Context management | Clipping, just-in-time reads, AGENT.md, caching, compaction | /context, /compact, CLAUDE.md, tool-results/ |
| 5 — Subagents | delegate(): a fresh run() with fewer tools | .claude/agents/*.md and subagents/ transcripts |
| 6 — A model per role | Traces and a benchmark across models and effort | /model and model: in subagent files |
| 7 — Harness controls | Permission rules, hooks, saved sessions | allow / ask / deny, permission modes, --resume |
| 8 — Plug-ins, and the real thing | An MCP server, then the agent on the Agent SDK | claude mcp add, /mcp |
Debug any agent with four questions
From now on, when Claude Code or your own agent goes wrong, open the trace, find the first turn where it went off course, and ask in order:
- Missing context? It didn’t know something it needed, or the thing it needed was buried. Go back to step 4.
- Bad tool? The description misled it, the output was too big, or an error was swallowed. Go back to steps 2 and 3.
- Weak model? It had the facts and the tools and still reasoned badly. Go back to step 6.
- Missing harness constraint? It did something it should never have been able to do. Go back to step 7.
Most failures that look like “the model is dumb” turn out to be the first two.
Where this setup tops out
- This is Level 1, one agent working for one person. Running many agents on a real codebase, with parallel work in isolated git worktrees, orchestration and review, is Lab 4.
- Nothing here checks that the output is good. Passing tests are one signal. Evaluating what an agent produces is Lab 3.
- Regex permissions are not a sandbox. For anything you didn’t write yourself, run the agent in a container with no credentials.
- In production you would use the platform’s server-side compaction and context editing rather
than
compact(). Buildingcompact()by hand is how you learn what those do.
Sources
- Anthropic — Building effective agents
- Anthropic — Effective context engineering for AI agents
- Anthropic — Writing effective tools for AI agents, using AI agents
- Anthropic — How we built our multi-agent research system
- Anthropic — Building agents with the Claude Agent SDK
- Anthropic — Effective harnesses for long-running agents
- Thorsten Ball, How to Build an Agent (Amp) · mini-swe-agent
- Liu et al., Lost in the Middle: How Language Models Use Long Contexts (2023) — arXiv:2307.03172
- Chroma — Context Rot · Cognition — Don’t Build Multi-Agents
- Claude API docs: Tool use · Prompt caching · Compaction
- Claude Code docs: Sessions · The .claude directory · Hooks · Subagents · Permissions · Headless mode · MCP · Agent SDK for Python
- MCP Python SDK