Sub topic 12
Splitting work across subagents with separate contexts
This chapter assumes you have read the chapters on the parts of a context, tool results, and compaction. You should be comfortable with token budgets and with the idea that every call sees only what you put into it.
Why one large context stops working
A single agent that does everything accumulates context. Each file it reads, each search result, and each failed attempt stays in the window. After enough steps, the useful facts are buried under bulk. This is the distraction failure from the earlier chapter on how context goes wrong.
A subagent is a separate model call, or a loop of calls, with its own context window. A parent agent gives it a task. The subagent does the work in its own window and returns a short result. The parent never sees the raw material the subagent read. It sees only the answer.
The value is isolation. The subagent may read 40,000 tokens of logs to find one cause. The parent receives 150 tokens.
What crosses the boundary
Two things cross: the task going down and the result coming back. Everything else stays behind. This makes the handoff the most important design decision.
- The task description must be self-contained. The subagent has no access to the parent's history. If the parent knows the project uses Python 3.11 and the subagent needs that fact, the task must say so.
- The result should be in a fixed, compact format. Ask for findings, evidence such as file paths or line numbers, and open questions. Do not ask for a transcript of the work.
Here is a minimal sketch of the pattern. The run_model function stands in for your provider's API call.
def run_subagent(task, sources, run_model):
# The subagent gets a fresh context: its own instructions, the task, and the sources.
prompt = (
"You are a research subagent. Answer the task using only the sources.\n"
"Reply in under 120 words. Cite the source name for each claim.\n\n"
"Task: " + task + "\n\n"
+ "\n\n".join(name + ":\n" + text for name, text in sources.items())
)
return run_model(prompt)
def run_parent(question, jobs, run_model):
# jobs maps a task string to the sources that task needs.
findings = [run_subagent(task, sources, run_model) for task, sources in jobs.items()]
prompt = (
"Question: " + question + "\n\n"
"Findings from subagents:\n- " + "\n- ".join(findings) + "\n\n"
"Write the final answer."
)
return run_model(prompt)The parent prompt grows with the number of findings, not with the size of the sources. That is the budget benefit.
When to use subagents
Subagents suit work that is read-heavy and can be summarized. Examples are searching a codebase, reviewing several documents, or investigating independent hypotheses. Independent tasks can also run in parallel.
They suit work poorly when steps depend tightly on each other or when the parent needs the detail. Writing code that must match many decisions made earlier is a bad fit, because the subagent cannot see those decisions.
| Situation | Better choice |
|---|---|
| Reading many files to answer one question | Subagent |
| Several independent lookups | Parallel subagents |
| Sequential edits that share decisions | Single agent |
| Facts needed again in later turns | External memory or notes |
Trade-offs and failure modes
- Lost detail. The summary is lossy. If the subagent omits a fact the parent needs, the parent cannot recover it. Ask for evidence references so the parent can request a follow-up.
- Cost. Each subagent pays for its own instructions and reads. Total tokens across all calls often exceed a single-agent run. You trade total cost for a cleaner parent context.
- Conflicting results. Two subagents may return statements that clash. The parent must decide, so ask for confidence and sources.
- Poisoned summaries. A wrong claim in a summary enters the parent as trusted text. Treat subagent output as data to check, not as instructions.
Exercise
A parent agent must find why a build fails. The logs total 60,000 tokens, and the parent window is 32,000 tokens. Describe how you would use a subagent, and name what the task and the result should contain.
Show answer
Send the logs to a subagent with its own window large enough to read them. The task should state the goal (find the cause of the build failure), relevant facts such as the build tool and version, and the required output format. The result should be short and contain the likely cause, the log lines or file paths that support it, and any open questions. The parent then works from that result and never loads the 60,000 tokens.
Exercise
Give two reasons a team might choose a single agent over subagents for a task.
Show answer
Possible answers: the steps depend on each other and share decisions that a subagent could not see, and the extra token cost of several separate contexts outweighs the benefit. Another valid reason is that the parent needs the raw detail, which a summary would lose.