Gemini API 推出对话中途指令功能

查看产品与平台中的全部内容
查看创新与 AI 中的全部内容
查看产品与平台中的全部内容
在 Gemini API 中推出对话中途指令
在 Gemini API 中推出对话中途指令
开发者现在可以在对话中途更新系统指令或工具,同时保持提示缓存不变。
Gemini API 产品经理
Google Cloud 产品经理
您的浏览器不支持音频元素。
今天,会话中途的系统指令和工具变更已可通过 Interactions API 在 Gemini API 中预览使用。这让智能体能够在不重新读取整个对话、也不推高成本的情况下,在会话中途适应新的规则和工具。
长时间运行的智能体往往要跨越多达数十甚至数百轮的交互。在此过程中,规则、权限和可用工具都会发生变化。在此之前,你一旦要给出新的系统指令或更新工具,就会导致缓存失效。
开发者通常依赖两种变通做法:
把指令放进用户消息:为了保留缓存,你可以把新的系统指令作为用户消息发送。但这类指令不具备顶层系统指令同等的权重,而且用户可能尝试模仿它们。
重写系统指令或工具:重写系统指令或更改工具会使提示缓存失效。模型需要重新处理整个对话历史,从而增加成本和延迟。
Interactions API 现在允许你在任意一轮发送 system_input 步骤,以变更系统指令或工具。它具备与顶层系统指令同等的权威,并优先于用户消息,同时此前的轮次仍保持缓存。
要在会话中途发送指令,请在 previous_interaction_id 中传入上一轮的 ID,并在 input 数组中把 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="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 一起按顺序出现,让多轮智能体轨迹的审计和调试变得简单直接:
{ "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 在每一轮之间都保持完全一致,从而复用 prompt cache。你可以在每一轮的 user_input 之前直接发送一个 system_input 步骤,其中包含文本内容、工具更新,或两者兼有。
对话中途的工具更新如何生效
工具声明位于 prompt 的开头,因此在轮次之间更改顶层 tools 列表会破坏 prompt cache。使用 system_input 时,我们建议你在每一轮都保留相同的顶层 tools 列表,并使用 added_tools 和 removed_tools 来更新模型在会话演进过程中可以调用的函数。目前仅支持通过 added_tools 和 removed_tools 更新函数工具,不支持 MCP server 工具。
在本示例中,一个支持类智能体最初拥有 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": "查询商店的营业时间。", "parameters": { "type": "object", "properties": {"store_id": {"type": "string"}}, "required": ["store_id"], }, }, { "type": "function", "name": "issue_refund", "description": "为订单退款。", "parameters": { "type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"], }, }, ] turn1 = client.interactions.create( model="gemini-4-neon", input="你好,我有一个关于订单的问题。", 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": ( "该账户正在接受账单审核。解释 " "退款已暂停,并提议升级处理。" ), } ], "removed_tools": [{"type": "function", "name": "issue_refund"}], "added_tools": [ { "type": "function", "name": "create_escalation_ticket", "description": "将账单审核问题升级至支持团队。", "parameters": { "type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"], }, } ], }, { "type": "user_input", "content": [{"type": "text", "text": "退款订单 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 费率。
对话中系统指令现已在 Gemini API 的 Interactions API(通过 v1beta endpoint)中面向所有 Gemini 4 模型开放公开预览。对 Gemini Enterprise Agent Platform 的支持即将推出。
现在就可以开始:阅读文档,对比 Gemini 模型以找到适合你工作负载的 Gemini 4 模型,并加入 AI Developer Forum 的讨论。
译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。
查看英文原文(Introducing mid-conversation instructions in the Gemini API)
See all in Products & platforms
See all in Innovation & AI
See all in Products & platforms
Introducing mid-conversation instructions in the Gemini API
Introducing mid-conversation instructions in the Gemini API
Developers can now update system instructions or tools in the middle of a conversation while keeping the prompt cache intact.
Product Manager, Gemini API
Product Manager, Google Cloud

Your browser does not support the audio element.
Today, mid-conversation system instruction and tool changes are available in preview in the Gemini API through the Interactions API. This lets agents adapt to new rules and tools mid-session without re-reading the entire conversation or driving up costs.
Long-running agents often work across dozens, if not hundreds, of turns. Along the way, rules, permissions and available tools change. Until now, you couldn't give the model a new system instruction or update tools without invalidating the cache.
Developers have commonly relied on two workarounds:
Putting instructions in a user message: To preserve the cache, you could send new system instructions as a user message. These don’t carry the same weight as your top-level system instructions and allow users to try and imitate them.
Rewriting system instruction or tools: Rewriting the system instruction or changing tools invalidates the prompt cache. The model reprocesses the entire conversation history, adding cost and latency.
The Interactions API now allows you to send a system_input step at any turn with system instruction or tool changes. It carries the same authority as your top-level system instruction and takes precedence over user messages, while earlier turns stay cached.
To send an instruction mid-conversation, pass the previous turn's ID in previous_interaction_id and place a system_input step directly before user_input in the input array. Keep your top-level system_instruction unchanged across turns so the prompt prefix stays valid in the cache:
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)
Each system_input appends to the conversation history as its own step and stays active for the rest of the session alongside the top-level system_instruction. Rules that cover different topics remain in effect together. When a newer system_input conflicts with an earlier instruction, the model favors the most recent instruction.
When you retrieve an interaction with client.interactions.get(id=turn2.id), each system_input step appears in order alongside user_input and model_output, making multi-turn agent traces straightforward to audit and debug:
{
"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..."
}
]
}
]
}
Because system_input appends at the current turn boundary, all preceding tokens remain identical from turn to turn and reuse the prompt cache. You can send one system_input step per turn directly before user_input, containing text content, tool updates, or both.
How mid-conversation tool updates work
Tool declarations sit at the start of the prompt, so changing the top-level tools list between turns breaks the prompt cache. With system_input, we recommend you leave the same top-level tools list on every turn and use added_tools and removed_tools to update which functions the model can call as the session evolves. Only updating function tools via added_tools and removed_tools is currently supported and not MCP server tools.
In this example, a support agent starts with get_store_hours and issue_refund. On the next turn, the app removes issue_refund and adds create_escalation_ticket while keeping the top-level tools list identical:
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)
Tool updates stay in effect until a later system_input changes them. When a single step includes both fields, the API processes removed_tools before added_tools, letting you replace a tool definition in place. Adding a short text note when removing a tool also gives the model context to explain why an action is unavailable.
Keep in mind that system_input, added_tools, and removed_tools steer model behavior and serve as orchestration controls. Your application should still authorize every function call on the backend before executing it and keep untrusted external or end-user content out of system_input.
Session-dependent constraints: Apply rules discovered partway through a conversation, such as workspace settings, account entitlements, or spending limits.
Long-horizon instruction reinforcement: Re-emphasize key formatting, tone, or policy rules deep into multi-turn sessions as context grows.
Dynamic permissions and mode switches: Enable write or account-action tools after authentication or plan approval, and remove them when returning to read-only review.
Live operational changes: Disable a tool and update guidance mid-session when a downstream dependency goes offline, then restore it once service recovers.
Large tool catalogs: Start with a small set of tools and add more as the task needs them, without declaring every tool up front or breaking the cache.
How pricing and supported models work
Mid-conversation system instructions carry no separate fee. Tokens in system_input steps are billed at the standard input token rates for the model you call. Earlier turns remain eligible for cached-token rates when rules or tools change.
Mid-conversation system instructions are available in public preview across all Gemini 4 models in the Interactions API (through v1beta endpoint), in the Gemini API. Support for Gemini Enterprise Agent Platform to come soon.
Get started today by reading the documentation, comparing Gemini models to find the right Gemini 4 model for your workload, and join the discussion on the AI Developer Forum.
原文为英文,正文由自动抓取并保留原文。
原文发布于 Google Blog(Introducing mid-conversation instructions in the Gemini API),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。