Google 在 Gemini API 的 Interactions API 中推出 Tool Retrieval 预览
Google 在 Gemini API 推出 Tool Retrieval 预览
Google 在 Gemini API 的 Interactions API 中以预览形式推出 Tool Retrieval,让智能体按需加载工具参数 schema,而不是每次请求都发送完整工具库。
Google Blog:AI·2026-11-20 02:00· 2 小时前精选AI 评分62
在 Gemini API 中引入 Tool Retrieval
AI 导读
Google 在 Gemini API 的 Interactions API 中以预览形式推出 Tool Retrieval,让智能体按需加载工具参数 schema,而不是每次请求都发送完整工具库,以减少输入 token 并在工具数量扩大时保持选择准确。
推荐理由
原文给出按需加载工具 schema 的机制、代码示例和计费方式,可帮助开发者评估大型工具库下的 token 与准确性取舍。
2026年11月19日
|
Tool Retrieval 现已在 Gemini Interactions API 中开放预览。按需加载工具参数 schema,无需在每次提示词中发送完整的工具库,旨在减少输入 token,并在智能体规模扩大时保持工具选择的准确性。
Lucia Loher
Gemini API 产品经理
Nora Kassner
Google DeepMind 研究科学家
您的浏览器不支持音频元素。
收听文章
[[duration]] 分钟
本内容由 Google AI 生成。生成式 AI 仍处于实验阶段
今天我们推出 Gemini API 中的 Tool Retrieval,现已在 Interactions API 中开放预览。
当智能体接入几十甚至上百个工具时,每次请求都发送完整的参数 schema,开销会迅速累积:输入 token 数被推高、延迟增加,而且候选列表越长,工具选择的准确性越低。Tool Retrieval 让智能体把完整的工具库放在待命状态,只在当前任务需要时才加载参数 schema。
工作原理
要启用 Tool Retrieval,在请求中添加一个 tool_search 工具,并在你希望暂缓加载的工具上设置 defer_loading: true。模型仍然能看到每个延迟工具的 name 和 description,因此知道有哪些工具可用,但参数 schema 不会进入上下文。当模型需要某个工具时,它会按名称请求,API 加载完整 schema,模型再调用该函数,整个过程都在同一个请求内完成。
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,该服务器暴露的每个工具都会按需加载,这样你就能连接一个大型服务器,而无需逐个列出其中的工具。
客户端检索如何工作
如果你维护自己的工具注册表、嵌入索引或按用户权限过滤,可以用 "execution": "client" 声明 tool_search 并定义搜索参数。客户端检索每次搜索会多一次往返,因为查找由你的应用执行。工具无需预先声明,因此当用户连接某个集成时,你也可以在对话中途暴露新工具:
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),
}],
)定价与支持的模型
Tool Retrieval 不单独收费。输入和输出 token 按你所调用模型的标准费率计费。开启 tool_search 会增加一小块固定的输入 token,模型加载的每个 schema 在后续对话中都会作为输入计费,与预先声明的工具相同。当从未加载的 schema 多于实际加载的 schema 时,你就能节省 token,对于大型工具库来说这通常是常态。请结合你自己的流量进行衡量。
工具检索功能可在 Interactions API 中配合 Gemini 3.6 及更高版本模型使用,包括所有 Gemini 4 模型。
如何开始
- 阅读文档——包含示例的完整设置指南
- 对比 Gemini 模型——找到最适合你使用场景的模型
- 加入 Discord 讨论——与团队分享反馈
在收件箱中获取 Google 最新资讯
订阅我们的新闻邮件,获取产品更新、活动信息、特别优惠等更多内容。
你的信息将按照Google 隐私政策使用。你可以随时退订。
来源:Google Blog:AI · blog.google
来源:RadarAI · aihot.news