Lesson 49.5 · 5 of 5 · 5 min read · Reviewed 24 Nov 2026
Shared vs isolated context
By the end, you can
- Compare shared and isolated context between agents.
- Choose what each agent should see.
Two extremes
| Shared context | Isolated context | |
|---|---|---|
| What each agent sees | Everything so far | Its task and what it was given |
| Re-explaining needed | Little | Some: write good task briefs |
| Token cost | High, grows with team size | Lower |
| Focus | Distracted by others’ detail | Sharp |
| Leaks between roles | Easy | Contained |
Why isolation usually wins
Subagents (chapter 43) get their own context windowContext window: The maximum number of tokens a model can consider at once, counting the input and the output it generates.. A research worker can read 50 documents and return 10 lines. The supervisor never sees the 50 documents, so it stays focused and cheap.
The middle ground
supervisor context: goal, plan, worker summaries
worker context: its brief + its own tool results
shared store: files, notes or a database with full results and ids
Workers write detail to the store and return summaries plus references (paths, ids). Anyone who needs the detail fetches it.
Writing a good brief
Because isolated workers start fresh, their brief must carry what they need: objective, scope, output format, tools to use, and what not to do (43.3). Most multi-agentMulti-agent system: A design where several agents with different roles or contexts work together on a task. failures traceTrace: A record of everything that happened during one request or agent run, as a tree of timed steps. back to thin briefs.
Key points
- Shared context gives every agent the full history; isolated context gives each agent only what its job needs.
- Isolation keeps each context small and focused, and stops one agent's noise confusing another.
- Sharing avoids re-explaining but costs tokens and spreads irrelevant detail.
- A common middle ground: isolated workers, plus a shared store of results and references.
Compare shared and isolated context
Measure the token cost and the leak risk of shared context against isolated briefs with a shared store.
You will have: A context builder for both modes and a printed comparison of tokens and leaks.
Steps
- Run the starter: both modes send the full team history.
- Write the isolated mode: the agent's brief plus references to shared results.
- Compare tokens per call and for the whole run.
- Check whether the legal team's internal note can reach the copywriter in each mode.
Done when
- Isolated context uses far fewer tokens per call than shared.
- The legal note is visible in shared mode and not in isolated mode.
- The copywriter can still reach the survey and price by reference.
Open in the Playground No API key needed.
Starter code (Python)
# Shared context gives every agent everything; isolated context gives each agent only its brief.
TEAM_LOG = [
("legal", "INTERNAL: we may not claim 'certified' until the audit closes"),
("research", "Survey: 62% of users want offline mode"),
("pricing", "Launch price: 12 credits a month"),
("design", "Hero image uses the blue palette"),
] * 50 # a long shared history
STORE = {"research": "ref:store/survey", "pricing": "ref:store/price"} # shared results, by reference
BRIEFS = {"copywriter": "Write launch copy. Use the survey result and the launch price. 80 words."}
def tokens(texts):
return sum(len(t) // 4 for t in texts)
def build_context(agent, mode):
if mode == "shared":
return [text for _, text in TEAM_LOG]
# TODO: isolated mode: return the agent's brief plus the references in STORE
return [text for _, text in TEAM_LOG]
for mode in ["shared", "isolated"]:
ctx = build_context("copywriter", mode)
leak = any("INTERNAL" in t for t in ctx)
per_call = tokens(ctx)
print(f"{mode}: {per_call} tokens per call, {per_call * 4 * 10} for 4 agents x 10 calls, legal note visible to copywriter: {leak}")
Show a solution
# Shared context gives every agent everything; isolated context gives each agent only its brief.
TEAM_LOG = [
("legal", "INTERNAL: we may not claim 'certified' until the audit closes"),
("research", "Survey: 62% of users want offline mode"),
("pricing", "Launch price: 12 credits a month"),
("design", "Hero image uses the blue palette"),
] * 50 # a long shared history
STORE = {"research": "ref:store/survey", "pricing": "ref:store/price"} # shared results, by reference
BRIEFS = {"copywriter": "Write launch copy. Use the survey result and the launch price. 80 words."}
def tokens(texts):
return sum(len(t) // 4 for t in texts)
def build_context(agent, mode):
if mode == "shared":
return [text for _, text in TEAM_LOG]
return [BRIEFS[agent]] + list(STORE.values())
for mode in ["shared", "isolated"]:
ctx = build_context("copywriter", mode)
leak = any("INTERNAL" in t for t in ctx)
per_call = tokens(ctx)
print(f"{mode}: {per_call} tokens per call, {per_call * 4 * 10} for 4 agents x 10 calls, legal note visible to copywriter: {leak}")
Finished the chapter? Take the chapter check.