[BUG] Prompt cache invalidated by rewrites of messages in long sessions

Open 💬 1 comment Opened Jul 11, 2026 by oakif

What's Wrong?

Claude code sessions sometimes reprocess the entire conversation instead of just a new message that a user sends. This is not due to any reason visible in the chat itself.

I found two causes by diffing /v1/messages requests around a cost spike.

  1. Claude Code rewrites an old hook reminder's shape later in the session. It does this by either moving it into its own message, or merging it into a neighboring one. Becausr this edits a message from earlier in the session's history, the entire session cache gets invalidated. I hit this multiple times in the same day.
  2. Long sessions only get one cache checkpoint, at the very end. This means that if this checkpoint is missed, you're SOL and the entire convo hits a cache write.

What Should Happen?

Old messages should not be modified, since this invalidates the entire cache, which can cost up to $20 (or even more...)

Steps to Reproduce

  1. Run a long VS Code session with PreToolUse hooks that add extra context to tool calls
  2. Diff consecutive raw request bodies around a big cache_creation_input_tokens spike which should hit eventually
  3. You'll see an old <system-reminder> block change shape, either split into its own message or merged into a neighboring one, breaking the cache for everything after it

Claude Code Version

2.1.197 (Claude Code)

Is this a regression?

Not sure, probably not

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

VS Code extension (CLI wrapper)

View original on GitHub ↗

This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗