Expand description
Code mode: one tool through which the model runs a short Python program that calls the session’s own tools.
Function calling makes the model do the arithmetic, sorting and joining in
its head, one call at a time, with every result added to the conversation.
In code mode the model writes a program instead: run_code runs it in a
sandbox, each function the program calls is the session’s tool of that
name, and the model reads only the program’s final value. Measured on
Gemini 3.8 Live over 120 payments, the answer was right in 5 of 5 runs
against 2 of 5 with function calls, and the prompt held 77% fewer tokens.
On a single lookup, writing a program cost the caller up to 700 ms before
the first action, so by default the functions stay callable directly as
well, and the tool’s description says when to use which.
§Every call is a governed call
A program cannot reach around the session’s governance. Each call it makes
pauses the interpreter and runs down the same path as a direct call: the
phase’s tool list, the decision gate, the flow gate (with extraction of the
turn in progress), middleware, confirmation, the tool itself, and the flow
advance on its result. A refused call raises RuntimeError in the program
with the reason, and every call and its full result is recorded as a tool
execution. A program cannot write state or move the flow except through
the calls it makes.
Commit tools (a booking, a payment) are better left out of the functions: a commit should be one visible call, not a line inside a program.
§Sandboxes
InProcess runs Monty, a Python
interpreter written in Rust, on a blocking thread of this process. It is
fast and needs nothing installed, but it is not crash-isolated: a program
that asks for a huge allocation ('a' * 10**10) can abort the process or
run past its time limit while the allocation completes. Use it for
development, tests and trusted deployments. MontyPool (feature
code-pool) runs programs in Monty worker processes with a memory cap and
hard deadlines, so a runaway program kills only its worker.
use gemini_adk_rs::code_mode::{CodeLimits, CodeMode};
let code = CodeMode::new()
.function("lookup_payment_history")
.function("check_availability")
.limits(CodeLimits {
calls: 10,
..CodeLimits::default()
});Structs§
- Code
Function - A function a program can call: a tool of the session, by name.
- Code
Limits - Bounds on one program.
- Code
Mode - The code tool of a session: its name, the tools a program can call, its limits and the sandbox it runs in. See the module docs.
- InProcess
- Runs programs in Monty on a blocking thread of this process.
- Monty
Pool - Runs programs in a pool of Monty worker processes.
- Program
- A program running in a sandbox, suspended at a call or finished. See
CodeMode::start. - Unavailable
- A sandbox that refuses every program: the default when this crate is
built without the
code-modefeature.
Enums§
- Suspension
- Where a program has stopped: at a call to one of its functions, or at its end.
Constants§
- DEFAULT_
TOOL - The default name of the code tool.
Traits§
- Code
Sandbox - Runs programs: Python source with the given functions defined as globals.
- Execution
- A program suspended at a call. Dropping it cancels the program.
Functions§
- arguments
- Name a call’s arguments for
declaration: positional values in stub order, then the keywords. The errors are Python’s, for the program. - parameters
- The parameters of
declarationin stub order: required ones first, in the order the declaration requires them, then the optional ones. - python_
stub - The Python stub for
declaration: its signature and a docstring with the tool’s description, its parameters’ descriptions and, when known, what it returns. - without_
redefinitions sourcewithout its own stand-ins for the session’sfunctions.