Skip to content

fix(session): cache messages across prompt loop to preserve prompt cache byte-identity#25367

Open
BYK wants to merge 1 commit intoanomalyco:devfrom
BYK:perf/session-prompt-cache
Open

fix(session): cache messages across prompt loop to preserve prompt cache byte-identity#25367
BYK wants to merge 1 commit intoanomalyco:devfrom
BYK:perf/session-prompt-cache

Conversation

@BYK
Copy link
Copy Markdown
Contributor

@BYK BYK commented May 2, 2026

Issue for this PR

Closes #25366

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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 from pendingcompleted with 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:

  • First iteration
  • After compaction (conversation structure changes)
  • After subtask/overflow recovery (these structurally change the conversation)

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?

  • Verified cached messages are used on tool-call continuation
  • Full reload still triggers after compaction
  • Existing tests pass

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@BYK BYK requested a review from adamdotdevin as a code owner May 2, 2026 00:57
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prompt cache busts on every tool-call continuation step

1 participant