Sub topic 4
Context isolation: keeping the parent's context clean
This chapter assumes you have used a coding agent, know that a model has a limited context window, and can read basic Python. It builds on the previous chapter's definition of a subagent. Here the focus is what isolation does and where it goes wrong.
What isolation means
A subagent starts with a fresh context. It does not see the parent's conversation, earlier tool output, or files the parent read. It receives one thing, the brief, and it returns one thing, a summary.
The parent's context therefore holds only the brief it sent and the summary it got back. The subagent's file reads, failed attempts, and command output stay in the subagent's own context and are discarded when it finishes.
Why it helps
Suppose the parent needs to know why a test fails. Done in the parent, the agent might read 12 files and run the test suite 5 times. Each of those results stays in the context for the rest of the session. Done by a subagent, the parent keeps one paragraph.
| Approach | What the parent's context keeps | Effect on later turns |
|---|---|---|
| Parent does the work itself | Every file read and command output | Less room for new work, more noise to reason over |
| Subagent does the work | The brief and a short summary | More room, and the parent reasons over conclusions |
Isolation also contains mistakes. If a subagent follows a wrong path, the wrong path never enters the parent's reasoning.
The cost: nothing is shared
Isolation works in both directions. The subagent cannot see what the parent knows, so anything it needs must be in the brief. The parent cannot see what the subagent did, so it must rely on the summary.
The orchestrator in the earlier code example shows this. run_subagent takes a Task and nothing else:
from dataclasses import dataclass
@dataclass
class Task:
name: str
brief: str
def build_prompt(task: Task) -> str:
# The subagent sees only this string, never the parent's history.
return task.brief
print(build_prompt(Task("add-tests", "Add unit tests for parse_args in tests/test_cli.py.")))The common errors follow from this:
- Missing context. A brief such as "fix the bug we discussed" refers to a conversation the subagent never saw. State the file, the symptom, and the expected behavior.
- Bloated returns. If the subagent pastes full file contents back, the parent's context fills up again. Ask for a short summary in the brief.
- Unverifiable summaries. A summary says what the subagent claims it did. Request the command it ran and its result, so the parent can check.
Deciding what crosses the boundary
A useful rule is to send what the subagent needs to start and ask back for what the parent needs to decide.
- Send: the goal, relevant file paths, constraints, and the finish condition.
- Return: the outcome, the evidence (a command and its result), and pointers such as file paths or line numbers.
If the parent later needs detail, it can open the specific file itself or start another subagent. Pointers cost a few tokens, while full contents cost thousands.
Exercise
A parent agent asks a subagent to "investigate the flaky test" and gets back 400 lines of log output. Name two problems with this exchange and how to fix each.
Show answer
First, the brief refers to a test without naming it, the file, or the symptom, so the subagent has to guess. Fix it by stating the test name, file path, and observed failure. Second, the long return defeats context isolation by filling the parent's context with log output. Fix it by asking for a short summary: the likely cause, the command used to reproduce it, and file paths or line numbers for details.