Repository navigation
feat(tools): add anchor_to_user_turn option to PreloadMemoryTool for stable prompt caching (#8496) - #8523
Open
Abdulrehman1978 wants to merge 1 commit into
Conversation
…stable prompt caching (google#8496)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PreloadMemoryToolpreviously always inserted<PAST_CONVERSATIONS>viallm_request._insert_transient_user_content(...), which places the block at the end ofllm_request.contentsand 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
anchor_to_user_turn: bool = FalsetoPreloadMemoryTool.__init__()(src/google/adk/tools/preload_memory_tool.py).False(default), behavior is unchanged to preserve existing Gemini explicit context caching semantics.True, the preloaded memory block is inserted immediately beforetool_context.user_contentinllm_request.contents(matchingRunConfig.model_input_contextplacement), keeping the entire earlier request an exact prefix across every model call in the turn.test_preload_memory_anchor_to_user_turn_preserves_prefixintests/unittests/tools/test_preload_memory_tool.py.Testing
pytest tests/unittests/tools/test_preload_memory_tool.py -q(16 passed)