2.4 KiB
Managed Agent - Code Execution
For setup, authentication, backends, and background on
ManagedAgent, see the ManagedAgent guide.
Overview
This sample runs a ManagedAgent configured with the built-in code execution
tool so it can write and run code server-side to compute answers.
Unlike a regular LlmAgent (which enables code execution via
code_executor=BuiltInCodeExecutor()), ManagedAgent has no code_executor
field. Instead you pass the raw built-in tool config
types.Tool(code_execution=types.ToolCodeExecution()) in tools -- the same
config BuiltInCodeExecutor produces under the hood. This makes the sample a
demonstration of the raw types.Tool server-side tool path.
Sample Inputs
-
What is the sum of the first 50 prime numbers? Use code to compute it.The model writes and runs code server-side; the answer (5117) comes from the executed code rather than the model guessing.
-
Now do the same for the first 100 primes.A follow-up turn that reuses the recovered remote sandbox and the previous interaction (answer: 24133), demonstrating multi-turn chaining.
Graph
graph LR
User -->|message| ManagedAgent
ManagedAgent -->|interactions.create| ManagedAgentsAPI
ManagedAgentsAPI -->|server-side code execution| ManagedAgentsAPI
ManagedAgentsAPI -->|streamed events| ManagedAgent
ManagedAgent -->|answer| User
How To
- Create the agent: instantiate
ManagedAgentwith anagent_id, anenvironmentspec, andtools=[types.Tool(code_execution=types.ToolCodeExecution())]. Nomodelis set -- the model is part of the managed agent on the server. - Enable code execution:
ManagedAgenthas nocode_executorfield, so the rawtypes.Tool(code_execution=...)config is passed intools. The interactions converter turns it into the server-sidecode_executiontool. - Provision a sandbox:
environment={'type': 'remote'}requests a fresh remote sandbox. The resulting environment id is stored on emitted events, so subsequent turns automatically recover and reuse it. - Multi-turn chaining: the agent recovers the
previous_interaction_idfrom the session events, so follow-up turns continue the same interaction without any extra wiring. - Drive it: a
ManagedAgentis aBaseAgent, so a standardRunnerruns it just like any other agent.