Sub topic 4
Context isolation: keeping the parent's context clean
This chapter assumes you know what a context window is, how an agent loop calls tools, and what a subagent is at a basic level. You should also be comfortable reading short Python snippets.
What context isolation means
A subagent starts with its own context window. It does not see the parent agent's conversation, the files the parent has read, or the tool output the parent has collected. It sees only what the parent puts in the brief, plus whatever the subagent reads or runs for itself.
The same boundary works in the other direction. The subagent may read forty files, run a test suite three times, and try two dead-end approaches. None of that enters the parent's context. The parent receives one final message, which is the subagent's summarized return.
This is the main reason to use subagents. A parent that does everything itself accumulates every file read and every log line. A parent that delegates keeps only the briefs it wrote and the summaries it received.
What crosses the boundary
Only two things cross it.
- Inbound: the brief. This is everything the subagent knows at the start.
- Outbound: the final reply. Intermediate steps stay behind.
Side effects are a third channel, and they are easy to overlook. If a subagent edits a file on disk, that edit is visible to everyone who shares the workspace. Context isolation separates what agents know. It does not separate what agents change. Keeping changes apart is the job of workspace isolation, covered in a later chapter.
| Item | Visible to the subagent? | Visible to the parent? |
|---|---|---|
| Parent's conversation history | No | Yes |
| Contents of the brief | Yes | Yes (parent wrote it) |
| Files the subagent read | Yes | No, unless the summary mentions them |
| Commands the subagent ran | Yes | No, unless the summary mentions them |
| Final reply | Yes (wrote it) | Yes |
| Edits on a shared disk | Yes | Yes, if the parent looks |
A worked comparison
Suppose the parent is asked to find why a CLI flag is ignored. Investigating requires reading src/cli.py, src/config.py, a 600-line tests/test_cli.py, and the output of a failing test run.
If the parent investigates alone, all of that text stays in its context for the rest of the session. Every later step, such as editing, writing docs, and answering follow-up questions, pays for it in tokens and in dilution of attention.
If the parent delegates, the exchange looks like this:
Parent -> Subagent:
Goal: Find out why --output is ignored when --verbose is also passed.
Context: The CLI is in src/cli.py. Config merging is in src/config.py.
Scope: Read-only. Do not edit any files.
Done when: You can name the file, function, and line where --output is dropped.
Return: Two or three sentences with the location and cause. No code dumps.
Subagent -> Parent:
In src/config.py, merge_options() (line 41) copies verbose settings over the
whole options dict, which overwrites output with its default. Confirmed by
running pytest tests/test_cli.py::test_output_with_verbose, which fails.The parent now holds about 100 words instead of thousands. The investigation cost was real, but it was paid in the subagent's context, which is discarded afterward.
Why a clean parent context matters
Context isolation helps in four specific ways.
- Room to keep working. The window is finite. Offloading bulky exploration leaves space for planning and review.
- Focus. A model attends to everything in its window. Old logs and irrelevant files can distract it from the current goal.
- Fresh starts. A subagent that is stuck can be discarded. The parent loses nothing, because the failed attempt never entered its context.
- Independent review. A subagent that did not write the code has no memory of the reasoning behind it, so it can check the result without sharing the author's assumptions.
Costs and failure modes
Isolation is not free. Each of these costs follows directly from the boundary.
The subagent lacks context you forgot to give it
The subagent cannot ask "what did we decide earlier?" if the answer is only in the parent's history. Missing context shows up as wrong assumptions, repeated questions, or work that contradicts a decision the user already made. The fix is in the brief: state decisions and constraints explicitly instead of assuming they carry over.
The summary loses information
A summary is lossy compression. If the subagent found three possible causes but reported one, the parent never learns about the other two. Ask for what you need, for example "list anything you were unsure about" or "name the files you read."
The summary can be wrong
The parent sees a claim, not the evidence. A subagent can report "tests pass" without having run them. Ask the subagent to include the command it ran and its result, and verify anything important yourself. A later chapter covers verification in detail.
Duplicated work
Two subagents that both need the same background will each read it. That uses more total tokens than a single agent reading it once. Isolation trades total cost for a cleaner parent context.
Too little detail returned, or too much
If you ask for no limit, a subagent may paste whole files into its reply, which undoes the benefit. State a format and a length in the brief.
Controlling what comes back
The reply format is part of the design. A reply should contain what the parent needs to take its next decision and nothing more.
For an investigation, ask for a location and a cause. For an implementation, ask for the files changed, the check command, and its result. For a review, ask for a ranked list of problems with file and line references.
The orchestrator can also enforce the shape in code. In this sketch, the parent accepts only a small structured result, and truncates anything oversized so one chatty subagent cannot flood the parent's context:
from dataclasses import dataclass
MAX_SUMMARY_CHARS = 1200
@dataclass
class SubagentResult:
name: str
status: str
summary: str
def accept_result(name: str, raw_reply: str) -> SubagentResult:
"""Keep only a bounded summary of a subagent's reply."""
text = raw_reply.strip()
if len(text) > MAX_SUMMARY_CHARS:
text = text[:MAX_SUMMARY_CHARS] + " [truncated]"
return SubagentResult(name=name, status="ok", summary=text)
if __name__ == "__main__":
reply = "Fixed merge_options in src/config.py. Ran pytest tests/test_cli.py: 12 passed."
print(accept_result("fix-merge", reply))Truncation is a safety net, not a substitute for a clear brief. If the important part is at the end of a long reply, cutting it off loses exactly what you wanted. Prefer asking for a short reply in the first place.
Common mistakes
- Writing the brief as a reference to history. "Do what we discussed" means nothing to a subagent. Restate the task.
- Passing the whole conversation. Copying the parent's full history into the brief defeats isolation and brings back the bloat you wanted to avoid. Pass only what the task needs.
- Assuming edits are isolated. Two subagents editing the same checkout can overwrite each other even though their contexts are separate.
- Delegating tiny tasks. If the work is a single short file read, the cost of writing a brief and reading a summary exceeds the context it saves.
Deciding what to delegate for context reasons
A useful rule is to delegate work whose process is large but whose result is small. Searching a codebase, reading logs, auditing dependencies, and running long test suites all fit. Work whose result is large, or whose reasoning you must follow closely step by step, fits poorly. A final answer for the user, or a delicate design decision, usually belongs in the parent.
Exercise
A parent agent asks a subagent to investigate a failing test. The subagent reads eight files, runs the test suite twice, and replies with a 40-line paste of the most relevant file plus the sentence "I think the bug is in the parser." Name two problems with this reply from a context isolation point of view, and say how you would change the brief.
Show answer
First, the pasted file brings bulk back into the parent's context, which undoes the benefit of delegating. Second, "I think the bug is in the parser" gives no location, no evidence, and no command the parent can use to check the claim. Change the brief's Return section to ask for the file, function, and line of the cause, the command that reproduces the failure and its result, and a short statement of anything uncertain, with an instruction not to paste full file contents.
Exercise
Two subagents run in the same checkout. One edits docs and one edits tests. Their contexts are isolated. Can they still interfere with each other? Why?
Show answer
Yes. Context isolation limits what each agent knows, not what it changes. Both write to the same files on disk, so one agent's edits, formatting runs, or failed test fixtures can affect the other. Separate branches or git worktrees isolate the workspace.