Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 960963424
Managed Agent: Create and Use a Custom Agent
For setup, authentication, backends, and background on
ManagedAgent, see the ManagedAgent guide.
Overview
This sample demonstrates the control-plane lifecycle of a custom managed agent: creating a persistent, named agent resource — its persona and server-side tools baked in — then driving it and deleting it.
You do not need a custom resource just to set a persona or server-side
tools. ManagedAgent accepts both inline: instruction=... for a persona (see
the system_instruction sample) and
tools=[google_search] for server-side tools (see the basic
sample). Create a custom resource when you instead want a reusable,
server-managed agent that other apps and sessions can share by id.
This module drives that lifecycle: run it with --create to provision the
resource (reusing the genai client ManagedAgent already holds,
root_agent.api_client, which exposes both interactions and agent
create/delete), then drive root_agent with adk web / adk run, and
--delete to remove it.
Setup
Custom-agent creation requires the GEAP / Vertex backend (global
location); the Gemini API backend cannot create agent resources. For backend
selection, authentication, and credentials, see the
ManagedAgent guide.
Usage
# 1. Create the custom agent (once).
python contributing/samples/managed_agent/custom_agent/agent.py --create
# 2. Chat with it. Provisioning can take a few minutes (longer for the first
# agent in a project), so wait a moment after --create before the first turn.
adk run contributing/samples/managed_agent/custom_agent
# or: adk web
# 3. Delete it when done.
python contributing/samples/managed_agent/custom_agent/agent.py --delete
Creation is asynchronous: --create returns before the agent is fully ready, so
if the first turn fails with a "not found" / "being created" error, wait a few
seconds and retry.
Sample Inputs
Answers are grounded in live search, so exact text varies:
-
What are the most significant AI announcements this week?The created agent's persona makes it answer concisely and cite its sources, using server-side
google_search. -
Summarize that in one sentence.A follow-up turn that reuses the recovered interaction (multi-turn chaining).
Graph
graph LR
User -->|message| CustomManagedAgent
CustomManagedAgent -->|interactions.create| ManagedAgentsAPI
ManagedAgentsAPI -->|server-side google_search| ManagedAgentsAPI
ManagedAgentsAPI -->|streamed events| CustomManagedAgent
CustomManagedAgent -->|answer| User
How To
- Define the custom agent: pass a
system_instruction(persona) and server-sidetools(here{'type': 'google_search'}) toclient.agents.create(...), extending theantigravity-preview-05-2026base agent. - Reuse the ManagedAgent client:
root_agent.api_clientis the genai clientManagedAgentalready holds; itsagents.create/agents.deletecover the control plane. - Provision a sandbox:
ManagedAgent(environment={'type': 'remote'})gives each interaction a remote sandbox — optional, and omitted by samples whose tools do not need one (seeremote_mcp). - Run it:
--createprovisions,--deleteremoves; in between,root_agentis a normalBaseAgent, soadk web/adk run(or aRunner) drive it.