fix: place cache point on last user message instead of assistant#1821
Open
kevmyung wants to merge 2 commits intostrands-agents:mainfrom
Open
fix: place cache point on last user message instead of assistant#1821kevmyung wants to merge 2 commits intostrands-agents:mainfrom
kevmyung wants to merge 2 commits intostrands-agents:mainfrom
Conversation
Move cache point injection to last user message to improve cache hit rates in agent loops and avoid reasoningContent block conflicts.
pgrayy
reviewed
Mar 6, 2026
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
7 tasks
pgrayy
approved these changes
Mar 6, 2026
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.
Move cache point injection in
_inject_cache_pointfrom the last assistant message to the last user message (including toolResult messages) for the Anthropic cache strategy. Closes #1820Description
The current implementation appends
cachePointto the last assistant message. This PR changes it to append to the last user message instead, for two reasons:Avoids potential issues with assistant message generation — Assistant messages are generated via streaming and may be subject to unexpected interruptions or content block ordering issues (see [BUG] Invalid Cache Point Injection in BedrockModel #1820). Placing the cache point on user messages avoids this entirely since user messages are always complete and well-structured.
Aligns with Anthropic's official caching guide — Anthropic's automatic caching documentation (released Feb 2026, not yet supported on Bedrock) places the cache breakpoint on the last user message, moving it forward as the conversation grows.
In testing, this change showed improved cache hit rates in both multi-turn conversations and agent loops with tool calls.
Related Issues
#1820
Type of Change
Bug fix
Testing
All existing cache point tests updated and passing (7 tests)
hatch run preparepassed (2110 tests)I ran
hatch run prepareChecklist