报道正文
开发者现在可以在对话进行中更新系统指令或工具,同时保持提示缓存不变。

你的浏览器不支持音频元素。
收听文章 [[duration]] 分钟
今天,对话中途系统指令与工具变更功能已在 Gemini API 中通过 Interactions API 提供预览。这让智能体能够在会话中途适应新的规则和工具,而无需重新读取整个对话,也不会推高成本。
长时间运行的智能体往往要处理数十甚至数百轮对话。在此过程中,规则、权限和可用工具会发生变化。在此之前,你无法在不使缓存失效的情况下,向模型提供新的系统指令或更新工具。
开发者通常依赖两种变通方案:
- 将指令放入用户消息: 为保留缓存,你可以将新的系统指令作为用户消息发送。这类指令不具备顶层系统指令同等的权威性,并且允许用户尝试模仿它们。
- 重写系统指令或工具: 重写系统指令或更改工具会使提示缓存失效。模型会重新处理整个对话历史,从而增加成本和延迟。
Interactions API 现在允许你在任意轮次发送一个 system_input 步骤,以携带系统指令或工具变更。它与你的顶层系统指令具有同等权威,并优先于用户消息,同时此前的轮次仍保持缓存。

工作原理
要在对话中途发送指令,请在 previous_interaction_id 中传入上一轮的 ID,并将 system_input 步骤直接放在输入数组中的 user_input 之前。在各轮次之间保持顶层 system_instruction 不变,以便提示前缀在缓存中保持有效:
from google import genai
client = genai.Client(http_options={"api_version": "v1beta"})
system_instruction = "You are an infrastructure troubleshooting assistant."turn1 = client.interactions.create( model="gemini-4-neon", system_instruction=system_instruction, input="我们服务的延迟刚刚飙升了。我应该先检查什么?", )
turn2 = client.interactions.create( model="gemini-4-neon", previous_interaction_id=turn1.id, system_instruction=system_instruction, input=[ { "type": "system_input", "content": [ { "type": "text", "text": ( "生产部署冻结现已生效。" "建议仅进行只读诊断,不允许进一步发布。" ), } ], }, { "type": "user_input", "content": [ { "type": "text", "text": "给我部署新版本的命令。", } ], }, ], ) print(turn2.output_text)
每个 `system_input` 都会作为独立的步骤追加到对话历史中,并在整个会话的剩余部分与顶层的 `system_instruction` 一同保持生效。涉及不同主题的规则会同时生效。当较新的 `system_input` 与较早的指令冲突时,模型会优先采用最新的指令。
当你使用 `client.interactions.get(id=turn2.id)` 检索一次交互时,每个 `system_input` 步骤都会按顺序与 `user_input` 和 `model_output` 一同出现,使多轮 agent 轨迹的审计和调试变得简单直接:
{ "steps": [ { "type": "system_input", "content": [ { "type": "text", "text": "生产部署冻结现已生效。建议仅进行只读诊断,不允许进一步发布。" } ] }, { "type": "user_input", "content": [ { "type": "text", "text": "给我部署新版本的命令。" } ] }, { "type": "model_output", "content": [ { "type": "text", "text": "生产部署冻结已生效,因此我们无法发布。以下是一些你可以运行的检查……" } ] } ] }
由于 `system_input` 在当前轮次的边界处追加,所有先前的 token 在各轮之间保持完全一致,从而复用提示缓存。你可以在每一轮中紧接在 `user_input` 之前发送一个 `system_input` 步骤,其中包含文本内容、工具更新,或两者兼有。
## 对话中途的工具更新如何工作
工具声明位于 prompt 的开头,因此在多轮对话之间更改顶层 tools 列表会破坏 prompt 缓存。使用 `system_input` 时,我们建议你在每一轮都保留相同的顶层 tools 列表,并使用 `added_tools` 和 `removed_tools` 来更新模型在会话演进过程中可以调用的函数。目前仅支持通过 `added_tools` 和 `removed_tools` 更新函数工具,不支持 MCP 服务器工具。
在这个示例中,一个客服 agent 初始拥有 `get_store_hours` 和 `issue_refund`。在下一轮中,应用移除 `issue_refund` 并添加 `create_escalation_ticket`,同时保持顶层 `tools` 列表不变:
from google import genai client = genai.Client(http_options={"api_version": "v1beta"}) tools = [ { "type": "function", "name": "get_store_hours", "description": "Look up opening hours for a store.", "parameters": { "type": "object", "properties": {"store_id": {"type": "string"}}, "required": ["store_id"], }, }, { "type": "function", "name": "issue_refund", "description": "Refund an order.", "parameters": { "type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"], }, }, ] turn1 = client.interactions.create( model="gemini-4-neon", input="Hi, I have a question about my order.", tools=tools, ) turn2 = client.interactions.create( model="gemini-4-neon", previous_interaction_id=turn1.id, tools=tools, input=[ { "type": "system_input", "content": [ { "type": "text", "text": ( "This account is under billing review. Explain " "that refunds are paused and offer to escalate." ), } ], "removed_tools": [{"type": "function", "name": "issue_refund"}], "added_tools": [ { "type": "function", "name": "create_escalation_ticket", "description": "Escalate a billing review issue to support.", "parameters": { "type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"], }, } ], }, { "type": "user_input", "content": [{"type": "text", "text": "Refund order 12345."}], }, ], ) print(turn2.output_text)
工具更新会一直生效,直到后续的 `system_input` 将其更改。当单个步骤同时包含这两个字段时,API 会先处理 `removed_tools`,再处理 `added_tools`,从而让你可以就地替换某个工具定义。在移除工具时附加一段简短的文字说明,也能为模型提供上下文,让它解释某项操作为何不可用。
请记住,`system_input`、`added_tools` 和 `removed_tools` 会引导模型行为,并充当编排控制手段。你的应用仍应在后端对每一次函数调用进行授权后再执行,并确保不可信的外部内容或最终用户内容不会进入 `system_input`。
## 如何使用
**依赖会话的约束:** 应用在对话进行到中途才发现的规则,例如工作区设置、账户权益或支出限额。
**长程指令强化:** 随着上下文增长,在多轮会话的深处再次强调关键的格式、语气或策略规则。
**动态权限与模式切换:** 在身份验证或方案获批后启用写入类或账户操作类工具,并在回到只读审阅时将其移除。
**实时运营变更:** 当下游依赖项离线时,在会话中途禁用某个工具并更新指引,待服务恢复后再将其恢复。
**大型工具目录**:先从小规模工具集开始,随着任务需要再逐步添加,无需预先声明所有工具,也不会破坏缓存。
## 定价与支持的模型如何运作
对话中途的系统指令不收取单独费用。`system_input` 步骤中的 token 按你所调用模型的标准输入 token 费率计费。当规则或工具发生变化时,此前的轮次仍可享受缓存 token 费率。
对话中途的系统指令已在 Interactions API(通过 [v1beta 端点](https://aistudio.google.com/api-reference/api-versions))中面向[所有 Gemini 4 模型](https://aistudio.google.com/docs/latest-model)开放公开预览,同时也已在 [Gemini API](https://aistudio.google.com/docs/mid-conversation-instructions) 中提供。对 [Gemini Enterprise Agent Platform](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/capabilities/interactions) 的支持即将推出。
立即开始使用:阅读[文档](https://aistudio.google.com/docs/mid-conversation-instructions),[对比 Gemini 模型](https://aistudio.google.com/docs/choosing-a-model)以找到适合你工作负载的 Gemini 4 模型,并[加入 AI 开发者论坛的讨论](https://discuss.ai.google.dev/)。