Gemini API 支持对话中途动态更新系统指令与工具
今天,对话中途修改 system instruction 和工具的功能已在 Gemini API 的 Interactions API 中开放预览。这让 agent 无需重新读取整个会话或推高成本,就能在会话进行中适应新规则和新工具。
长期运行的 agent 往往要跨越数十甚至数百轮对话,期间规则、权限和可用工具都可能发生变化。在此之前,如果不让缓存失效,你就无法给模型下发新的 system instruction 或更新工具。
开发者通常靠两种变通办法:
- 把指令放进用户消息:为了保住缓存,可以把新的 system instruction 作为用户消息发送。但这类消息的效力不如顶层的 system instruction,而且用户可能尝试模仿它们。
- 重写 system instruction 或工具:这样做会使 prompt 缓存失效,模型要重新处理整个对话历史,带来额外的成本和延迟。
现在,Interactions API 允许你在任意一轮对话中发送一个 system_input 步骤来修改 system instruction 或工具。它与顶层 system instruction 具有同等效力,且优先于用户消息,同时之前的对话轮次仍保留在缓存中。
工作原理
要在对话中途发送指令,只需在 previous_interaction_id 中传入上一轮的 ID,并在输入数组中把 system_input 步骤直接放在 user_input 之前。同时保持顶层 system_instruction 在各轮之间不变,这样 prompt 前缀在缓存中依然有效:
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="Latency just spiked on our service. What should I check first?",
)
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": (
"A production deployment freeze is now active. "
"Recommend read-only diagnostics and do not allow further rollouts."
),
}
],
},
{
"type": "user_input",
"content": [
{
"type": "text",
"text": "Give me the command to deploy a new release."
}
],
},
],
)
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": "A production deployment freeze is now active. Recommend read-only diagnostics and do not allow further rollouts."
}
]
},
{
"type": "user_input",
"content": [
{
"type": "text",
"text": "Give me the command to deploy a new release."
}
]
},
{
"type": "model_output",
"content": [
{
"type": "text",
"text": "A production deployment freeze is active, so we can't release. Here are some checks you can run..."
}
]
}
]
}
由于 system_input 是在当前轮次的边界处追加的,之前所有 token 在各轮之间保持不变,因此可以复用 prompt cache。你可以在每轮的 user_input 之前发送一个 system_input 步骤,内容可以是文本、工具更新,或两者兼有。
对话中途更新工具的机制
工具声明位于 prompt 的开头,所以如果在轮次之间修改顶层的 tools 列表,会导致 prompt cache 失效。使用 system_input 时,我们建议每轮都保持相同的顶层 tools 列表,通过 added_tools 和 removed_tools 来随会话进展更新模型可调用的函数。目前仅支持通过 added_tools 和 removed_tools 更新函数工具,暂不支持 MCP server 工具。
在下面这个例子中,客服 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 中开启公开预览,覆盖 所有 Gemini 4 模型(通过 v1beta 端点),并已在 Gemini API 中可用。对 Gemini Enterprise Agent Platform 的支持即将推出。
立即上手:阅读文档,对比各 Gemini 模型为你的工作负载找到合适的 Gemini 4 模型,并加入 AI Developer Forum 参与讨论。