Gemini Tool Retrieval Plus Code Mode Cuts Agent Tokens 94%
Tool retrieval plus code mode cut an agent's input tokens from about 564,000 to about 30,000 on the same task. 43 API calls ran in the sandbox; only printed results reached the model.
- AuthorJames Calloway
- ChannelAI & Emerging Interfaces
- Filed
- Read4 min

- Input tokens fell from ~564,000 to ~30,000 when Gemini tool retrieval was combined with code mode on the same task
- Tool retrieval is in preview and available only through the Gemini Interactions API
- The combined run loaded five tool definitions and executed 43 API calls inside the sandbox
- Tool definitions alone consumed ~26,000 tokens before the agent started in the all-tools-loaded run
- Prompt Engineering recommends an approval step for data-changing actions and a dedicated container for production
An AI agent's input-token use fell from roughly 564,000 to roughly 30,000 — a 94% cut — when Google's Gemini tool retrieval was paired with code mode to count tickets by priority for gold-tier customers in Europe. The count came out the same in all three configurations tested by Prompt Engineering; only the token bill changed.
The test ran one agent and one task in each setup against a made-up company dataset. The agent had to join two APIs that return records one page at a time, filter open tickets, group them by priority, and post the counts to Slack. Exact numbers will vary with tools and data, but the spread tells a clear story: 564,000 input tokens with all tools loaded, 326,000 with tool retrieval alone, and 30,000 with retrieval plus code mode.
What is tool retrieval, and what does it defer?
Tool retrieval, in preview and available only through the Gemini Interactions API, attacks the first source of token bloat: tool definitions. A tool definition tells the model what a function does and which parameters it takes. Load every definition upfront and the context pays for tools the task may never touch — in the first test run, definitions alone occupied about 26,000 tokens before the agent did anything.
To use retrieval, you add tool_search to the tool list and mark tools with defer_loading. The model initially sees only each deferred tool's name and a short description. When it needs a tool, the API handles discovery within one request:
- The model calls tool_search with the function names it wants.
- The API returns the full definitions into the conversation.
- The model makes a function call that the app runs.
Once loaded, a tool stays in the conversation, and you must resend the full tool list on later requests. Google's documentation also says not to defer a tool named in the system prompt.
Where does code mode take over?
Code mode handles the second, bigger source of bloat: intermediate results. Records returned by tools while the agent works can swamp the context long before the answer arrives. With code mode, the agent writes a program that calls tools as functions and runs in a sandbox — a separate, locked-down environment. The program can loop through pages, filter records, and join data; only what it prints returns to the model. For a counting task, the model ends up with counts, not every page of tickets.
Cloudflare calls this pattern code mode; Anthropic calls it code execution with MCP. Gemini's tool retrieval supplies the discovery step for the combined setup, where deferred ordinary tools sit alongside an always-loaded tool that runs Python in the sandbox.
Why does the system prompt matter so much?
The system prompt — the standing instruction the model receives each turn — has to tell the model to read parameters through tool_search but make calls inside a program rather than invoking tools directly. Without it, the model can call loaded tools one at a time and drag raw results back into its context, undoing the savings.
In the combined run, the agent searched once and loaded five tool definitions. Its programs explored the data, counted tickets, and posted to Slack. 43 API calls ran inside the sandbox, with only a few printed lines reaching the model. The counting program walked the pages, matched customers to tickets, and verified the counts a second way using the API filters.
What safeguards does model-written code need?
Prompt Engineering recommends an approval step for actions that change data, such as posting a message or updating a record. It also suggests prefixing tool names with their service, like CRM or support, and having programs print only what the model needs. The demo used a separate Python process with a timeout; for production, a properly isolated environment such as a dedicated container is the recommendation.
The caveats are worth holding onto. The headline saving is a best case from a single demo on invented data. For a small, stable tool library, loading tools upfront may fit better, and prompt caching can cut the cost; keeping frequently used tools loaded and deferring the rest is the practical middle ground. The trade to weigh before copying the setup is what model-written code is allowed to do to live systems.
The broader lesson is that token cost depends less on what a model can see than on what has to pass through it. As agents take on more tools and more data-heavy work across APIs, pairing discovery with sandboxed execution looks set to become a standard cost lever rather than an exotic one.
Source: geeky-gadgets.com
More from James Calloway
Show full bio
Senior reporter covering regulation, policy and their impact on Product and interaction design, design systems at Interface Wire.
186 articles