Back to Adk Python

ADK Agent Sub-Agents Sample

contributing/agent_samples/sub_agents/README.md

2.0.0b12.1 KB
Original Source

ADK Agent Sub-Agents Sample

Overview

This sample demonstrates how to create a hierarchical agent setup using sub-agents in the ADK framework, and also showcases how to use tool confirmation.

It defines a root Agent named sub_agents that coordinates two sub-agents: info_agent and close_agent.

  • info_agent is equipped with a tool to check account status.
  • close_agent is equipped with a tool to close accounts, which requires user confirmation before execution.

The root agent delegates tasks to these sub-agents based on the user's prompt. This sample illustrates how to modularize capabilities into separate agents instead of combining all tools on a single agent.

Sample Prompts

  • Check the status of account ACC-123.
  • Close account ACC-123.
  • Check if account ACC-123 is active, and if so, close it.

Graph

text
Agent (name="sub_agents")
├── Agent (name="info_agent")
│   └── Tool (name="get_account_status")
└── Agent (name="close_agent")
    └── Tool (name="close_account", require_confirmation=True)

How To

  1. Define the specific tools for each sub-agent:

    python
    def get_account_status(account_id: str) -> str:
        """Gets the status of a bank account."""
        return f"Account {account_id} is active."
    
    def close_account(account_id: str) -> str:
        """Closes a bank account."""
        return f"Account {account_id} has been closed."
    
  2. Register tools to their respective sub-agents, using FunctionTool for confirmation:

    python
    from google.adk.agents import Agent
    from google.adk.tools.function_tool import FunctionTool
    
    info_agent = Agent(
        name="info_agent",
        description="An agent that can check account status.",
        tools=[get_account_status],
    )
    
    close_agent = Agent(
        name="close_agent",
        description="An agent that can close accounts.",
        tools=[FunctionTool(func=close_account, require_confirmation=True)],
    )
    
  3. Add the sub-agents to the root agent's sub_agents list:

    python
    root_agent = Agent(
        name="sub_agents",
        sub_agents=[info_agent, close_agent],
    )