Sub topic 12
Using subagents to isolate noisy work
This chapter assumes you have used a coding agent in a terminal, know what a context window is, and have read the earlier chapters on what fills the context and how to keep tool output small. Here you will learn when to hand a task to a subagent, how to write a delegation that works, and where subagents fail.
What a subagent is
A subagent is a separate agent session that your main agent starts to do one job. It has its own context window. It starts nearly empty, reads what it needs, does the work, and returns a final message. The main agent receives that message and nothing else.
That last point is the whole benefit. The subagent may read 40 files, run a noisy search, and try three dead ends. None of that enters the main session. Only the summary does.
| Property | Main session | Subagent |
|---|---|---|
| Context at start | Instructions plus the whole conversation so far | Instructions plus the task prompt you give it |
| What it reads | Whatever you ask for | Whatever it needs for its job |
| What flows back | Everything stays | Only the final message |
| Lifetime | Hours | One task |
Why isolation helps
Earlier chapters showed that reads and tool output are the main consumers of context. Targeted reading and truncation reduce that cost. Subagents remove it from the main session entirely, for work where you only need the conclusion.
Consider the question "Where is the retry logic for HTTP calls, and does anything bypass it?" Answering it may take a dozen searches and several file reads. The main session needs a short answer, such as a list of file paths and line numbers. The exploration is noise once the answer exists.
Good candidates for delegation
A task suits a subagent when its input is large, its output is small, and you do not need the intermediate steps.
- Codebase exploration. "Find every caller of
charge_cardand list the file, line, and whether it handlesPaymentError." - Log and test triage. "Run the integration suite and report which tests fail and the first error line of each."
- Research across many files. "Summarize how configuration is loaded, in under 200 words, with file references."
- Independent review. "Read the diff on this branch and list possible bugs." A fresh context has no attachment to the code it just wrote, so it can review more critically.
- Parallel independent work. Several subagents can search different parts of a repository at the same time, if your tool supports it.
Poor candidates for delegation
Some work does not isolate well.
- Tightly coupled edits. If step 2 depends on decisions made in step 1, the subagent lacks that context. You would have to restate it, and the restatement costs about as much as doing the work yourself.
- Small tasks. Starting a subagent has overhead: you write a prompt, it re-reads files, and you read the reply. Reading one 40-line function directly is cheaper.
- Work where you need the details. If you will want to see the actual code, a summary forces a second read, now in your main session.
- Interactive debugging. Debugging needs a loop of hypothesis, command, and observation that you steer. A subagent that works alone may drift.
Writing a delegation prompt
A subagent knows only what you tell it. It cannot see your conversation. Treat the prompt as a handoff note for a capable colleague who has just joined.
A useful delegation states four things:
- The goal and why it matters. One or two sentences.
- Constraints and known facts. Paths to check, things already ruled out, the language and test command.
- Scope. Read-only or allowed to edit, and which directories.
- The output format and size limit. This is the most often missed. Without it, the subagent may return a long report that defeats the purpose.
Here is a prompt that includes all four:
Goal: I am replacing the `requests` library with `httpx` and need to know every place
that sets a timeout, so I can keep behavior the same.
Context: The code is in src/ and tests/. Ignore vendor/ and docs/. The test command is
`pytest -x -q`.
Scope: Read-only. Do not edit any files.
Output: A markdown table with columns file, line, timeout value, and whether the value
comes from config or is hard-coded. Maximum 25 rows. If there are more, give the 25 most
important and state the total count. No other commentary.Compare a weak version: "Look into timeouts." The subagent has to guess the purpose, the scope, and the format. It may return two pages of prose.
Handling the returned summary
Treat the summary as a claim, not as proof. The subagent compressed its work, and compression can lose or distort facts.
- Ask for evidence. Request file paths and line numbers so you can spot-check. A claim like "nothing bypasses the retry wrapper" is much weaker than a list of the 14 call sites it checked.
- Verify before acting on it. Before you edit based on a summary, open the one or two files that matter most. Reading 30 lines costs little.
- Ask for uncertainty. Add "say which parts you did not check" to the prompt. Otherwise a subagent may present a partial search as complete.
- Record the result if it matters later. If the finding will be needed after a compaction or reset, write it to your notes file. The subagent's message lives only in the main conversation.
Failure modes
Subagents introduce their own problems.
Lost context
The subagent does not know decisions you made earlier. It may suggest an approach you already rejected, or violate a convention that you explained in conversation. Put those facts in the prompt, or keep them in the persistent project instructions, which most tools load into each session.
Over-long or vague summaries
If you do not cap the output, the subagent returns what it found interesting. A 3,000-word summary can cost more context than the direct reads it replaced. Always set a limit.
Confident but wrong answers
A subagent that searches by text can miss dynamic calls, generated code, or renamed imports. Its summary will not mention what it could not see unless you ask. Request a list of the search methods used, so you can judge the gaps.
Conflicting edits
If you allow two subagents to edit, they may change the same file. Use read-only subagents for exploration. If you allow edits, give each one a separate set of files, and review the combined diff with git diff before committing.
Cost and latency
Each subagent consumes tokens in its own window, and a task that re-reads the same files can be paid for twice, once there and once in your session. Isolation saves your main context, not total tokens. The saving is worth it when the main session is large or long-lived, because every later turn in that session pays for the context it carries.
Deciding whether to delegate
Ask these questions in order.
- Will the work read or print a lot, such as many files, logs, or search hits?
- Do I need only a conclusion, not the intermediate content?
- Can I describe the task fully in a short prompt?
- Can I check the result cheaply?
If all four answers are yes, delegate. If the first is no, do the work directly. If the third is no, the task is coupled to your session, so do it yourself or split it first.
Worked example
Suppose your main session is at 55% of its window. You are changing a function's signature and want to know how many callers break. You do not want 30 search hits and several file bodies in your context.
You delegate with a prompt like this:
Goal: I am adding a required `currency` argument to `charge_card(amount, user)` in
src/billing/charge.py. List every caller that would need to change.
Scope: Read-only. Search src/ and tests/.
Output: One line per call site in the form `path:line - passes currency? (yes/no)`.
Maximum 30 lines, then the total count. End with one sentence naming any call you could
not resolve statically, such as calls through getattr or a wrapper.The subagent runs its searches, reads the surrounding lines of each match, and returns:
src/api/orders.py:88 - no
src/api/refunds.py:41 - no
src/jobs/nightly.py:17 - no
tests/test_billing.py:12 - no
tests/test_billing.py:30 - no
Total: 5 call sites.
Unresolved: src/jobs/registry.py builds handler names from strings and may call charge_card indirectly.That costs your main session about 100 tokens. You then open src/jobs/registry.py yourself to check the unresolved case, and write the list into your PROGRESS.md under "Next", so it survives a compaction.
Subagents and the rest of the routine
Subagents complement the other techniques. They do not replace them.
- They reduce growth, so you compact less often.
- They do not fix a main session that is already crowded. Compaction or a fresh session still applies.
- They work best when your task is well scoped, because a clear scope makes a clear delegation prompt.
Exercise
You are 70% through your context window. You need to know which of 200 test files use a deprecated fixture named legacy_db, and you will then rewrite about five of them. Which parts should you delegate, and what should the delegation prompt specify?
Show answer
Delegate the search. It reads and prints a lot, and you only need the list of files. Do the rewrite of the five files in the main session, because editing needs the code in your context and the changes may depend on decisions you made earlier. The prompt should state the goal, that the work is read-only, the directory to search (tests/), and the output format and limit, for example one line per file with the line numbers of each use, a maximum of 40 lines, and the total count. It should also ask the subagent to note any use it could not detect, such as the fixture name built from a string.
Exercise
A subagent returns a 2,500-word report when you only needed to know whether a function is called anywhere. Name two changes to your prompt that would have prevented this.
Show answer
State the output format and a size limit, for example "answer yes or no, then list at most 10 call sites as path:line". Also state the purpose of the task so the subagent can leave out material that does not serve it. Asking it to omit other commentary also helps.