GeezerVison is showing github.com — Changing ThinkingConfig (thinking_level / thinking_budget) mid-conversation invalidates implicit prompt cache prefix · Issue #3412 · googleapis/python-genai
Changing ThinkingConfig (thinking_level / thinking_budget) mid-conversation invalidates implicit prompt cache prefix #3412DescriptionEnvironment
- SDK:
google-genai (Vertex AI backend, vertexai=True)
- Feature: Implicit Context Caching +
GenerateContentConfig.thinking_config (thinking_level / thinking_budget)
Description
In multi-turn agentic conversations using implicit context caching on Vertex AI, changing thinking_config.thinking_level (for example from "MEDIUM" to "LOW" and back) between turns on the exact same conversation prefix invalidates the active implicit prompt cache bucket:
- Turns 1–2 (
thinking_level="MEDIUM"): cached_content_token_count = 26,655, uncached prompt_token_count = 3,441 (warm "MEDIUM" cache prefix).
- Turn 3 (switch to
thinking_level="LOW", identical system instruction, tools, and message history prefix): cached_content_token_count = 0, uncached prompt_token_count = 32,871 (100% cache miss).
- Turns 4–19 (staying on
thinking_level="LOW"): cached_content_token_count grows monotonically from 30,822 to 72,541 (prompt_token_count ~2,300–4,600 per turn).
- Turn 20 (switch back to
thinking_level="MEDIUM"): cached_content_token_count = 30,097, uncached prompt_token_count = 47,828 (misses the entire 42.4k-token "LOW" prefix accumulated across Turns 3–19 and falls back to the older "MEDIUM" prefix from Turn 2).
- Turn 21 (switch back to
thinking_level="LOW"): cached_content_token_count = 75,469, uncached prompt_token_count = 7,076 (re-attaches to the "LOW" cache bucket).
Expected vs. Actual Behavior
- Actual:
thinking_config appears to be injected/hashed ahead of system_instruction, tools, and contents in the implicit KV-cache key, partitioning the implicit cache into separate buckets per thinking_level / thinking_budget. Dynamically routing simpler turns to "LOW" thinking and complex turns to "MEDIUM"/"HIGH" thinking within one session therefore costs more in uncached input tokens than keeping thinking fixed at the higher level.
- Requested:
- Document in the
google-genai ThinkingConfig docstring and Vertex AI / Gemini API implicit-caching documentation that thinking_config must remain constant across turns to preserve implicit cache hits on the conversation history prefix.
- If feasible on the serving side, apply
thinking_config at the generation boundary (after the shared system_instruction + tools + historical contents prefix) so changing thinking_level for the current turn can still reuse the cached historical KV prefix.
ActivityVenkaiahbabuneelam commented on Nov 23, 2026 Hi @DUCKJAIII,
In Vertex AI and Gemini implicit prompt caching, generation configurations (including thinking_config parameters like thinking_level and thinking_budget) are incorporated into the prefix cache key and internal prompt prefill framing, which isolates the KV cache per configuration.
To maintain high cache hit rates across multi-turn sessions under the current serving architecture, keep thinking_config constant throughout the conversation:
from google import genai
from google.genai import types
client = genai.Client(vertexai=True, project="YOUR_PROJECT_ID", location="us-central1")
# Keep thinking_config fixed across the conversation session
session_config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_level="MEDIUM"),
system_instruction="You are a helpful coding assistant.",
)
chat = client.chats.create(model="gemini-3.5-flash", config=session_config)
response1 = chat.send_message("First turn prompt")
response2 = chat.send_message("Second turn prompt")
For more details on prefix matching requirements, see the Vertex AI Context Caching documentation.
Thanks
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|