Lesson 04 · planning

Handle big problems to a newly opened agent

"Process isolation gives context isolation for free." The child agent does the dirty work, and the parent agent only collects the clean summary.

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

The parent agent’s dilemma

Imagine you asked Claude Code to "figure out how this 100,000-line Rust repository handles concurrency." Intuitive approach: It does ls, cat, grep, and searches in the main context.

Problem: This round of exploration will pile up 30 tool_results in messages[], each with several thousand tokens. By the time it actually starts writing the answer, the context has been filled up by the exploration process - the upper limit will be triggered after a few more steps, and the answer will still be confusing.

Solution to s04: Throw the exploration task to a new agent. The new agent starts from messages=[], explores its own, and finally only returns the summary to the parent agent. The parent context only has one more sentence: "The task tool was adjusted once, and the result was XXX", which is refreshing.

def run_subagent(prompt: str) -> str:
    sub_messages = [{"role":"user", "content": prompt}] # New context
    for _ in range(30): # Safety upper limit to prevent loss of control
        response = client.messages.create(..., messages=sub_messages, tools=CHILD_TOOLS, ...)
        ...
    # Only the last text is returned, all intermediate reasoning is discarded
    return "".join(b.text for b in response.content if hasattr(b, "text"))

Father and son context comparison

This widget simulates a real task: "List all places in this warehouse that call deprecated API". You can run it with two strategies: (A) the parent agent does it by itself; (B) spawn a subagent. Compare the sizes of the last two contexts left and right.

CHILD_TOOLS: What tools can the sub-agent get?

There is a detail in the implementation of s04 that is easy to miss: the sub-agent cannot get the task tool.

# Sub-agent can only use basic tools and cannot spawn grandson
CHILD_TOOLS = [bash, read_file, write_file, edit_file]

# Parent agent has one more task tool
PARENT_TOOLS = CHILD_TOOLS + [task]

Why? Avoid recursive dispatch becoming a tree explosion. One subagent spawns four subsubagents, and there are dozens of concurrent calls in just a few rounds. Neither the token nor the API speed limit can handle it. The convention of s04: spawn is flat, and one layer of parent → child is one layer. This is also the case in the real implementation of Claude Code - it is forbidden to call the Task tool again in the Task tool.

visible and invisible

Let’s sort out the rights and responsibilities: Which of the following questions do you think is T and which F do you think?

Interactive

Widget 1 · Parent vs Child · context size comparison

Two strategies for the same task. Click Run and look at the final length of messages[] on both sides and the total token estimate.

🧠 父 agent 亲自做
messages: 0 · ~0 tokens
🎯 spawn subagent
messages: 0 · ~0 tokens
Interactive

Widget 2 · True or False · Rights and responsibilities of father and son agents

6 T/Fs to test your understanding of isolation boundaries.

答对 0 / 6
Interactive

Widget 3 · When to spawn · What tasks are suitable to be assigned to subagent?

You are given 6 task descriptions, decide whether (A) the parent agent does it by itself or (B) the spawn subagent. There is no "absolute right", but there are obvious advantages and disadvantages.

答对 0 / 6