Guide to Mid-Conversation System Instructions and Tool Updates in the Gemini API
Learn how to update system instructions and tools mid-conversation in the Gemini API via the Interactions API, keeping the prompt cache intact to reduce cost and latency in long sessions.
Open the official reference| Item | Details |
|---|---|
| Provider | |
| Level | Intermediate |
| Published | 2026-12-03 |
| Official source | |
| Topics | #engineering · #llm |
Background and Value
Long-running agents often work across dozens, if not hundreds, of turns. Along the way, rules, permissions and available tools change. Until now, developers could not give the model a new system instruction or update tools without invalidating the cache. The Gemini API now offers mid-conversation system instruction and tool changes in preview through the Interactions API, letting agents adapt to new rules and tools mid-session without re-reading the entire conversation or driving up costs.
Problems with Previous Workarounds
Developers commonly relied on two workarounds:
- Putting instructions in a user message: To preserve the cache, new system instructions could be sent as a user message. These do not carry the same weight as 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.
Core Mechanism
The Interactions API now allows sending a system_input step at any turn with system instruction or tool changes. It carries the same authority as the top-level system instruction and takes precedence over user messages, while earlier turns stay cached. 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.
Step-by-Step: Sending Mid-Conversation System Instructions
- To send an instruction mid-conversation, pass the previous turn's ID in
previous_interaction_id. - Place a
system_inputstep directly beforeuser_inputin the input array. - Keep the top-level
system_instructionunchanged across turns so the prompt prefix stays valid in the cache.
Example code (Python):
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)Auditing and Debugging
When retrieving 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.
Step-by-Step: Mid-Conversation Tool Updates
Tool declarations sit at the start of the prompt, so changing the top-level tools list between turns breaks the prompt cache. It is recommended to 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 the 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.
Limitations and Scope
- The feature is currently in preview through the Interactions API.
- Only updating function tools via
added_toolsandremoved_toolsis currently supported and not MCP server tools. - One
system_inputstep per turn can be sent directly beforeuser_input, containing text content, tool updates, or both. - Keep the top-level
system_instructionand top-leveltoolslist unchanged across turns to keep the prompt cache valid. system_input,added_tools, andremoved_toolssteer model behavior and serve as orchestration controls; the application should implement its own controls accordingly.
Sources & Further Reading
This page is grounded in the authoritative sources below — verifiable and citable by AI engines and readers.
- 🔗 Official source
- # Topic: EngineeringAPIs, prompting and system design
- # Topic: LLMsLLM capabilities, products and best practices
Citation: Please attribute Laojin Global (laojinchuhai.com) and keep the original link.
Made by Laojin · AI that ships
AllModelsAPIOne key for many models
AllModelsAPI is a multi-model API gateway: one key reaches many models through an OpenAI-compatible interface — point your existing code at a new base URL and you're migrated. It's not a demo: our own production workloads run on it every day.
Related
Linked by topic, people and hubs