Context Isolation Through Subagents: Read the Logs Elsewhere, Keep One Line
Three log files hold the cause of a failed checkout. Read inline, the main context holds 6,828 characters; delegated to subagents with a one-line brief, it holds 283, with the same answer.
What you will learn
- Explain why every file read inline stays in the main context for every later step
- Delegate each file to a subagent call with a brief that asks for one short line
- Spot a leaky brief that hands the whole file back
- Name the three costs of isolation and the workflow alternative
Before you start
- The Claude Messages API: your first call
- Context engineering: what belongs in the context window
A checkout failed last night, and the main agent has to find out why. Three long log files hold the answer. Read them all in one place and they bury the question. In this article a subagent does the reading in its own context and brings back one line.
Three files, 6,828 characters of context
When the main agent reads everything itself, the question goes in first, a thin sliver. Then checkout.log, about 2,300 characters. Then gateway.log, and it stays there. Then payments.log. The context now holds 6,828 characters, and the one useful line is buried somewhere in it.
The key point: every file you read stays in the main context for every step after it.
A manager who sends an intern
A manager has a question about three filing cabinets. Instead of reading them, the manager sends an intern with the question. The intern reads all three cabinets, 144 lines, in the intern's own room. The manager's desk stays clear. Back comes a one-page note: payments timed out. The reading stays in the intern's room.
brief.txt: the reply is the product
A subagent starts with nothing. It doesn't see the main chat, so the brief is its whole set of instructions:
You read one log file for a failed checkout.
Reply in one line: how many ERROR lines, and the first one with its line number.
Do not copy any other line of the file.
A role, one job, the exact shape of the reply, and a rule against copying the file back. Asking for the line number matters: it lets the main agent look closer later if it needs to.
desk.py: ask and delegate
The project uses the Anthropic SDK with a scripted stand-in on a mock transport, so no key is needed. The stand-in reads only what's in the one request it receives, which is exactly the isolation we want to show. The parts that matter:
QUESTION = "Why did checkout fail last night?"
MAIN = "You find the cause of a failed checkout from the notes you are given."
def ask(system, content):
"""One call. The model sees only this system prompt and this content."""
reply = client.messages.create(
model="claude-sonnet-5-5", max_tokens=1000, system=system,
messages=[{"role": "user", "content": content}])
return reply.content[0].text
def inline():
"""The main context reads every file itself."""
context = "\n\n".join([QUESTION] + [f.read_text() for f in LOGS])
return ask(MAIN, context), len(context)
def delegate(brief_file):
"""One subagent per file. Only its reply reaches the main context."""
brief = (HERE / brief_file).read_text()
notes = [QUESTION]
for log in LOGS:
text = log.read_text()
reply = ask(brief, text) # its own, fresh context
print(f" {log.name:13}read {len(text):,} chars, returned {len(reply):,}")
print(f" {reply.splitlines()[0][:78]}")
notes.append(f"{log.name}: {reply}")
context = "\n\n".join(notes)
return ask(MAIN, context), len(context)
A subagent here is simply a separate call with its own system prompt (the brief) and its own content (one file). The whole file goes to the subagent and never into notes. Only the reply is appended.
Same question, two ways
inline: main context 6,828 chars -> Cause: card-vault timeouts in payments.
delegated with brief.txt:
checkout.log read 2,290 chars, returned 76
3 ERROR lines, first at line 10: checkout order 4411 payment failed: timeout
gateway.log read 2,233 chars, returned 67
2 ERROR lines, first at line 13: gateway 502 from payments, retry 1
payments.log read 2,266 chars, returned 60
5 ERROR lines, first at line 11: payments card-vault timeout
delegated: main context 283 chars -> Cause: card-vault timeouts in payments.
Same answer both times: card-vault timeouts in payments. The main context went from 6,828 characters to 283, about 24 times smaller. But notice: the subagents still read every character. Isolation moves the reading out of the main context. It doesn't make the reading free.
Break it: a subagent that returns everything
Here's the leaky brief:
You read one log file for a failed checkout.
Reply with the whole file you read, so nothing is lost.
delegated with brief_leaky.txt:
checkout.log read 2,290 chars, returned 2,290
02:05:00 INFO checkout cart loaded id=1000
gateway.log read 2,233 chars, returned 2,233
02:05:00 INFO gateway GET /cart 200 id=1000
payments.log read 2,266 chars, returned 2,266
02:05:00 INFO payments token received id=1000
leaky brief: main context 6,869 chars -> Cause: card-vault timeouts in payments.
main context: 6,828 inline, 283 delegated, 6,869 leaky
Each subagent hands back its whole file, and the main context ends at 6,869 characters, slightly bigger than reading inline. A subagent that returns everything gives the main context nothing back. The brief is what makes isolation work.
Isolation has three costs
- A summary can lose detail. Ask for the file and line number, as our brief does, so the main agent can look closer.
- The subagent starts with nothing. Put everything it needs in the brief; it inherits no chat history.
- Replies still add up. Many subagents with long replies fill the main context again.
A workflow isolates too
There's a second route, with no subagent at all: a multi-step workflow. Step one extracts the error lines from one file. Step two decides the likely cause from step one's output only. Step three writes a note for the on-call team from step two's output. Step three never sees the raw log. Each step's context holds one short piece. Both routes do the same job: something reads elsewhere, and only a short result comes back.
When the question is which fix a long session needs (prune, compact, isolate or start fresh), see long-running sessions.
Recap
| Setup | Main context | Answer |
|---|---|---|
| Read inline | 6,828 chars | card-vault timeouts |
| Subagents, one-line brief | 283 chars | card-vault timeouts |
| Subagents, leaky brief | 6,869 chars | card-vault timeouts |
When big tool or file output bloats the context, pick isolation: a subagent or a separate step that returns a short result. A bigger model or a longer window doesn't fix it.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
Claude Certified Developer - Foundations (CCDV-F) Exam Prep
Build and ship Claude apps: the API, agents, tools and MCP, prompts, evals, security and model choice.View on Udemy ↗: Claude Certified Developer - Foundations (CCDV-F) Exam Prep (paid course, opens in a new tab)Related posts
The Claude Messages API: Your First Call, Field by Field
One request in, one Message out. The four fields of a Messages API call (model, max_tokens, system, messages), how to read content blocks, stop_reason and usage, and why every turn resends the history.
Claude Tool Use: How tool_use and tool_result Work in the Messages API
Follow one order question through Claude's tool round trip: define a tool, read the tool_use block, run your own function and send a matching tool_result back, including parallel calls and is_error.
Claude Client SDKs Wrap One REST API: Raw HTTP, Python, TypeScript and Async Calls Compared
Every Claude client SDK wraps the same REST request: a POST to /v1/messages with a JSON body. See it built by hand, through the Python and TypeScript SDKs, as typed errors, and as async calls that wait together.
Anthropic SDK Timeouts, Retries and Error Classes: Configure the Client Once, Then Give 429, 529, 400 and Timeouts Their Own Move
Configure the Anthropic SDK client (timeout, max_retries, with_options, async, pagination), see which errors it retries by itself, then repair a retry wrapper so a 429, a 529, a 400 and a timeout each get the move the docs support.