Lesson 04 · Planning

Delegate big problems to a fresh agent

"Process isolation gives context isolation for free." The child agent does the dirty work; the parent gets a clean summary.

⏱ ~10 min · 📝 3 interactive widgets · 🧑‍💻 Based on shareAI-lab · s04_subagent.py

The parent agent's dilemma

Imagine asking Claude Code to "figure out how concurrency is handled in this 100K-line Rust repo." The naive approach: it runs ls, cat, grep, explores everything in the main context.

The problem: that exploration packs 30 tool_results into messages[], each a few thousand tokens. By the time it starts writing an answer, the context is already stuffed - a few more steps and it hits the limit, with a scattered result.

s04's solution: hand the exploration off to a new agent. The new agent starts fresh from messages=[], explores on its own, and returns only a summary to the parent. The parent's context only gains one entry: "called the task tool, result was X." Clean.

def run_subagent(prompt: str) -> str:
    sub_messages = [{"role":"user", "content": prompt}]  # fresh context
    for _ in range(30):  # safety cap to prevent runaway
        response = client.messages.create(..., messages=sub_messages, tools=CHILD_TOOLS, ...)
        ...
    # return only the final text; discard all intermediate reasoning
    return "".join(b.text for b in response.content if hasattr(b, "text"))

Parent vs child context comparison

This widget simulates a real task: "List all places in this repo that call a deprecated API." Run it two ways - (A) parent does it all, (B) spawns a subagent - and compare the final context sizes side by side.

CHILD_TOOLS: what tools the child gets

There's an easy-to-miss detail in s04: the child agent does not get the task tool.

# child can only use basic tools - cannot spawn grandchildren
CHILD_TOOLS = [bash, read_file, write_file, edit_file]

# parent has one extra: task
PARENT_TOOLS = CHILD_TOOLS + [task]

Why? To prevent recursive spawning from becoming a tree explosion. One subagent spawning four sub-subagents, who each spawn four more - within a few levels you have dozens of concurrent calls, blowing past both token budgets and API rate limits. s04's contract: spawning is flat, parent to child one level deep. Claude Code's real implementation works the same way - the Task tool cannot call the Task tool.

What the parent can and can't see

A quick responsibility check. For each statement below, decide True or False.

Interactive

Widget 1 · Parent vs Child · context size comparison

Same task, two strategies. Click Run to see the final messages[] length and estimated token count for each approach.

Parent does it all
messages: 0 · ~0 tokens
Spawn subagent
messages: 0 · ~0 tokens
Interactive

Widget 2 · True or False · parent-child agent boundaries

6 T/F questions to test your understanding of isolation boundaries.

Correct: 0 / 6
Interactive

Widget 3 · When to spawn · which tasks belong in a subagent?

Six task descriptions. Decide whether (A) the parent should handle it directly or (B) spawn a subagent. There's no single right answer, but some choices are clearly better.

Correct: 0 / 6