Open this lesson in your favourite AI. It'll walk you through the why, explain the demo, and quiz you on the try-it list.
A powerful technique: when a task has a self-contained sub-problem, spawn a sub-agent with a fresh, clean context window scoped just to that sub-problem, and return only the result to the parent. This keeps the parent's context from accumulating the sub-problem's scratch work, and gives the sub-agent the full window for its focused task. It's like calling a function with its own stack frame. Knowing when to spawn a fresh context (isolatable sub-task) vs. continue in the current one (tightly coupled) is central to scaling agent context.
The demo spawns a sub-agent with a clean context for an isolatable task (analyzing one document), returning only the conclusion. The parent never sees the sub-agent's intermediate reasoning, keeping its window clean.
Use these three in order. Each builds on the one before.
What does it mean to spawn a sub-agent with a fresh context window, and why is it useful?
Explain how delegating an isolatable sub-task to a sub-agent with a clean context keeps the parent's window lean, and when this works vs. when the sub-task needs parent history.
Design a recursive agent architecture that spawns fresh-context sub-agents for isolatable sub-tasks: how to decide what's isolatable, pass scoped inputs, merge results, and bound total cost across the tree.
from anthropic import Anthropic
client = Anthropic()
def sub_agent(task: str, scoped_input: str) -> str:
# Fresh, isolated context window — none of the parent's history
r = client.messages.create(model="claude-sonnet-4-6", max_tokens=200,
system=f"You handle one task: {task}. Return only the conclusion.",
messages=[{"role": "user", "content": scoped_input}])
return r.content[0].text # only this comes back to the parent
# Parent delegates an isolatable analysis; its own window stays clean:
conclusion = sub_agent("Assess legal risk in this clause",
scoped_input="Clause 7: ...the vendor may terminate without notice...")
print("parent receives only:", conclusion)python3 main.py