|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""A coordinator LlmAgent that calls ManagedAgent specialists as single-turn tools. |
| 16 | +
|
| 17 | +This sample shows a local ``LlmAgent`` orchestrating two server-backed |
| 18 | +``ManagedAgent`` specialists exposed as single-turn sub-agents |
| 19 | +(``mode='single_turn'``). ADK auto-wraps each single-turn sub-agent as an inline |
| 20 | +tool: the coordinator calls a specialist like a tool, receives the result, and |
| 21 | +may call several specialists within a single turn before composing the final |
| 22 | +answer. The specialists' internal events are preserved in the shared session. |
| 23 | +
|
| 24 | +Each managed call is stateless: single-turn runs are isolated, so the |
| 25 | +coordinator should pass a self-contained request to each specialist. |
| 26 | +
|
| 27 | +Two specialists are configured: |
| 28 | +
|
| 29 | +- ``managed_search_agent`` -- a ``ManagedAgent`` with the server-side |
| 30 | + ``google_search`` tool, for questions that require web search results. |
| 31 | +- ``managed_code_execution_agent`` -- a ``ManagedAgent`` with server-side code |
| 32 | + execution, for questions that require computation. |
| 33 | +
|
| 34 | +Run with ``adk web`` / |
| 35 | +``adk run contributing/samples/managed_agent/single_turn``. See the README |
| 36 | +for the required environment / auth setup. |
| 37 | +""" |
| 38 | + |
| 39 | +import os |
| 40 | + |
| 41 | +from google.adk.agents import LlmAgent |
| 42 | +from google.adk.agents import ManagedAgent |
| 43 | +from google.adk.tools import google_search |
| 44 | +from google.genai import types |
| 45 | + |
| 46 | +# The Managed Agent id served by the Managed Agents API. Override with the |
| 47 | +# MANAGED_AGENT_ID environment variable if your project has access to a |
| 48 | +# different agent. |
| 49 | +_DEFAULT_AGENT_ID = 'antigravity-preview-05-2026' |
| 50 | +_AGENT_ID = os.environ.get('MANAGED_AGENT_ID', _DEFAULT_AGENT_ID) |
| 51 | + |
| 52 | +# A ManagedAgent specialist for questions that require web search results. |
| 53 | +# mode='single_turn' exposes it to the coordinator as an inline tool. |
| 54 | +managed_search_agent = ManagedAgent( |
| 55 | + name='managed_search_agent', |
| 56 | + mode='single_turn', |
| 57 | + description=( |
| 58 | + 'Answers questions that require up-to-date information from the web.' |
| 59 | + ' Uses server-side Google Search.' |
| 60 | + ), |
| 61 | + agent_id=_AGENT_ID, |
| 62 | + environment={'type': 'remote'}, |
| 63 | + tools=[google_search], |
| 64 | +) |
| 65 | + |
| 66 | +# A ManagedAgent specialist that solves computational questions by running code |
| 67 | +# server-side. mode='single_turn' exposes it to the coordinator as an inline |
| 68 | +# tool. |
| 69 | +managed_code_execution_agent = ManagedAgent( |
| 70 | + name='managed_code_execution_agent', |
| 71 | + mode='single_turn', |
| 72 | + description=( |
| 73 | + 'Solves computational, math, or data questions by writing and running' |
| 74 | + ' code server-side. Use for arithmetic, numeric, and other tasks best' |
| 75 | + ' handled by executing code.' |
| 76 | + ), |
| 77 | + agent_id=_AGENT_ID, |
| 78 | + environment={'type': 'remote'}, |
| 79 | + tools=[types.Tool(code_execution=types.ToolCodeExecution())], |
| 80 | +) |
| 81 | + |
| 82 | +# The local coordinator. No `model` is set, so ADK uses the default model. The |
| 83 | +# two managed specialists are single-turn sub-agents, so ADK exposes each as an |
| 84 | +# inline tool; the coordinator calls them and keeps control of the turn (it can |
| 85 | +# call both before answering). |
| 86 | +root_agent = LlmAgent( |
| 87 | + name='managed_tool_coordinator', |
| 88 | + description='Calls managed specialists as tools and composes the answer.', |
| 89 | + instruction=( |
| 90 | + 'You are an assistant with two specialist tools.\n' |
| 91 | + '- Use `managed_search_agent` to look up current information from the' |
| 92 | + ' web.\n' |
| 93 | + '- Use `managed_code_execution_agent` to compute results by running' |
| 94 | + ' code.\n' |
| 95 | + 'You may call both tools in a single turn -- for example, look up a' |
| 96 | + ' value and then compute with it -- and then write the final answer' |
| 97 | + ' yourself.' |
| 98 | + ), |
| 99 | + sub_agents=[managed_search_agent, managed_code_execution_agent], |
| 100 | +) |
0 commit comments