Lesson 16 of 40Agents
Subagents and Orchestrators
Split big tasks across subagents with their own context, write a Claude Code subagent file, and know when spawning more agents costs more than it saves.
One agent with one conversation works well for small tasks. On big ones it starts to struggle: the context fills with file dumps, logs and dead ends, and the quality of its decisions drops. The fix that most serious agent systems use is to split the work. One agent coordinates, and other agents with their own fresh context do focused pieces and report back. This lesson explains that pattern, shows how Claude Code does it with subagents, and gives you a complete reviewer subagent you can drop into a project.
Why one huge context fails
Everything an agent reads stays in its context: every file, every command output, every failed attempt. Three things go wrong as it grows:
- Quality drops. Models pay less reliable attention to any one detail when the context is crowded with noise. This is often called context rot. The instruction you gave at the start gets buried under 40,000 tokens of test logs.
- Cost rises. The API is stateless, so the whole context is resent on every turn. A large context makes every later step more expensive.
- The window runs out. Even a very large context window has an end. Long sessions get compacted (summarised), and summaries lose detail.
Most of what fills the context is only needed for a moment. To find which of 30 files uses an old API, you have to read them, but once you know the answer ("3 files, here they are") the contents are noise. Ideally, the reading would happen somewhere else and only the answer would come back.
The orchestrator-worker pattern
That is exactly what the orchestrator-worker pattern does:
- The orchestrator (or lead agent) holds the goal and the plan. It breaks the task into pieces and hands each one out.
- Each worker (a subagent) starts with a fresh, empty context plus a short brief. It does the messy work, reading and searching and running things, then returns a summary.
- The orchestrator reads the summaries, decides what to do next, and puts the results together.
The orchestrator gets the goal and keeps a clean context for planning.
Two benefits come from this shape. Context isolation: the orchestrator stays focused because it only ever sees conclusions. Parallel fan-out: independent pieces (search the frontend, search the backend, check the docs) can run at the same time, so the whole task finishes sooner.
The simulation below compares the two approaches in tokens. The numbers are made up but realistic in size.
Press Run (⌘ Enter) to see what happens.Read the last line carefully. The orchestrator's context is tiny, but the total work went up, not down: each worker pays for its own brief, and the orchestrator still pays for the summaries. Subagents buy you focus and speed, not free tokens.
Subagents in Claude Code
Claude Code has this pattern built in. A subagent is a Markdown file with YAML frontmatter (settings between --- lines at the top) and a system prompt as the body. Put it in one of two places:
.claude/agents/<name>.mdin a project, committed so the whole team gets it.~/.claude/agents/<name>.mdin your home folder, for all your projects.
The /agents command creates and manages them interactively. Claude Code also ships built-in subagents, including Explore (read-only search), Plan, and general-purpose, so you get the benefit even before you write your own.
Here is a complete reviewer:
---
name: code-reviewer
description: Reviews code changes for bugs, security issues and duplication. Use after any code change, before committing.
tools: Read, Grep, Glob, Bash
model: sonnet
maxTurns: 20
---
You are a senior code reviewer. You did not write this code, and your job is
to find real problems in it, not to praise it.
1. Run `git diff` (and `git diff --staged`) to see what changed. Review only
the changed lines and the code they touch.
2. For each change, check:
- Correctness: wrong logic, off-by-one errors, unhandled empty or null input.
- Security: unvalidated input, secrets in code, injection, unsafe file paths.
- Duplication: a new helper that copies one that already exists. Use Grep
to search for similar function names before you report this.
- Tests: changed behaviour with no test that covers it.
3. Do not edit any files. Do not comment on formatting the linter handles.
Report in this format, most serious first:
- **file:line** [bug | security | duplication | missing test] one-sentence problem.
Suggested fix in one or two sentences.
If you find nothing real, say "No issues found" and list what you checked.
Keep the whole report under 300 words.What each field does:
| Field | Required | Purpose |
|---|---|---|
name | Yes | The id you and Claude refer to it by |
description | Yes | When to use it. Claude reads this to decide whether to delegate |
tools | No | Allowlist. This reviewer can read and run commands, but has no Edit or Write |
model | No | haiku, sonnet, opus, fable, or inherit from the main session |
maxTurns | No | A hard cap on its loop, a stop condition from the first lesson of this unit |
Other optional fields include permissionMode, skills (skills to give the subagent), and isolation: worktree (work in a separate git worktree, so parallel workers do not edit the same files).
Claude delegates automatically when your request matches a subagent's description, or you can ask explicitly: "Use the code-reviewer subagent on my changes." The body is written as a brief to a colleague: what to look at, what not to do, and exactly what shape the report should take. That last part matters most, because the report is all the main conversation will ever see.
- my-app/
- .claude/
- agents/
- code-reviewer.mdproject subagent, committed
- test-runner.mdruns tests, returns failures only
- src/
- CLAUDE.md
- ~/.claude/
- agents/
- explainer.mdpersonal, every project
Match each frontmatter field to what it controls.
Writer and reviewer
The single most useful pair of agents is a writer and a reviewer. An agent that just wrote code is primed to believe it is correct: the reasoning that produced it is sitting in its context. A reviewer with a fresh context reads only the diff and the plan, the way a colleague would. It catches things the writer talked itself past.
This is why the reviewer above has no Edit or Write tools. Its job is to report, not fix. The main session (or you) decides which findings to act on. The Build, Test, Review Loop turns this into a full build, review and test loop.
Cheaper models for workers
Not every worker needs the most capable model. Searching files, summarising logs or running tests and reporting failures are simple, high-volume jobs. Set model: haiku on those subagents and they run faster and far cheaper. Keep the stronger model for the orchestrator, which makes the decisions, and for reviewers, where a missed bug is expensive. The lesson on choosing a model covers the trade-off in depth.
The cost of over-spawning
Subagents are not free, and more is not better:
- Every subagent pays for its own setup. Its system prompt, brief and tool definitions are fresh tokens each time. Spawning five agents to answer a question one
grepcould answer wastes money and time. - Summaries lose information. The orchestrator only sees what the worker chose to report. If a worker summarises "tests pass" when one was skipped, nobody upstream knows.
- Parallel writers collide. Two workers editing the same file at once will overwrite each other. Split write work by file or use worktree isolation. Read-only work parallelises safely.
- Coordination is work too. Briefs must be clear and specific. A worker cannot ask the orchestrator a follow-up question halfway through.
A good rule: use a subagent when the work would flood your main context, can run independently, or benefits from fresh eyes. Do small, quick lookups yourself.
None picked yet
Put an orchestrated task in order.
- Tap the steps below in the right order.
- subagent
- An agent started by another agent, with its own context window, that returns a summary when done.
- orchestrator
- The lead agent that plans, hands out pieces of work, and combines the results.
- context isolation
- Keeping a worker's large reads and logs out of the main conversation by running them in a separate context.
- fan-out
- Starting several independent workers at once so their pieces run in parallel.
- context rot
- The drop in answer quality as a context fills up with noise and old material.
- writer/reviewer
- Separating the agent that writes code from a fresh-context agent that reviews it.