跪拜 Guibai
← All articles
AI Programming · Agent · Gemini

Gemini's Tool Retrieval: Load Agent Tool Schemas On Demand to Cut Tokens and Latency

By 可夫小子 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

For teams integrating hundreds of business endpoints and MCP servers into agents, Tool Retrieval removes the forced trade-off between paying for every schema in every request and building a custom tool-routing layer. It also keeps multi-turn prefix caching intact, which is a key cost control that naive RAG-based tool search tends to break.

Summary

Google's new Tool Retrieval feature, now in preview in the Gemini API, tackles the classic agent headache: ballooning input tokens and slower first-token latency when an agent carries dozens or hundreds of tools. Every request used to send full JSON parameter schemas for all tools, even those never used. Tool Retrieval changes that by letting developers mark long-tail tools with `"defer_loading": true` and add `{"type": "tool_search"}` to the request. The model initially sees only names and short descriptions, and loads a full schema only when it decides a tool is relevant, all within a single API request.

The mechanism runs in three logged steps: `tool_search_call`, `tool_search_result`, and the eventual `function_call`. Server-side search is handled by Gemini, so no custom vector index is needed. The approach also preserves prefix caching across multi-turn conversations, because lazily loaded schemas are appended later in the prompt rather than injected into the system prefix. For MCP-heavy setups, marking the `mcp_server` tool with `defer_loading` automatically puts all its sub-tools on demand. An optional `"execution": "client"` mode hands search back to the developer's backend for permission filtering, at the cost of an extra network round-trip.

Pricing stays standard per-token; there's no separate fee. But the savings only materialize when the schemas never used outnumber those actually loaded. Google recommends testing with real traffic. The feature is restricted to the newer Interactions API, only one `tool_search` per request is allowed, and forgetting it alongside `defer_loading` triggers a 400 error. Tool naming and descriptions matter more than before, since the model decides which tools to retrieve based only on those brief hints.

Takeaways
— Tool Retrieval is a preview feature in the Gemini API, built into the Interactions API (v1beta), and supports Gemini 3.6 and later, including Gemini 4.
— Enable it by adding `{"type": "tool_search"}` to the tools array and marking functions or tools with `"defer_loading": true`.
— Deferred tools initially expose only their names and short descriptions; full parameter schemas are loaded on demand.
— The lookup flow is `tool_search_call` → `tool_search_result` → `function_call`, and all steps are logged in `interaction.steps`.
— Gemini manages server-side search automatically, so no vector index or custom retrieval infrastructure is required.
— Setting `defer_loading: true` on an `mcp_server` tool definition makes all its sub-tools lazy-load without manual listing.
— The `"execution": "client"` mode lets developers filter tools by user permissions, but adds one network round-trip per search.
— There is no separate fee; tokens are billed at the model's standard rate. Savings require more never-used schemas than actually loaded schemas.
— Only one `tool_search` is allowed per request, and using `defer_loading` without it returns a 400 error.
— The feature works only with the newer Interactions API, not the legacy `generateContent` endpoint.
— Clear tool names and descriptions are critical, because the model only sees those brief hints when deciding which tool to retrieve.
Conclusions

The prefix-caching preservation is the architectural win: unlike naive RAG-based tool search, which mutates the prompt prefix and busts caching, Tool Retrieval keeps the core prefix stable, making it viable even when raw token savings are modest.

The feature is not a universal win; for agents with only a handful of tools or high usage rates of all tools, the fixed cost of `tool_search` and schema re-loading can exceed the savings.

It shifts a non-trivial burden back onto developers: tool retrieval quality now depends on naming and description discipline, so sloppy metadata cancels out the infrastructure benefits.

The client-execution escape hatch acknowledges real enterprise permission requirements, but the added network round-trip per search is a trade-off that may matter for latency-sensitive agents.

Concepts & terms
tool_search
A marker added to the tools array in a Gemini API request that enables on-demand loading of deferred tool schemas. Without it, deferred tools trigger a 400 error.
defer_loading
A boolean flag on a tool definition that keeps its full parameter schema out of the initial request context, leaving only the name and description until the model actually retrieves it.
prefix caching
A mechanism that caches the computation for the fixed initial part of a prompt, cutting cost and latency. It fails when the prompt's beginning changes between turns, which is why Tool Retrieval appends loaded schemas later rather than at the start.
Interactions API
The newer Gemini API surface (v1beta) that supports Tool Retrieval, as opposed to the older `generateContent` endpoint. It exposes a structured, step-level interaction log (`interaction.steps`).
Source: juejin.cn ↗ Google Translate ↗ Backup ↗