Sub-agent Context Isolation
A sub-agent explores in a clean context and returns only a summary.
Problem
Some steps generate huge amounts of intermediate material, like reading 30 files to find one function, browsing a dozen pages for one fact, or trying several failed approaches. If the main agent does this itself, its context fills with noise that it carries for the rest of the task, crowding out what matters.
- Exploration produces far more text than the answer it finds.
- The main agent needs to stay focused on the overall task.
- A sub-agent starting fresh lacks context the main agent has.
- Every sub-agent adds startup cost and a summary that may miss details.
Solution
When a step is exploratory and self-contained, the main agent spawns a sub-agent with a fresh context and a precise brief: what to find, where to look, and what to return. The sub-agent does all the reading and trial-and-error in its own context. It returns only a short, structured result, such as the answer, key evidence and file or source references. Its working context is then discarded. The main agent’s context grows by a paragraph instead of thousands of lines.
Diagram
The sub-agent's context is thrown away when it finishes. Only the short result crosses back into the main agent's context.
When to use
Use it when
- A step needs heavy reading or searching to produce a small answer
- Exploration involves trial and error the main agent doesn't need to see
- The main task is long and its context is precious
- The step can be described clearly without the main agent's full history
Avoid it when
- The step needs the main agent's full context to be done right
- The exploration is small enough to just do inline
- Many tightly linked steps would each need their own sub-agent
Trade-offs
Gains
- The main agent's context stays small and focused
- Exploratory dead ends don't pollute the main task
- Exploration can run in parallel across several sub-agents
Costs
- Sub-agents may miss context the main agent had
- Summaries can drop a detail the main agent later needs
- More moving parts to trace when debugging
Worked example
A coding agent is fixing a bug where invoices show the wrong currency for some EU customers.
- Main agentNeeds to know where currency is chosen. It spawns a sub-agent with the brief: “find where invoice currency is set; return file, function and the rule used; under 150 words.”
- Sub-agentSearches the codebase, reads 23 files and 2 config files, and follows one dead end in a deprecated module. That’s about 60,000 tokens of reading.
- Sub-agent resultReturns 6 lines: currency is set in
billing/pricing.py,resolve_currency(), from the billing country, with a fallback to the company’s home country when the country code is missing. EU customers created before 2024 have no country code. - Main agentIts context grew by about 150 tokens. It writes the fix and a data backfill using the precise location it was given.
- ComparisonDoing the search inline would have added about 60,000 tokens to the main context for the rest of the task.
Failure modes
| Failure | Symptom | Mitigation |
|---|---|---|
| Under-specified brief | Sub-agent explores the wrong thing or returns the wrong shape | Brief states goal, scope, return format and length limit |
| Thin summary | Main agent has to redo the exploration for a missing detail | Return references (files, links) so details can be fetched directly |
| Over-delegation | Sub-agents spawned for trivial lookups | Only isolate steps that would add substantial context |
| Hidden assumptions | Sub-agent misses a constraint the main agent knew | Pass relevant constraints explicitly in the brief |