Why Compose Agents?
- Separation of concerns: Each agent focuses on one domain
- Reusability: The same agent can be used in multiple contexts
- Hierarchical reasoning: Coordinator agents can delegate to specialists
- Scalability: Complex tasks are broken into manageable pieces
Basic Composition
Use theas_tool() method to convert an agent into a tool:
How It Works
When an agent is used as a tool:1
Parent calls child agent
The coordinator receives a task like “population of France, calculate 15% of it” and decides to delegate to
research_agent.2
Child agent runs its loop
The research agent executes its own think-act loop, searches for information, and returns “67 million”.
3
Results flow back
The coordinator receives the result and decides to call
math_agent with “calculate 15% of 67M”.4
Parent continues
After receiving “10.05 million” from the math agent, the coordinator synthesizes the final answer.
Custom Tool Names
Customize how the agent appears as a tool:Tracing Nested Agents
Parent spans automatically connect to child spans:Multi-Level Hierarchies
Agents can be nested multiple levels deep:Parallel Agent Execution
When a coordinator calls multiple agents, they can execute in parallel:Best Practices
- Clear descriptions: Help the parent agent know when to use each child
- Single responsibility: Each agent should do one thing well
- Appropriate depth: Too many levels adds latency and token cost
- Shared context: Pass relevant context through input, not globals
- Error handling: Child agent errors bubble up; handle gracefully