
在 Gemini API 中引入工具检索
Gemini API 推出 Tool Retrieval 预览:工具库待命,模型按名请求才加载参数架构,减少输入 token 并保持工具选择准确。
工具检索功能现已在 Gemini Interactions API 中开放预览。按需加载工具参数架构,无需在每次提示中发送完整的工具库,旨在减少输入 token 数量,并在智能体规模扩展时保持工具选择的准确性。
今天,我们在 Gemini API 中推出工具检索(Tool Retrieval)功能,现已在 Interactions API 中开放预览。
当智能体连接数十甚至数百个工具时,每次请求都发送完整的参数架构会迅速累积开销。这会推高输入 token 数量、增加延迟,并且随着候选列表增长而降低工具选择的准确性。工具检索让你的智能体将完整工具库保持在待命状态,仅在当前任务需要时才加载参数架构。
工作原理
要启用工具检索,请在请求中添加一个 tool_search 工具,并在你希望暂缓加载的工具上设置 defer_loading: true。模型仍能看到每个暂缓加载工具的名称和描述,因此知道有哪些工具可用,但参数架构不会进入上下文。当模型需要某个工具时,它会按名称请求该工具,API 随即加载完整架构,模型再调用该函数——这一切都在同一请求内完成。
from google import genai
client = genai.Client()
tools = [
{"type": "tool_search"},
{
"type": "function",
"name": "issue_refund",
"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"],
},
},
# ... 50+ other deferred functions (leave frequent tools undeferred)
]
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="Order A-1042 arrived damaged. Please refund 40 dollars.",
tools=tools,
)
在这单次 API 调用中,模型会搜索 issue_refund,API 会加载其 schema,随后模型发出 function_call。这三个步骤都会出现在 interaction.steps 中,因此在调试工具名称或描述时,可以很方便地查看模型考虑过哪些工具:
"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}
}
]
从这里开始就是常规的函数调用:你的代码执行退款,并返回一个 function_result。
无需构建或托管索引。Gemini 在服务器端执行搜索,因此你的代码只需新增这个工具,并在想要延迟加载的工具上设置一个标志。你可以把最常用的工具预先加载,而将长尾工具延迟加载。
Tool Retrieval 与前缀缓存也能很好地配合。延迟加载的 schema 不会出现在提示词开头,模型加载的 schema 是在对话中较后位置加入的,因此缓存前缀在各轮之间保持不变。
Tool Retrieval 也适用于 Gemini API 中的远程 MCP 服务器。在 mcp_server 工具上设置 defer_loading: true,该服务器暴露的每个工具都会按需加载,因此你可以连接一个大型服务器,而无需逐一列出其工具。
客户端检索的工作原理
如果你维护自己的自定义检索机制、嵌入索引或按用户权限过滤,请将 tool_search 声明为 "execution": "client",并定义你的搜索参数。客户端检索每次搜索会增加一次往返,因为查找由你的应用执行。工具无需预先声明,因此当用户连接某个集成时,你也可以在对话中途暴露新工具:
tools = [{
"type": "tool_search",
"execution": "client",
"name": "search_tools",
"description": "Search the internal tool registry for functions.",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
}]
# When the model emits a function_call for "search_tools",
# return matching {"type": "function", ...} schemas in function_result:
followup = client.interactions.create(
model="gemini-4-hydrogen",
previous_interaction_id=interaction.id,
tools=tools,
input=[{
"type": "function_result",
"name": "search_tools",
"call_id": step.id,
"result": my_registry.search(**step.arguments),
}],
)
定价与支持的模型如何运作
工具检索不单独收费。输入和输出 token 按你所调用模型的标准费率计费。开启 tool_search 会增加一小块固定的输入 token,而模型加载的每个 schema 都会在后续对话中作为输入计费,与预先声明的工具相同。当未加载的 schema 多于已加载的 schema 时,你就能节省 token,这在大型工具声明中通常是常见情况。请在你自己的流量上测试,以找到适合你配置的即时工具与延迟工具之间的最佳平衡。
工具检索适用于 Gemini 3.6 及更高版本的模型,包括 Interactions API 中的所有 Gemini 4 模型。
如何开始
- 阅读文档 — 包含示例的完整设置指南
- 比较 Gemini 模型 — 为你的用例找到最佳模型
- 加入 Discord 上的讨论 — 与团队分享反馈