Concept #63Mediumextended-ai-concepts

What is a sub-agent?

#gen-ai#agents

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

PropertyDescription
SpecializationFocused on one domain or task type
Receives tasksGets instructions from orchestrator
Returns resultsSends output back to orchestrator
Has own toolsAccess to domain-specific tools
Can spawn sub-agentsHierarchy can be multi-level

Example with LangGraph

python
from 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

ScenarioWhy Sub-Agents Help
Complex multi-step tasksDivide and conquer
Parallel processingRun independent tasks simultaneously
Specialized expertiseEach agent has domain-specific tools
Quality controlOne agent checks another's work
Large-scale automationScale workers independently

Sub-Agent vs Regular Agent

Regular AgentSub-Agent
ControlSelf-directedDirected by orchestrator
ScopeFull taskSingle subtask
Goal sourceUserParent agent
IndependenceHighLow 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