You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Prompt loop DB reload breaks Anthropic cache after tool calls (63% of spend) #24841
The prompt loop in prompt.ts reloads all messages from the DB (filterCompactedEffect) at the start of every iteration. Between tool-call steps, tool parts transition from pending to completed with output text. When toModelMessages() serializes the conversation for the next API call, the previous assistant message has different bytes:
Before tool execution (step N): state: "output-error", errorText: "[Tool execution was interrupted]" (message-v2.ts line 912)
After tool execution (step N+1): state: "output-available", output: <actual output> (message-v2.ts line 853)
Anthropic's prompt cache sees different bytes at that message position → invalidates everything after it → the entire remaining context is re-written at cache-write pricing ($6.25/MTok for Opus, 12.5× the cache-read price).
Cost impact from real session data (Opus 4.7, 1M context):
63% of total API spend is cache writes ($2,264 out of $3,584 on April 21st)
95% of rapid cache busts (<60s gap) have a tool call in the preceding message
Bust turns: cache_read=54K, cache_write=560K — only system prompt survives
Sessions with heavy tool use see 20-84% bust rates
This is likely the same root cause as #20565 (same snapshot hash, mixed cache behavior) and related to #20110 (prompt cache invalidation class).
Plugins
Lore (opencode-lore) — but the issue is in OpenCode core, not the plugin. Lore's gradient transform returns input.messages unmodified at layer 0.
OpenCode version
v1.14.29 (latest dev)
Steps to reproduce
Start a session with Claude Opus 4.7 (1M context)
Have the model make tool calls (read files, run bash, etc.)
Observe token usage: warm turns have cache_write ~1K, but turns immediately after tool execution have cache_write ~500K+
Query the OpenCode DB: SELECT cache_read, cache_write FROM message WHERE session_id = '...' AND role = 'assistant' — the alternating pattern of warm/bust is visible
This issue might be a duplicate of existing issues. Please check:
perf: session loop re-reads all messages from DB every iteration #22208: Covers the same root cause — filterCompactedEffect re-reading all messages from DB on every prompt loop iteration. This issue provides a more specific analysis of the Anthropic cache-busting impact of that behavior.
If this is a distinct enough angle (cache cost impact vs. DB read overhead), feel free to clarify how it differs from #22208 so maintainers can track them separately.
Activity
github-actions commentedon Apr 28, 2026
This issue might be a duplicate of existing issues. Please check:
filterCompactedEffectre-reading all messages from DB on every prompt loop iteration. This issue provides a more specific analysis of the Anthropic cache-busting impact of that behavior.If this is a distinct enough angle (cache cost impact vs. DB read overhead), feel free to clarify how it differs from #22208 so maintainers can track them separately.
github-actions commentedon Jul 3, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.