Session 8· 10· 20 min

Multi-Agent Subagents (Supervisor)

What you'll learn
  • Create a supervisor agent that dispatches subagents as tools
  • See the "research then write" pipeline in action
  • Understand context isolation: each subagent starts fresh

What you will build

A supervisor that (1) calls a research subagent to gather facts, then (2) passes those facts to a writer subagent. Each subagent is a separate create_agent wrapped as a @tool on the supervisor.

10_subagents.py
# Create subagents
research_agent = create_agent(model=..., tools=[], system_prompt="Research specialist...")
writer_agent   = create_agent(model=..., tools=[], system_prompt="Technical writer...")

# Wrap as tools
@tool("research", description="Research a topic")                ①
def call_research(topic: str) -> str:
    result = research_agent.invoke(                               ②
        {"messages": [{"role": "user", "content": f"Research: {topic}"}]}
    )
    return result["messages"][-1].content                         ③

@tool("write_summary", description="Write a summary from notes")
def call_writer(notes: str) -> str:
    result = writer_agent.invoke(
        {"messages": [{"role": "user", "content": f"Summarize:\n{notes}"}]}
    )
    return result["messages"][-1].content

# Supervisor uses subagents as tools
supervisor = create_agent(
    model=...,
    tools=[call_research, call_writer],                           ④
    system_prompt="Coordinate research then writing.",
)
Each subagent is wrapped as a @tool — the supervisor calls it like any function.
The subagent gets a fresh invoke() with a custom message — no shared state.
Return only the final content, not the full trace. Context isolation keeps things clean.
The supervisor orchestrates: it decides the order and what to pass between subagents.
$ python 10_subagents.py
Context isolation is a feature
Each subagent invocation starts with a clean slate. The research agent does not see the writer's work, and vice versa. This prevents context pollution and keeps token costs low.
Knowledge Check
Why do subagents start with a clean context on every invocation?
Session 8 complete
You can now build single agents, add memory and streaming, return structured data, gate dangerous actions with human-in-the-loop, hand off between agents, and orchestrate subagents. Head to the capstone projects to apply these patterns in real apps.
Recap — what you just learned
  • Subagent-as-tool: wrap each agent in a @tool function
  • The supervisor orchestrates, subagents execute
  • Context isolation: each subagent starts fresh, reducing token waste
  • Return only the final content — not the full internal trace
Next up: Session 8 Troubleshooting