Skip to content

fix(anthropic): anchor rolling cache breakpoint before transient addendum - #48

Merged
zhanghanduo merged 2 commits into
mainfrom
fix/anthropic-cache-transient-tail
Sep 26, 2026
Merged

zhanghanduo merged 2 commits into
mainfrom
fix/anthropic-cache-transient-tail

Conversation

@zhanghanduo

Copy link
Copy Markdown
Collaborator

Problem

system_addendum_per_call is appended only to messages_for_call (not history), and _add_prompt_cache marked msgs[-1] — the addendum. Next turn it is replaced by the real assistant/tool turn, so the cached prefix never matches again. Observed in Forge (claude-sonnet-5, stateful-react-agent): cache_read constant at 3610 every turn, cache_create growing; ~4x billed input vs claude_code.

Fix

  • Message.transient flag (in-process only; dropped by for_wire).
  • Agent loop flags the per-call addendum transient.
  • Anthropic provider counts trailing transient messages and puts the rolling breakpoint on the last persistent one. Without an addendum (turns ≤ system_addendum_min_turn) behaviour is unchanged.

Tests

  • Breakpoint skips the addendum; prefix up to previous turn's breakpoint is byte-identical next turn (fails without the fix).
  • No addendum → breakpoint on last message.
  • for_wire strips transient.
  • Loop flags the addendum and keeps it out of history.

Full suite: 1605 passed; ruff clean.

🤖 Generated with Claude Code

zhanghanduo and others added 2 commits September 26, 2026 16:41
…ndum

The per-call system addendum is appended to messages_for_call but never
kept in history, and the rolling prompt-cache breakpoint was placed on
msgs[-1] -- i.e. on the addendum. The next request replaces it with the
real turn, so no conversation prefix was ever reused: cache_read stayed
constant at the static head while cache_create re-wrote the whole
conversation every turn.

Flag the addendum `transient` (stripped by for_wire) and have the
Anthropic provider anchor the rolling breakpoint on the last persistent
message.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@zhanghanduo
zhanghanduo merged commit a02a71e into main Sep 26, 2026
5 checks passed
@zhanghanduo
zhanghanduo deleted the fix/anthropic-cache-transient-tail branch September 26, 2026 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant