feat(cache): add stable prefix caching strategy for improved hit rates (#2271)#2272
Open
dinindunz wants to merge 2 commits intostrands-agents:mainfrom
Open
feat(cache): add stable prefix caching strategy for improved hit rates (#2271)#2272dinindunz wants to merge 2 commits intostrands-agents:mainfrom
dinindunz wants to merge 2 commits intostrands-agents:mainfrom
Conversation
strands-agents#2271) Introduces `anchor_first_message` to `CacheConfig` to inject a stable cache point on the first user message. This stable prefix acts as a fallback when the moving tail cache point (on the last user message) is invalidated by context pruning or summarization, enhancing cache resilience and hit rates.
…sage (strands-agents#2271) Removes the exclusion that prevented the moving tail cache point from being added to the last user message if it was the same as the first user message and `anchor_first_message` was active. This ensures both caching strategies are applied concurrently, improving cache resilience in single-turn conversations.
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.
Description
The current CacheConfig(strategy="auto") in BedrockModel only injects a single cache point on the last user message (the "moving tail"). There is no stable/anchor cache point placed on the first user message to cover the system prompt + initial user prompt + tool descriptions.
This means that whenever the moving tail gets invalidated (e.g., due to context pruning, summarisation, or conversation reset), the entire prompt cache is lost and must be rebuilt from scratch. There is no fallback cache prefix to absorb the cost of the stable portions of the prompt.
Proposed Solution
Modify _inject_cache_point() to inject two cache points instead of one:
Stable prefix: A cache point appended to the first user message's content. This anchors the cache over the system prompt + first user message + tool descriptions - content that rarely changes across turns.
Moving tail: A cache point appended to the last user message's content (current behavior). This advances with the conversation to maximise cache hit rate on recent context.
Usage
Related Issues
#2271
Documentation PR
N/A
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.