Agentic Pattern Libraryby Vikas Goyal
← All patterns
17

Sub-agent Context Isolation

Also known as: subagent delegation, context firewall, scratch context

A sub-agent explores in a clean context and returns only a summary.

TrackAgentic systems FamilyContext & memory Maturity Emerging ChangesContextCoordination
01

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.
02

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.

03

Diagram

Main agentoverall task task · decisions + 6-line result Sub-agent · fresh contextdiscarded after 23 files 2 configs dead end ~60,000 tokens of reading and trial and error Structured resultanswer · evidence · file references brief: goal · scopeformat · ≤150 words ~150 tokens back

The sub-agent's context is thrown away when it finishes. Only the short result crosses back into the main agent's context.

04

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
05

Trade-offs

Latency Sub-agent startup; can run in parallel
Cost Sub-agent tokens, but the main context stays small
Complexity A brief, a return format and a spawn tool
Reliability Main agent stays focused; risk of thin summaries

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
06

Worked example

Scenario
A coding agent is fixing a bug where invoices show the wrong currency for some EU customers.
  1. 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.”
  2. 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.
  3. 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.
  4. Main agentIts context grew by about 150 tokens. It writes the fix and a data backfill using the precise location it was given.
  5. ComparisonDoing the search inline would have added about 60,000 tokens to the main context for the rest of the task.
07

Failure modes

FailureSymptomMitigation
Under-specified briefSub-agent explores the wrong thing or returns the wrong shapeBrief states goal, scope, return format and length limit
Thin summaryMain agent has to redo the exploration for a missing detailReturn references (files, links) so details can be fetched directly
Over-delegationSub-agents spawned for trivial lookupsOnly isolate steps that would add substantial context
Hidden assumptionsSub-agent misses a constraint the main agent knewPass relevant constraints explicitly in the brief