Files
google--adk-python/contributing/workflow_samples/shared_sub_agent
Wei (Jack) Sun 1c64a41b53 feat: ADK 2.0 alpha
Introduces two major capabilities:
- Workflow runtime: graph-based execution engine for composing
  deterministic execution flows for agentic apps, with support for
  routing, fan-out/fan-in, loops, retry, state management, dynamic
  nodes, human-in-the-loop, and nested workflows
- Task API: structured agent-to-agent delegation with multi-turn
  task mode, single-turn controlled output, mixed delegation
  patterns, human-in-the-loop, and task agents as workflow nodes

Co-Authored-By: Bo Yang <ybo@google.com>
Co-Authored-By: George Weale <gweale@google.com>
Co-Authored-By: Sean Zhou <seanzhougoogle@google.com>
Co-Authored-By: Shangjie Chen <deanchen@google.com>
Co-Authored-By: Swapnil Agarwal <swapnilag@google.com>
Co-Authored-By: Wei Sun <weisun@google.com>
Co-Authored-By: Xuan Yang <xygoogle@google.com>
Co-Authored-By: Yifan Wang <wanyif@google.com>
Change-Id: I35932c50cfe29ff68559e3781713dbb5eb7b3382
2026-03-17 23:24:58 -07:00
..
2026-03-17 23:24:58 -07:00
2026-03-17 23:24:58 -07:00
2026-03-17 23:24:58 -07:00

ADK Agent Shared Sub-Agent Sample

Overview

This sample demonstrates how to reuse the same LlmAgent instance in multiple places within an agent tree.

In complex agent architectures, you may have specialized sub-agents (like a travel_agent and a shopping_agent) that both need access to the same shared capability (like a web search_agent). Instead of redefining the search_agent multiple times, you can instantiate it once and pass it as a sub-agent to multiple parents.

The ADK framework supports this naturally. When the workflow executes, events generated by the shared sub-agent will properly reflect the different execution paths (e.g., root/travel_agent/search_agent vs root/shopping_agent/search_agent).

Sample Inputs

You can test the routing and the shared sub-agent by asking travel or shopping questions:

  • I want to book a flight to London from NYC.

    Routes to travel_agent -> search_agent.

  • What is the price of a MacBook Air M3?

    Routes to shopping_agent -> search_agent.

  • Hello, who are you?

    Handled by root_agent directly.

Graph

                  [ root_agent ]
                 /              \
                /                \
               v                  v
     [ travel_agent ]      [ shopping_agent ]
               \                  /
                \                /
                 v              v
               [ search_agent ]

How To

  1. Define the Shared Agent: Instantiate the agent that will be reused.

    from google.adk.agents.llm_agent import LlmAgent
    
    search_agent = LlmAgent(
        name='search_agent',
        description='Searches the web and returns raw results.',
        instruction='...',
        disallow_transfer_to_parent=True,
        disallow_transfer_to_peers=True,
    )
    
  2. Pass to Multiple Parents: Pass the exact same search_agent instance into the sub_agents list of multiple different parent agents.

    travel_agent = LlmAgent(
        name='travel_agent',
        instruction='Delegate to search_agent...',
        sub_agents=[search_agent],
    )
    
    shopping_agent = LlmAgent(
        name='shopping_agent',
        instruction='Delegate to search_agent...',
        sub_agents=[search_agent],
    )
    
  3. Assemble the Root: Combine the intermediate agents under a root agent.

    root_agent = LlmAgent(
        name='root_agent',
        instruction='Route to travel_agent or shopping_agent',
        sub_agents=[travel_agent, shopping_agent],
    )