@_philschmid: https://x.com/_philschmid/status/2123806307442401577
Summary
Google shipped Dynamic Tool Retrieval (Tool Search Tool) in preview for the Gemini API, letting agents keep hundreds of tools on standby with only compact descriptions upfront and load full parameter schemas on demand, supporting both built-in server-side retrieval and custom client-side retrieval stacks.
View Cached Full Text
Cached at: 11/21/26, 12:09 AM
Dynamic tool retrieval in the Gemini API
We just shipped Dynamic Tool Retrieval (the Tool Search Tool) in preview for the Gemini API.
Dynamic tool retrieval lets your agent keep hundreds of tools on standby, see only compact names and descriptions upfront, and load full parameter schemas on demand when a task needs them. Sending 100+ full tool schemas on every turn burns input tokens and hurts tool selection accuracy. This fixes that.
How it works
Dynamic tool retrieval supports two modes, depending on whether you want Gemini to handle search for you or plug in your own retrieval stack:
-
Built-in server-side retrieval: add {“type”: “tool_search”} to your tools list and set defer_loading: true on functions or remote MCP servers. Gemini keeps the full parameter schemas out of the initial prompt and only sees a one-line summary per tool (about 26 tokens, or even smaller with short_description). Leave your 3 to 5 most frequent tools loaded and defer the long tail. When the model needs a deferred tool, it searches, loads the schema inline, and emits the function_call inside a single API request, keeping your prompt prefix cacheable across turns.
-
Custom client-side retrieval: declare a tool_search tool with “execution”: “client” and pick any name, description, and parameters you want. Your functions live completely externally (no defer_loading or upfront declarations in tools[]). When the model calls your search tool, you run whatever logic fits your stack, such as vector search, per-user permission filtering, or scoring candidate tools in parallel with the Gemini Decisions API (gemini-decision-1) at zero output tokens, then return the matching schemas inside function_result.
Example 1: Built-in server-side retrieval
Put the initial call and every follow-up turn inside one while loop. Check interaction.steps for function_call steps rather than relying on status, execute them locally, and pass the function_result items back with previous_interaction_id.
The API does not carry tool declarations across turns, even with previous_interaction_id, so always resend the full tools array on every request. Pass apiVersion: “v1beta” when initializing @google/genai while defer_loading is in preview.
typescriptimport { GoogleGenAI } from “@google/genai”;
const ai = new GoogleGenAI({ apiVersion: “v1beta” });
const tools = [ { type: “tool_search” as const }, { type: “function” as const, name: “issue_refund”, description: “Refund an order to the original payment method with optional partial amount and reason code.”, short_description: “Refund an order to the original payment method.”, defer_loading: true, parameters: { type: “object”, properties: { order_id: { type: “string” }, amount: { type: “number” }, }, required: [“order_id”, “amount”], }, }, { type: “mcp_server” as const, name: “order_service”, url: “https://example.com/mcp”, defer_loading: true, }, ];
function runLocalTool(name: string, args: Record<string, unknown>) {
if (name === “issue_refund”) {
return { status: “refunded”, order_id: args.order_id, amount: args.amount };
}
return { error: Unknown tool: ${name} };
}
let nextInput: any = “Order A-1042 arrived damaged. Check the order and refund 40 dollars.”; let previousId: string | undefined; let interaction;
while (true) { interaction = await ai.interactions.create({ model: “gemini-4-neon”, previous_interaction_id: previousId, input: nextInput, tools, }); previousId = interaction.id;
const functionCalls = (interaction.steps ?? []).filter( (s) => s.type === “function_call” ); if (functionCalls.length === 0) break;
nextInput = functionCalls.map((fc) => ({ type: “function_result” as const, name: fc.name, call_id: fc.id, result: [ { type: “text” as const, text: JSON.stringify( runLocalTool(fc.name!, (fc.arguments ?? {}) as Record<string, unknown>) ), }, ], })); }
console.log(interaction.output_text);
Inside the first response, steps shows the tool_search_call, the loaded tool_search_result, and the final function_call. You only execute your own function_call; the server handles the search steps for you. If you run stateless (store: false), send every step back unchanged, including the search steps and their signature fields.
typescript{ “id”: “int_server_111”, “status”: “requires_action”, “steps”: [ { “type”: “tool_search_call”, “id”: “tsc_7f2a”, “arguments”: {“function_names”: [“issue_refund”]}, “signature”: “…” }, { “type”: “tool_search_result”, “call_id”: “tsc_7f2a”, “result”: [{“type”: “function”, “name”: “issue_refund”, “parameters”: {“…”: “…”}}], “signature”: “…” }, { “type”: “function_call”, “id”: “fc_91c3”, “name”: “issue_refund”, “arguments”: {“order_id”: “A-1042”, “amount”: 40} } ] }
Example 2: Build your own retriever with the Decisions API
When you want full control over how tools are ranked or filtered, declare a client-side tool_search with “execution”: “client” and any custom name you choose. Your full catalog stays outside tools[].
Here is the same agent loop using the Gemini Decisions API (gemini-decision-1) to score every candidate tool in parallel in one forward pass with zero output tokens (noul yes/no probabilities from 0.01 to 0.99), then inject only the relevant schemas into function_result.result:
typescriptimport { GoogleGenAI } from “@google/genai”;
const ai = new GoogleGenAI({ apiVersion: “v1beta” });
// External tool catalog (never sent in tools[] upfront) const TOOL_REGISTRY = { get_billing_history: { type: “function” as const, name: “get_billing_history”, description: “Retrieve invoices and payment history for a user.”, parameters: { type: “object”, properties: { user_id: { type: “integer” } }, required: [“user_id”], }, }, get_shipment_status: { type: “function” as const, name: “get_shipment_status”, description: “Track package delivery status and carrier updates.”, parameters: { type: “object”, properties: { order_id: { type: “string” } }, required: [“order_id”], }, }, // … 100+ other tools in your external registry };
async function searchRegistryWithDecisions(query: string, threshold = 0.5) {
const questions = Object.fromEntries(
Object.entries(TOOL_REGISTRY).map(([name, schema]) => [
name,
{
type: “noul” as const,
instructions: Is the tool '${name}' (${schema.description}) needed for this request?,
},
])
);
const decision = await ai.decisions.create({ model: “gemini-decision-1”, input: query, questions, });
return Object.entries(decision.answers ?? {}) .filter(([, ans]) => ans.type === “noul” && (ans.noul ?? 0) >= threshold) .map(([name]) => TOOL_REGISTRY[name as keyof typeof TOOL_REGISTRY]); }
const tools = [ { type: “tool_search” as const, execution: “client” as const, name: “search_tool_registry”, // any name you want description: “Search the internal tool registry for billing, shipping, or account tools.”, parameters: { type: “object”, properties: { query: { type: “string” } }, required: [“query”], }, }, ];
let nextInput: any = “Look up the billing history and invoices for user 123.”; let previousId: string | undefined; let interaction;
while (true) { interaction = await ai.interactions.create({ model: “gemini-4-neon”, previous_interaction_id: previousId, input: nextInput, tools, // stays unchanged across turns }); previousId = interaction.id;
const functionCalls = (interaction.steps ?? []).filter( (s) => s.type === “function_call” ); if (functionCalls.length === 0) break;
nextInput = await Promise.all( functionCalls.map(async (fc) => { if (fc.name === “search_tool_registry”) { // Inject matching full function schemas directly in function_result.result const query = String((fc.arguments as { query?: string })?.query ?? “”); const matches = await searchRegistryWithDecisions(query); return { type: “function_result” as const, name: fc.name, call_id: fc.id, result: matches.length > 0 ? matches : [{ type: “text” as const, text: “No matching tools found.” }], }; }
// Execute the newly discovered tool normally
const output = {
user_id: 123,
invoices: [{ id: "inv_9", amount: 49 }],
};
return {
type: "function_result" as const,
name: fc.name,
call_id: fc.id,
result: [{ type: "text" as const, text: JSON.stringify(output) }],
};
})
); }
console.log(interaction.output_text);
Because you own the search, Gemini emits a standard function_call using your custom name (search_tool_registry) instead of a server-side tool_search_call. Across the wire, the steps look like this:
typescript// Turn 1 response step: model calls your custom search tool { “type”: “function_call”, “id”: “tsc_abc123”, “name”: “search_tool_registry”, “arguments”: {“query”: “billing history and invoices”} }
// Turn 2 input: your app injects the matching schemas from the Decisions API { “type”: “function_result”, “name”: “search_tool_registry”, “call_id”: “tsc_abc123”, “result”: [ { “type”: “function”, “name”: “get_billing_history”, “description”: “Retrieve invoices and payment history for a user.”, “parameters”: { “type”: “object”, “properties”: {“user_id”: {“type”: “integer”}}, “required”: [“user_id”] } } ] }
// Turn 2 response step: model calls the tool you just injected { “type”: “function_call”, “id”: “fc_456def”, “name”: “get_billing_history”, “arguments”: {“user_id”: 123} }
Your tools[] array stays unchanged the entire time, so your prompt prefix stays cacheable. Because your app runs the lookup, each client-side search adds one round trip.
When to use it
Tool retrieval has no extra fee and uses standard token pricing. For fewer than 20 tools, static loading is still simpler and avoids the extra search pass. Above that, deferring tools cuts input tokens sharply, and client-side mode lets you combine dynamic tool injection with your own vector search or Gemini Decision 1 routing.
Read the full guide, error reference, and token math in the Gemini API Tool Retrieval docs.
Similar Articles
Introducing Tool Retrieval in the Gemini API
Google 推出 Gemini API 的 Tool Retrieval 预览功能,允许智能体按需加载工具参数 schema,从而减少输入 token、降低延迟并提升大规模工具库下的工具选择准确率。
@_philschmid: https://x.com/_philschmid/status/2122358498461691913
Google shipped Gemini 4 Carbon and Gemini 4 Hydrogen reasoning/coding models alongside a new Gemini Decisions API, self-hosted agent sandboxes in Gemini Managed Agents, and additional robotics and genomics models.
Gemini Tool Retrieval: check authorization again before execution
Google announced Gemini Tool Retrieval in preview, and this piece outlines implementation details and proposed security checks for verifying authorization boundaries between tool-schema discovery and actual execution in agent workflows.
@_philschmid: We shipped a small Gemini API Update! You can now combine Google Maps and Google Search tool at the same time with Gemi…
Google shipped a small Gemini API update that now lets developers combine Google Maps and Google Search tools simultaneously with Gemini to build location-based apps.
@_philschmid: I'm excited to introduce Managed Agents in the Gemini API. One API call gives you a full agent with code execution, web…
Phil Schmid announces Managed Agents in the Gemini API, enabling one-call agents with code execution, web browsing, and file management in isolated sandboxes, powered by Gemini 3.5 Flash.