Context caching not working with Agent Engine (VertexAiSessionService drops cache_metadata)
🔴 Required Information
Describe the Bug:
VertexAiSessionService does not persist cache_metadata or usage_metadata fields when writing/reading Event objects to/from Agent Engine. This causes ContextCacheRequestProcessor to never find prior cache metadata, so context caching creates a new cache on every invocation instead of reusing existing ones.
Steps to Reproduce:
- Install
google-adk[eval]==1.25.1 - Configure an agent with
ContextCacheConfigin App():
app = App(
name="my_agent",
root_agent=root_agent,
context_cache_config= ContextCacheConfig( min_tokens=2048, ttl_seconds=1800, cache_intervals=10),)
- Run with in-memory sessions:
adk web— send the same prompt twice in the same session. ObservecacheMetadatain the dev UI showingcache_namereuse on the second turn. - Run with Agent Engine sessions:
adk web --session_service_uri=agentengine://<your_engine_id>— send the same prompt twice in the same session. Observe that every turn creates a new cache with a different fingerprint;cache_nameis never reused.
Expected Behavior:
cache_metadata and usage_metadata should round-trip through Agent Engine sessions the same way they do with InMemorySessionService. The second invocation in the same session should reuse the existing cache.
Observed Behavior:
With VertexAiSessionService, every invocation creates a new cache. The cacheMetadata in events shows a new fingerprint and increasing contents_count each time, with no cache_name reuse.
Environment Details:
- ADK Library Version: 1.25.1
- Desktop OS: macOS
- Python Version: 3.11
Model Information:
- Are you using LiteLLM: Yes/No
- Which model is being used: gemini-2.5-pro
🟡 Optional Information
Regression: N/A — this has never worked with VertexAiSessionService as far as we can tell (tested 1.19.0 and 1.25.1).
Logs:
With InMemorySessionService (working):
cacheMetadata:
cache_name: "projects/798321343024/locations/us-central1/cachedContents/6261708034305687552"
expire_time: 1772555630.466322
fingerprint: "e8e3e4d5226b764d"
invocations_used: 2
contents_count: 2
created_at: 1772553830.466322
With VertexAiSessionService (broken — new cache every turn):
cacheMetadata:
fingerprint: "c3f575505d7ec95c"
contents_count: 6
Minimal Reproduction Code:
from google.adk.agents import Agent
from google.adk.app import App
from google.adk.context_cache import ContextCacheConfig
root_agent = Agent(
name="test_agent",
model="gemini-2.5-pro",
instruction="You are a helpful assistant.",
)
app = App(
name="test_app",
root_agent=root_agent,
context_cache_config=ContextCacheConfig(
min_tokens=2048,
ttl_seconds=1800,
cache_intervals=10,
),
)
# Works: adk web
# Broken: adk web --session_service_uri=agentengine://<engine_id>
How often has this issue occurred?:
- Always (100%)
Hello @muzna-naeem ,
Thank you for bringing this to our attention. We were able to reproduce the issue using google-adk==1.26.0.
It appears the failure happens during the if llm_request.cache_metadata check (line no 74) in gemini_context_cache_manager.py. When the ADK web session_service_uri is used llm_request.cache_metadata is always None which causes the check to fail. However, when using the default Inmemorysessionservice, it correctly returns a CacheMetadata object.
We are currently investigating why this discrepancy is happening. We appreciate your patience while we look into this further.
Hello @DeanChensj , could you please have a look at this and share your inputs?
Hey, any update on this one?
Is there any update ?
Is the intended behavior for ADK ContextCacheConfig to reuse a cache only within the same session, or should it also reuse equivalent caches across sessions when static_instruction + tools docstrings are identical?
Is there any way to create one shared cached content resource for the root agent’s static instruction + tools docstrings, then reuse that same cached content across all Agent Engine sessions when multiple people are talking at the same time?
I really hope things will move forward soon...
Hi @muzna-naeem @ssingh5bmcd @fufufukakaka , this issue should be resolved in the latest a few releases. Have you checked? We introduced blob based event storage in vertex ai session service. Could you double check?
This issue has been automatically marked as stale because it has not had recent activity for 7 days after a maintainer requested clarification. It will be closed if no further activity occurs within 7 days.
This has been fixed in https://github.com/google/adk-python/commit/9da9dee140a3c8971d2dc267eab7d8d17a22a089! As @DeanChensj said, we store the raw event. If the issue persists, please create another issue.