|
| 1 | +# Managed Agent: Create and Use a Custom Agent |
| 2 | + |
| 3 | +> For setup, authentication, backends, and background on `ManagedAgent`, see the |
| 4 | +> [ManagedAgent guide](../../../../docs/guides/agents/managed_agent/index.md). |
| 5 | +
|
| 6 | +## Overview |
| 7 | + |
| 8 | +This sample demonstrates the **control-plane lifecycle** of a custom managed |
| 9 | +agent: creating a persistent, named agent *resource* — its persona and |
| 10 | +server-side tools baked in — then driving it and deleting it. |
| 11 | + |
| 12 | +You do **not** need a custom resource just to set a persona or server-side |
| 13 | +tools. `ManagedAgent` accepts both inline: `instruction=...` for a persona (see |
| 14 | +the [`system_instruction`](../system_instruction) sample) and |
| 15 | +`tools=[google_search]` for server-side tools (see the [`basic`](../basic) |
| 16 | +sample). Create a custom resource when you instead want a reusable, |
| 17 | +server-managed agent that other apps and sessions can share by id. |
| 18 | + |
| 19 | +This module drives that lifecycle: run it with `--create` to provision the |
| 20 | +resource (reusing the genai client `ManagedAgent` already holds, |
| 21 | +`root_agent.api_client`, which exposes both interactions and agent |
| 22 | +create/delete), then drive `root_agent` with `adk web` / `adk run`, and |
| 23 | +`--delete` to remove it. |
| 24 | + |
| 25 | +## Setup |
| 26 | + |
| 27 | +Custom-agent creation requires the **GEAP / Vertex** backend (`global` |
| 28 | +location); the Gemini API backend cannot create agent resources. For backend |
| 29 | +selection, authentication, and credentials, see the |
| 30 | +[ManagedAgent guide](../../../../docs/guides/agents/managed_agent/index.md#prerequisites). |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +```bash |
| 35 | +# 1. Create the custom agent (once). |
| 36 | +python contributing/samples/managed_agent/custom_agent/agent.py --create |
| 37 | + |
| 38 | +# 2. Chat with it. Provisioning can take a few minutes (longer for the first |
| 39 | +# agent in a project), so wait a moment after --create before the first turn. |
| 40 | +adk run contributing/samples/managed_agent/custom_agent |
| 41 | +# or: adk web |
| 42 | + |
| 43 | +# 3. Delete it when done. |
| 44 | +python contributing/samples/managed_agent/custom_agent/agent.py --delete |
| 45 | +``` |
| 46 | + |
| 47 | +Creation is asynchronous: `--create` returns before the agent is fully ready, so |
| 48 | +if the first turn fails with a "not found" / "being created" error, wait a few |
| 49 | +seconds and retry. |
| 50 | + |
| 51 | +## Sample Inputs |
| 52 | + |
| 53 | +Answers are grounded in live search, so exact text varies: |
| 54 | + |
| 55 | +- `What are the most significant AI announcements this week?` |
| 56 | + |
| 57 | + The created agent's persona makes it answer **concisely** and **cite its |
| 58 | + sources**, using server-side `google_search`. |
| 59 | + |
| 60 | +- `Summarize that in one sentence.` |
| 61 | + |
| 62 | + A follow-up turn that reuses the recovered interaction (multi-turn chaining). |
| 63 | + |
| 64 | +## Graph |
| 65 | + |
| 66 | +```mermaid |
| 67 | +graph LR |
| 68 | + User -->|message| CustomManagedAgent |
| 69 | + CustomManagedAgent -->|interactions.create| ManagedAgentsAPI |
| 70 | + ManagedAgentsAPI -->|server-side google_search| ManagedAgentsAPI |
| 71 | + ManagedAgentsAPI -->|streamed events| CustomManagedAgent |
| 72 | + CustomManagedAgent -->|answer| User |
| 73 | +``` |
| 74 | + |
| 75 | +## How To |
| 76 | + |
| 77 | +- **Define the custom agent**: pass a `system_instruction` (persona) and |
| 78 | + server-side `tools` (here `{'type': 'google_search'}`) to |
| 79 | + `client.agents.create(...)`, extending the `antigravity-preview-05-2026` base |
| 80 | + agent. |
| 81 | +- **Reuse the ManagedAgent client**: `root_agent.api_client` is the genai client |
| 82 | + `ManagedAgent` already holds; its `agents.create` / `agents.delete` cover the |
| 83 | + control plane. |
| 84 | +- **Provision a sandbox**: `ManagedAgent(environment={'type': 'remote'})` gives |
| 85 | + each interaction a remote sandbox (required to run the agent). |
| 86 | +- **Run it**: `--create` provisions, `--delete` removes; in between, `root_agent` |
| 87 | + is a normal `BaseAgent`, so `adk web` / `adk run` (or a `Runner`) drive it. |
0 commit comments