
Agent input-token use fell from about 564,000 to about 30,000 when combining Gemini retrieval and code mode to handle a ticket-counting task. Prompt Engineering breaks down this pairing of function discovery and code execution using a made-up company dataset, showing how you can get counts without feeding every record back to the model. Find out how the pieces fit together, why instructions matter when directing the agent to run code, and where isolation and your approval fit before that code makes changes to your systems.
Gemini Tool Retrieval and Code Mode Cut Context Use
TL;DR Key Takeaways :
- Gemini tool retrieval, available in preview only through the Interactions API, loads deferred tool definitions when needed.
- Code mode runs tool calls in a sandbox, returning only printed results to the model’s context.
- In one test, retrieval plus code mode used about 30,000 input tokens, compared with about 326,000 for retrieval alone and about 564,000 with all tools loaded.
- In the combined test, the agent loaded five tool definitions and made 43 API calls inside the sandbox.
- Prompt Engineering recommends approval for actions that change data and a properly isolated environment for production.
Google’s Gemini tool retrieval reduces the context used by tool definitions, while code mode moves work on tool results into a sandbox. Tool retrieval is in preview and available only through the Gemini Interactions API.
An AI agent’s context window holds the information the model reads, including tool definitions and the results of calls to those tools. As an agent gains tools and handles more records, both can take up substantial space before it reaches an answer.
Load Tool Definitions Only When Needed
A tool definition tells the model what a function does and which parameters it accepts. Loading every definition upfront can consume context even when the task needs only a few tools.
To use retrieval, add tool_search to the tool list and mark tools that can be held back with defer_loading. The model initially sees each deferred tool’s name and short description, but not its parameters. When it needs a tool, the API handles discovery within one request:
- The model calls
tool_searchwith the names of the functions it wants. - The API returns the full definitions of those tools in the conversation.
- The model makes a function call that the app runs.
Once loaded, a tool stays in the conversation, and you must send the full tool list when you return to the API. Google’s documentation also says not to defer a tool named in the system prompt.
Below are more guides on Gemini API and tools from our extensive range of articles.
- Using the Gemini Pro API to build AI apps in Google AI Studio
- How to setup Google Gemini Pro API key and AI model- Beginners Guide
- How to use Gemini AI API function calling and more
- How to use Google’s AI Gemini Pro with Python
Keep Intermediate Results in the Sandbox
Code mode tackles a different source of token use: records returned by tools while an agent works toward an answer. Instead of sending every result through the model, the agent writes a program that calls tools as functions and runs in a sandbox, a separate, locked-down environment.
Inside the sandbox, the program can loop through pages, filter records and join data; only what it prints returns to the model. For a ticket-counting task, that can leave the model with the counts rather than every page of tickets.
To write the program, the model still needs the right tool names and parameters. Cloudflare calls this pattern code mode, while Anthropic calls it code execution with MCP; Gemini’s tool retrieval supplies the discovery step for the setup described here.
Wire Retrieval to a Code-Running Tool
In this setup, the ordinary tools are deferred and an always-loaded run code tool executes Python in the sandbox. The model searches for the definitions it needs, writes a program using those functions and lets the sandbox make the API calls.
A system prompt, the standing instruction the model receives on each turn, tells it to read parameters through tool_search but make calls in a program rather than invoke the tools directly. Without that instruction, the model can call loaded tools one at a time and bring raw results back into its context.
For a small, stable tool library, loading tools upfront may be a better fit, and prompt caching can make it less costly. Prompt Engineering recommends keeping frequently used tools loaded and deferring the rest.
The Combined Run Used About 30,000 Input Tokens
A test ran the same agent and task once in each configuration, using a made-up company dataset; the exact token counts will change with the tools and data. The agent had to count open tickets from gold-tier customers in Europe by priority and post the counts to Slack, joining two APIs that returned records one page at a time.
- 564,000 input tokens: All tools loaded; ticket pages returned to context and the count was correct.
- 326,000 input tokens: Tool retrieval reduced upfront definitions, but customer and ticket pages still reached the model.
- 30,000 input tokens: Retrieval plus code mode produced the same correct count.
In the first run, definitions alone occupied about 26,000 tokens before the agent started. With retrieval alone, the first request fell to a few thousand tokens, but the intermediate records still consumed context.
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 returning to the model. The counting program walked through the pages, matched customers to tickets and checked the counts a second way using the API filters.
Set Boundaries for Model-Written Code
Prompt Engineering recommends an approval step for actions that change data, such as posting a message or updating a record. It also recommends prefixing tool names with their service, such as CRM or support, and having programs print only what the model needs.
The demo used a separate Python process with a timeout. For production, Prompt Engineering recommends a properly isolated environment, such as a dedicated container. That isolation and an approval step are the safeguards to address before connecting model-written programs to tools that can make changes.
In our view, the useful idea is that token cost depends less on what a model can see than on what has to pass through it. Each feature trims a different part of that flow, so they work as a pair. The drop from 564,000 to 30,000 input tokens, with the same correct count, illustrates that distinction. We would treat the headline saving as a best case, since it comes from a single demo on invented data. Before copying the setup, weigh what model-written code is allowed to do to your live systems.
Based on its experiments, Prompt Engineering recommends pairing retrieval with code mode for agents with many tools and data-heavy tasks, not as a requirement for every agent. If you are building agents that work across APIs, Gemini tool retrieval with code mode is worth exploring in more detail to reduce how much those tasks put into context.
Media Credit: Prompt Engineering
Disclosure: Some of our articles include affiliate links. If you buy something through one of these links, Geeky Gadgets may earn an affiliate commission. Learn about our Disclosure Policy.