Sub topic 12
Using subagents to isolate noisy work
This chapter assumes you have used a coding agent in a terminal, know that the context window is limited, and have read the earlier chapters on what fills the context and on keeping tool output small.
Why subagents help
Some tasks produce a lot of output but only a little useful information. Examples are searching a large codebase for every caller of a function, reading a long log, or surveying ten files to learn how a module works. If the main agent does this work, all of the raw material stays in its context for the rest of the session.
A subagent is a separate agent session that the main agent starts for one job. It has its own context window. It does the noisy work there, then returns a short result. The main session receives only that result. The reading, the dead ends, and the raw output stay in the subagent's context and are discarded when it finishes.
Good candidates for delegation
Delegate work when the input is large and the answer is small.
| Task | Why it suits a subagent | Expected result |
|---|---|---|
| Find all uses of a deprecated API | Many search hits and file reads | List of file paths and line numbers |
| Diagnose a failing test from a long log | Thousands of log lines | Likely cause and the relevant lines |
| Summarize how a module works | Reads many files | One paragraph and a list of key functions |
| Review a large diff | Long text to read | List of concrete concerns |
Keep work in the main session when you need the details later. If you will edit a file, the main agent should read it directly, because a summary does not contain the exact text you need to change.
Writing a useful task
A subagent starts with no memory of your session. It knows only what you put in the task. A vague task gives a vague result, so state the question, the scope, and the output format.
Search the src/ directory for every call to load_settings().
For each call, report the file path, the line number, and whether the
caller passes a path argument. Do not edit any files.
Return a list of at most 30 entries and a one-line summary.
Do not paste file contents.The last two lines matter. Limiting the result size protects the main context, which is the reason you delegated in the first place.
Limits and failure modes
- Lost detail. A summary drops information. If the subagent misses something, the main agent will not know. Ask for file paths and line numbers so you can check the claims yourself.
- Startup cost. The subagent must find its own context, so a small task can cost more than doing it directly. Do small lookups in the main session.
- Conflicting edits. Two agents editing the same files can overwrite each other. Prefer read-only subagents, or give each one a separate set of files.
- Unverified claims. Treat the result as a report to check, not as a fact. Open the cited lines before you act on them.
Worked example
You are migrating a settings loader and need to know every caller. In the main session you write the task above. The subagent runs searches and opens a dozen files, which could add thousands of lines of output. It returns 14 entries, each with a path and a line number. The main session gains roughly 20 lines of context instead of the whole search history. You then open only the three callers that need changes.
Exercise
You need to learn why a 5,000-line integration test log shows a timeout, and then fix one line of code in config.py. Which part should you delegate to a subagent, and which part should the main agent do? Name one risk of the delegated part.
Show answer
Delegate reading the log and finding the cause, because the input is large and the answer is small. The main agent should read and edit config.py itself, because it needs the exact text of the file. A risk is lost or wrong detail in the summary, so ask for the relevant log lines and verify them before editing.