Skip to content

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

Closed

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

Activity

  1. added
    agent engine[Component] This issue is related to Vertex AI Agent Engine
    on Mar 3, 2026
  2. added theissue type on Mar 3, 2026
  3. self-assigned this
    on Mar 4, 2026
  4. llalitkumarrr commented on Mar 4, 2026

    @llalitkumarrr
    Collaborator

    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?

  5. added
    needs review[Status] The PR/issue is awaiting review from the maintainer
    on Mar 4, 2026
  6. added 4 commits that reference this issue on Mar 5, 2026
    07a9526
    2e0e44b
    d5b4e58
    eae3365
  7. added 5 commits that reference this issue on Mar 18, 2026
    01ce5b9
    ba4b2f7
    bf25e72
    fe003d7
    ca5f1e9
  8. muzna-naeem commented on Apr 10, 2026

    @muzna-naeem
    Author

    Hey, any update on this one?

  9. ssingh5bmcd commented on Apr 16, 2026

    @ssingh5bmcd

    Is there any update ?

  10. added a commit that references this issue on Apr 29, 2026
    b2e0cb1
  11. josipTheorem258 commented on May 25, 2026

    @josipTheorem258

    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?

  12. fufufukakaka commented on Jun 9, 2026

    @fufufukakaka

    I really hope things will move forward soon...

  13. DeanChensj commented on Jun 9, 2026

    @DeanChensj
    Collaborator

    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?

  14. adk-bot commented on Jun 16, 2026

    @adk-bot
    Collaborator

    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.

  15. added
    stale[Status] Issues which have been marked inactive since there is no user response
    request clarification[Status] The maintainer need clarification or more information from the author
    on Jun 16, 2026
  16. haranrk commented on Jun 17, 2026

    @haranrk
    Collaborator

    This has been fixed in 9da9dee! As @DeanChensj said, we store the raw event. If the issue persists, please create another issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions