Answer
What is a Sub-Agent?
A sub-agent is a specialized AI agent that operates under the direction of a parent (orchestrator) agent in a multi-agent system. The orchestrator delegates specific subtasks to sub-agents that have focused capabilities.
Concept
textāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā Orchestrator Agent ā ā (high-level planning & routing) ā āāāāāāāāāāāāā¬āāāāāāāāāāāāā¬āāāāāāāāāāāāāāāāāā ā ā āāāāāāāā¼āāā āāāāāāā¼āāāāāāā āāāāāāāāāāāāāā ā Search ā ā Coder ā ā Reviewer ā ā Agent ā ā Agent ā ā Agent ā āāāāāāāāāāā āāāāāāāāāāāāāā āāāāāāāāāāāāāā (web search) (writes code) (reviews code)
Characteristics of Sub-Agents
| Property | Description |
|---|---|
| Specialization | Focused on one domain or task type |
| Receives tasks | Gets instructions from orchestrator |
| Returns results | Sends output back to orchestrator |
| Has own tools | Access to domain-specific tools |
| Can spawn sub-agents | Hierarchy can be multi-level |
Example with LangGraph
pythonfrom langgraph.graph import StateGraph # Define sub-agents def research_agent(state): '''Sub-agent: searches web and retrieves info''' results = web_search(state["query"]) return {"research_results": results} def writing_agent(state): '''Sub-agent: writes content based on research''' content = llm.invoke( f"Write a report based on: {state['research_results']}" ) return {"draft": content} def review_agent(state): '''Sub-agent: reviews and improves draft''' improved = llm.invoke( f"Review and improve: {state['draft']}" ) return {"final": improved} # Orchestrator graph graph = StateGraph(dict) graph.add_node("research", research_agent) graph.add_node("write", writing_agent) graph.add_node("review", review_agent) graph.add_edge("research", "write") graph.add_edge("write", "review")
When to Use Sub-Agents
| Scenario | Why Sub-Agents Help |
|---|---|
| Complex multi-step tasks | Divide and conquer |
| Parallel processing | Run independent tasks simultaneously |
| Specialized expertise | Each agent has domain-specific tools |
| Quality control | One agent checks another's work |
| Large-scale automation | Scale workers independently |
Sub-Agent vs Regular Agent
| Regular Agent | Sub-Agent | |
|---|---|---|
| Control | Self-directed | Directed by orchestrator |
| Scope | Full task | Single subtask |
| Goal source | User | Parent agent |
| Independence | High | Low to medium |
Best Practices
- Keep sub-agents focused ā one responsibility each
- Define clear interfaces ā what input they receive, what they return
- Handle failures ā orchestrator should handle sub-agent errors gracefully
- Avoid deep nesting ā more than 3 levels becomes hard to debug