Skip to content

feat(tools): add anchor_to_user_turn option to PreloadMemoryTool for stable prompt caching (#8496) - #8523

Open
Abdulrehman1978 wants to merge 1 commit into
google:mainfrom
Abdulrehman1978:fix/preload-memory-stable-placement
Open

Abdulrehman1978 wants to merge 1 commit into
google:mainfrom
Abdulrehman1978:fix/preload-memory-stable-placement

Conversation

@Abdulrehman1978

Copy link
Copy Markdown

Summary

PreloadMemoryTool previously always inserted <PAST_CONVERSATIONS> via llm_request._insert_transient_user_content(...), which places the block at the end of llm_request.contents and causes its position to shift on every tool-loop iteration ([MEMORY] user, user call result [MEMORY], ...). That invalidates prefix-based prompt caches (such as OpenAI / LiteLLM) from the second model call onward.

Addresses #8496

Changes

  • Add anchor_to_user_turn: bool = False to PreloadMemoryTool.__init__() (src/google/adk/tools/preload_memory_tool.py).
    • When False (default), behavior is unchanged to preserve existing Gemini explicit context caching semantics.
    • When True, the preloaded memory block is inserted immediately before tool_context.user_content in llm_request.contents (matching RunConfig.model_input_context placement), keeping the entire earlier request an exact prefix across every model call in the turn.
  • Add test_preload_memory_anchor_to_user_turn_preserves_prefix in tests/unittests/tools/test_preload_memory_tool.py.

Testing

  • pytest tests/unittests/tools/test_preload_memory_tool.py -q (16 passed)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant