How to build a Multi-Agent System in Google ADK? Explain parent-child hierarchy and delegation with code examples.

#google-adk#multi-agent#orchestration#delegation#hierarchy

Answer

Multi-Agent Systems in Google ADK

Multi-Agent
Multi-Agent

Google ADK supports building multi-agent systems where multiple specialized agents collaborate under a parent orchestrator. The parent agent uses LLM reasoning to decide which child agent to delegate to.


Multi-Agent Architecture


Key Rules

RuleDescription
Single ParentAn agent instance can only have one parent
Tree StructureAgents form a tree, not a graph
Description MattersParent uses
text
description
to decide delegation
Shared StateAll agents in a tree share session state
Navigation
text
parent_agent
and
text
find_agent(name)
for traversal

Building a Multi-Agent System

python
from google.adk.agents import Agent

# Specialist agents
researcher = Agent(
    name="researcher",
    model="gemini-2.5-flash",
    description="Researches topics using web search and academic papers",
    instruction="You are a research specialist. Search for information and summarize findings.",
    tools=[google_search],
)

coder = Agent(
    name="coder",
    model="gemini-2.5-flash",
    description="Writes, debugs, and explains Python code",
    instruction="You are an expert Python programmer. Write clean, well-documented code.",
    tools=[code_execution],
)

writer = Agent(
    name="writer",
    model="gemini-2.5-flash",
    description="Writes clear, engaging technical content and documentation",
    instruction="You are a technical writer. Create well-structured content.",
)

# Orchestrator - delegates to specialists
orchestrator = Agent(
    name="orchestrator",
    model="gemini-2.5-pro",
    instruction="""You are a project manager. Analyze the user's request
    and delegate to the appropriate specialist:
    - researcher: for information gathering and research
    - coder: for programming and code tasks
    - writer: for content creation and documentation

    Combine results from multiple agents when needed.""",
    sub_agents=[researcher, coder, writer],
)

Combining Workflow + LLM Agents

python
from google.adk.agents import Agent, SequentialAgent, ParallelAgent

# Step 1: Gather data in parallel
data_gatherer = ParallelAgent(
    name="data_gatherer",
    sub_agents=[
        research_agent,     # search the web
        database_agent,     # query internal DB
    ]
)

# Step 2: Analyze (LLM-powered)
analyst = Agent(
    name="analyst",
    model="gemini-2.5-pro",
    instruction="Analyze the gathered data and extract key insights.",
    output_key="analysis",
)

# Step 3: Write report (LLM-powered)
report_writer = Agent(
    name="report_writer",
    model="gemini-2.5-flash",
    instruction="Write a report based on the analysis: {analysis}",
    output_key="report",
)

# Full pipeline
pipeline = SequentialAgent(
    name="research_pipeline",
    sub_agents=[data_gatherer, analyst, report_writer],
)

State Sharing Between Agents

python
# Agent A saves to state
agent_a = Agent(
    name="agent_a",
    model="gemini-2.5-flash",
    instruction="Research the topic and save your findings.",
    output_key="findings",     # saves output to state["findings"]
)

# Agent B reads from state
agent_b = Agent(
    name="agent_b",
    model="gemini-2.5-flash",
    instruction="Write a summary based on: {findings}",
    output_key="summary",
)

pipeline = SequentialAgent(
    name="pipeline",
    sub_agents=[agent_a, agent_b],  # agent_b reads agent_a's output
)

Learn more at Multi-Agent Systems and Workflow Agents.