Lesson 43.1 · 1 of 5 · 5 min read · Reviewed 24 Nov 2026
Why use subagents
By the end, you can
- Explain when subagents help: isolated context, parallel work and specialised tools.
- Spot tasks where a single agent is simpler and better.
A subagentSubagent: A separate agent instance, with its own context and instructions, that a main agent delegates a task to. is an agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. started by another agent, with its own fresh context, its own instructions and often its own tools. It does a job and hands back a short result.
Three good reasons
- Context isolation. Exploring a large codebase can fill a context windowContext window: The maximum number of tokens a model can consider at once, counting the input and the output it generates. with file contents. A subagent does the reading and returns a one-page summary, so the main agent stays focused.
- Parallel work. Independent questions (five vendors, three regions, four modules) can run at the same time.
- Specialisation. A reviewer subagent gets read-only tools and a reviewer’s prompt. A test-writer gets write access to the tests folder only.
The cost
Each subagent re-reads its instructions and does its own tool calls, so total tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. go up, often several times. The parent must also write good briefs and combine results.
When not to
If the pieces depend on each other step by step, such as changing an interface and then every caller, split work loses the thread. Keep it in one agent.
Key points
- A subagent runs with its own context window and returns only a summary to its parent.
- Use subagents to keep exploration out of the main context, to run independent work in parallel, or to give a task narrower tools.
- Subagents add tokens and coordination; tightly coupled tasks are better kept in one agent.
Measure context saved by a subagent
Compare the parent agent's context, and the total tokens, when it explores a codebase itself versus delegating to subagents.
You will have: A table of parent-context tokens and total tokens for three designs, with a note on when subagents pay off.
Steps
- Set illustrative numbers: 30 files of about 1,500 tokens each, a 3,000-token brief per subagent and a 500-token summary back.
- Design A: the parent reads every file itself. Its context holds them all.
- Design B: one subagent reads everything and returns a summary.
- Design C: three subagents each read a third in parallel and each return a summary.
- Print parent context, total tokens and relative cost for each design.
Done when
- Design A puts all 45,000 file tokens in the parent's context.
- Designs B and C keep the parent's context under 2,000 tokens of summaries.
- Design C's total is the highest, showing the extra cost of more subagents.
Open in the Playground No API key needed.
Starter code (Python)
FILES, PER_FILE, BRIEF, SUMMARY = 30, 1500, 3000, 500 # illustrative numbers
def design(subagents):
"""Return (parent_context, total_tokens). subagents=0 means the parent reads everything itself."""
reading = FILES * PER_FILE
# TODO: with no subagents the parent holds all the reading
# TODO: with k subagents each pays BRIEF plus its share of reading, and returns SUMMARY to the parent
return reading, reading
for name, k in [("A: parent reads all", 0), ("B: one subagent", 1), ("C: three subagents", 3)]:
parent, total = design(k)
print(f"{name:<22} parent={parent:>6,} total={total:>6,}")
Show a solution
FILES, PER_FILE, BRIEF, SUMMARY = 30, 1500, 3000, 500 # illustrative numbers
def design(subagents):
"""Return (parent_context, total_tokens). subagents=0 means the parent reads everything itself."""
reading = FILES * PER_FILE
if subagents == 0:
return reading, reading
parent = subagents * SUMMARY
total = subagents * (BRIEF + SUMMARY) + reading
return parent, total
base = design(0)[1]
for name, k in [("A: parent reads all", 0), ("B: one subagent", 1), ("C: three subagents", 3)]:
parent, total = design(k)
print(f"{name:<22} parent={parent:>6,} total={total:>6,} cost={total / base:.2f}x")
print("Subagents keep the parent focused, but total tokens go up with every subagent.")