Sub-Agents System
Complexity in AI tasks often requires specialization. The Sub-Agents System allows you to build a hierarchical architecture where a “Parent” agent can intelligently delegate tasks to specialized “Child” agents.Why Use Sub-Agents?
- Modular Design: Build and test small, expert agents individually before integrating them.
- Improved Accuracy: Specialized agents (e.g., a “Math Expert” or “Code Auditor”) are less likely to hallucinate in their specific domain.
- Tool Management: Avoid cluttering a single agent’s prompt with hundreds of tools. Instead, give each sub-agent only the tools it needs.
Configuration
To use sub-agents, create them individually and then pass them as a list to the parent agent.How Delegation Works
When a parent agent has sub-agents, it is automatically given tools to communicate with them:| Tool | Action |
|---|---|
call_sub_agent | Send a specific task to a child agent and wait for the result. |
Example Workflow
- User Query: “Research the latest AI news and write a Python script for a news aggregator.”
- Manager (Parent): Recognizes this requires research and coding.
- Manager: Calls
research_agentwith the task “Get latest AI news summaries”. - Researcher (Sub): Performs web search, returns summaries.
- Manager: Receives summaries, then calls
code_agentwith “Write a Python aggregator using this data: [summaries]”. - Coder (Sub): Writes the code, returns it.
- Manager: Synthesizes the final response to the user.
Best Practices
- Explicit Instructions: In the parent agent’s system instruction, briefly describe the expertise of each sub-agent so it knows who to turn to.
- Limit Depth: While hierarchical agents are powerful, avoid nesting agents too deep (e.g., Parent -> Child -> Grandchild) to prevent excessive latency and token usage.
- Shared Session IDs: Use the same
session_idfor both parent and child calls if you want them to share context indirectly through the memory store.