Skip to content

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

Description

@muzna-naeem

🔴 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%)

Metadata

Metadata

Labels

agent engine[Component] This issue is related to Vertex AI Agent Engineneeds review[Status] The PR/issue is awaiting review from the maintainerrequest clarification[Status] The maintainer need clarification or more information from the authorstale[Status] Issues which have been marked inactive since there is no user response

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions