fix(session): cache messages across prompt loop to preserve prompt cache byte-identity#25367
Open
BYK wants to merge 1 commit intoanomalyco:devfrom
Open
fix(session): cache messages across prompt loop to preserve prompt cache byte-identity#25367BYK wants to merge 1 commit intoanomalyco:devfrom
BYK wants to merge 1 commit intoanomalyco:devfrom
Conversation
…ache byte-identity
Contributor
|
The following comment was made by an LLM, it may be inaccurate: Potential Duplicate Found:
Related PRs that may be of interest:
Recommendation: Check PR #24842 to see if it was already addressing this issue or if there's a reason for the duplicate attempt. |
This was referenced May 2, 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.
Issue for this PR
Closes #25366
Type of change
What does this PR do?
OpenCode reloads messages from the DB at the start of every prompt loop iteration via
MessageV2.filterCompactedEffect(). Between tool-call steps, tool parts transition frompending→completedwith output text. This means the same assistant message serializes to different bytes on consecutive API calls, busting Anthropic's prompt cache from that position forward.Fix: Cache the conversation array across loop iterations. On tool-call continuation steps, only append genuinely NEW messages instead of reloading all messages from the DB. Existing messages retain their original serialized form (as the API last saw them), preserving byte-identity for the prompt cache.
Full reloads still happen on:
Impact: On sessions with heavy tool use, this eliminates prompt cache busts on continuation steps. On April 21st alone, the cache-bust pattern cost $2,264 in cache writes vs $1,234 in cache reads on a single account.
How did you verify your code works?
Checklist