adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Context caching not working with Agent Engine (VertexAiSessionService drops cache_metadata)

Open muzna-naeem opened this issue 8 months ago • 8 comments

🔴 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:

  1. Install google-adk[eval]==1.25.1
  2. Configure an agent with ContextCacheConfig in App():
     app = App(   
      name="my_agent",    
      root_agent=root_agent,    
      context_cache_config= ContextCacheConfig( min_tokens=2048,  ttl_seconds=1800,   cache_intervals=10),)
  1. Run with in-memory sessions: adk web — send the same prompt twice in the same session. Observe cacheMetadata in the dev UI showing cache_name reuse on the second turn.
  2. 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_name is 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%)

muzna-naeem avatar Mar 03 '26 17:03 muzna-naeem

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?

llalitkumarrr avatar Mar 04 '26 08:03 llalitkumarrr

Hey, any update on this one?

muzna-naeem avatar Apr 10 '26 09:04 muzna-naeem

Is there any update ?

ssingh5bmcd avatar Apr 16 '26 08:04 ssingh5bmcd

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?

josipTheorem258 avatar May 25 '26 09:05 josipTheorem258

I really hope things will move forward soon...

fufufukakaka avatar Jun 09 '26 01:06 fufufukakaka

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?

DeanChensj avatar Jun 09 '26 01:06 DeanChensj

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.

adk-bot avatar Jun 16 '26 08:06 adk-bot

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.

haranrk avatar Jun 17 '26 21:06 haranrk