Sub topic 10
Subagents as a way to isolate context
This chapter assumes you have read the earlier chapters on context growth and prompt caching. You should also be comfortable reading short Python programs and JSON.
The problem a subagent solves
Every turn of an agent loop resends the whole context. A file search that returns 30,000 tokens of grep output costs you once when it arrives. It then costs you again on every later turn, because the output stays in the history.
A subagent is a separate agent run with its own fresh context. The parent agent hands it a task, the subagent does the noisy work, and only a short result goes back to the parent. The subagent's intermediate tool calls and outputs never enter the parent's history.
How the flow works
The parent pays for the task description and the summary. The subagent pays for its own loop, but that loop starts from a small context and is thrown away when it finishes.
A worked estimate
Reuse the loop cost function from the earlier chapter. Suppose the parent has a 10,000-token context and then runs a search that takes 10 turns, adding 3,000 tokens per turn.
def loop_input_tokens(system_tokens, step_tokens, turns):
"""Total input tokens billed across an agent loop with no caching."""
total = 0
context = system_tokens
for _ in range(turns):
total += context
context += step_tokens
return total
# Option A: the parent does the search itself.
inline = loop_input_tokens(10_000, 3_000, 10)
# Option B: a subagent searches from a small context (2,000 tokens),
# then the parent adds a 500-token summary and runs 5 more turns.
subagent = loop_input_tokens(2_000, 3_000, 10)
parent_after = loop_input_tokens(10_000 + 500, 500, 5)
print(inline) # 235000
print(subagent + parent_after) # 179500 for the search plus 5 turnsTo compare fairly, add the same five follow-up turns to option A. In option A the parent carries 30,000 extra tokens into each of those turns.
inline_followup = loop_input_tokens(10_000 + 30_000, 500, 5)
print(inline + inline_followup) # 235000 + 205000 = 440000Option B bills about 179,500 input tokens and option A about 440,000. The gap comes mostly from the follow-up turns, which carry the search output in option A and a short summary in option B. The longer the parent keeps working after the search, the larger the saving.
Trade-offs and failure modes
Subagents are not free.
- Fixed overhead. Each subagent has its own system prompt and task description. For a lookup that needs one small read, delegating costs more than doing it inline.
- Lost detail. The parent sees only the summary. If the summary omits a file path or a line number the parent needs, it must delegate again or read the file itself.
- Weak briefs. The subagent knows only what the parent writes in the task. A vague brief produces wasted turns or an answer to the wrong question.
- No shared cache. A subagent with a different prompt prefix cannot reuse the parent's cached prefix. Its savings come from a smaller context, not from caching.
- Parallel spend. Several subagents running at once multiply spend, so cap how many can run.
When to use one
Delegate work that reads a lot and concludes in a little: codebase searches, log analysis, summarizing a long document, or running a test suite and reporting failures. Keep work inline when the next step depends on exact details of the output.
Subagents also connect to routing. The search step does not need your strongest model, so a subagent can run on a small, cheap tier while the parent stays on a larger one.
Exercise
A parent agent has a 20,000-token context and 8 turns of work left after a search. The search would add 25,000 tokens to its context if run inline. A subagent would return a 600-token summary instead. Roughly how many extra input tokens per remaining turn does delegating save, and why might delegation still lose for a search that needs only one small file read?
Show answer
Delegating saves about 24,400 input tokens per remaining turn (25,000 minus 600), or about 195,200 over 8 turns. For a single small read, the subagent's fixed overhead (its own system prompt and task brief) and the risk of a lossy summary can cost more than the few hundred tokens the inline read would add.