claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[BUG] Warm prompt cache fully re-written (cache_read collapses to system+tools floor) seconds after ToolSearch / Skill / tool_result on v2.1.258 — 4 events with request IDs; plus pre-2.1.257 screenshot size-cap data

Open MihaiDicu2025 opened this issue 3 months ago • 10 comments

Environment

  • Claude Code v2.1.258 (macOS 25.4, Apple Silicon), Claude Max 20x subscription (OAuth login, no API key / gateway)
  • Model: claude-fable-5-1 (events below); the same signature was observed earlier on claude-opus-4-8 and claude-sonnet-5
  • A second machine (Windows 11, same account, Claude Code 2.1.250–2.1.252) shows the same signature independently

Summary

Mid-session, with a warm, established prompt cache (≥100k tokens read on the previous call, same model, ≤65 s earlier), a single call re-writes the whole context: cache_read_input_tokens collapses to the system+tools floor (33,376–33,405 on Fable 5.1; 27,182 on Opus 4.8) and cache_creation_input_tokens ≈ the entire conversation. The very next call reads the re-written cache normally again. Nothing in the conversation changed except an appended tool_result — so the invalidation must come from the client re-rendering the request prefix (tools / system / first message).

This looks like the class already described in #63930 (deferred-tool materialisation collapsing cache_read to the floor) and #81967 (tools array mutation invalidates everything after it). It is still present on 2.1.258.

Events on v2.1.258 (2026-09-02, Fable 5.1) — request IDs for cache diagnostics

Each row: the last GOOD call → the call that re-wrote the cache. "Trigger" = tool(s) whose result was appended between them.

# eviction (UTC) request IDs (good → eviction) gap cache_read before cache_read at eviction cache_creation at eviction trigger (tool result appended in between) screenshots in history
B1 05:13:17Z req_011Cee3DV4pAp9ca9hVD9Msd → req_011Cee3GufVmAn48jVFADzEU 44 s 329,438 33,376 337,258 Bash, Bash 1
B2 05:43:01Z req_011Cee5a8NwAWLH7JUT4eBhX → req_011Cee5aUcr9mWFkjzd2TNFJ 5 s 133,834 33,403 111,403 ToolSearch 1
B3 06:29:21Z req_011Cee96dKQrc63h5MnPUdWG → req_011Cee97HeG6CEvsuUDW4TFb 7 s 179,492 33,405 154,475 ToolSearch 0
B4 07:15:09Z req_011CeeCWoCVjihFg4ieDFar1 → req_011CeeCYvMfeab85CV99UcuQ 65 s 752,216 33,376 726,403 playwright:browser_run_code_unsafe, chrome:computer 53

Notes per event:

  • B2 / B3: the preceding call was ToolSearch (deferred tool schemas materialised). 5–7 s later the next request misses everything after the tools block. This is exactly the #63930 "Mode A".
  • B1: the preceding call was a Skill invocation earlier in the same minute (skill content injected), then Bash; the request after it missed everything.
  • B4: the biggest one (726k re-written, ≈$9 at list on Fable pricing). Between the good call and the eviction: a Playwright tool result (Connection closed) and a <system-reminder> block injected into the following user turn. The cache floor is identical to B1 (same session), i.e. the miss starts right after the tools block.

All four: same model, cache warm (5–65 s), no /compact, no /model switch, no resume, request well under the size cap (0–53 screenshots).

Detection method (so the numbers are not inflated)

Local transcripts (~/.claude/projects/**/*.jsonl) parsed one API call per message.id (the transcript writes one line per content block, all repeating the same usage — counting lines over-counts ~2.2×). A call is classed as an eviction only if: cache established (≥100k read on the previous call), same model, ≤300 s since previous call, and cache_read collapses below 50% of the established size while cache_creation ≥ 50% of it. Session-start priming, model switches and >5-min TTL re-caches are excluded. Per-event dossiers (all fields above + everything appended between the calls) are available on request.

Related, pre-2.1.257 class (for confirmation that the fix covers it)

On 2.1.250–2.1.252 the same account had 110 evictions over 31 Aug–1 Sep on two machines, all in long screenshot-heavy Claude-in-Chrome sessions. Measured on the raw transcripts: once the cumulative base64 image payload in history reached ~28–40 MB (~21–30 MB binary) — machine A from screenshot #298, machine B from #276 — every subsequent screenshot turn became a full re-write (cache_read = 27,182 floor, cache_creation 600k–950k, ≈$11–17/call at Opus list). Smoking-gun pair, same conversation, 15 s apart, warm cache: req_011CecStEr48YXCWRaUFXc6v (cache_read 833,928 / cache_creation 2,136) → req_011CecStv4jEeDLZwgtLr2vh (cache_read 27,182 / cache_creation 696,858), with only one screenshot tool_result in between. This matches the 2.1.257 changelog entry "Fixed prompt-cache misses on every turn in long screenshot-heavy sessions once images exceeded the per-request size cap." — could you confirm that fix targets exactly this (image stripping mutating early history), and whether a per-image/count threshold is involved? Since 2.1.257 we have not reproduced this class; the 4 events above are the remaining (tool-set) class.

Expected

Materialising deferred tools / injecting skill or system-reminder content mid-session should not invalidate the cached prefix for the whole conversation (e.g. append new tool definitions after the cached breakpoint, or keep the tools block byte-stable and place the cache breakpoint before mutable content).

Impact

On a subscription plan this burns the session/weekly quota ~10× faster per affected call (each event = re-caching the entire context at cache-write rates). Over 5 days: 114 real evictions ≈ $1.6k at list-equivalent, ~2/3 of which on the pre-2.1.257 screenshot class.

MihaiDicu2025 avatar Sep 02 '26 13:09 MihaiDicu2025

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/87234
  2. https://github.com/anthropics/claude-code/issues/88142
  3. https://github.com/anthropics/claude-code/issues/84847

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

github-actions[bot] avatar Sep 02 '26 14:09 github-actions[bot]

The read-collapse-to-floor after a tool_result on 2.1.258 matches #83913; 2.1.259 fixed it in my capture. Your pre-2.1.257 screenshot data is a separate, still-live residual: a pasted image is re-rendered as a file pointer on the next request → one rewrite (https://github.com/anthropics/claude-code/issues/91705). Workaround that sidesteps it: give the model a path to Read instead of pasting, https://github.com/ArshansGithub/shotpath.

ArshansGithub avatar Sep 03 '26 07:09 ArshansGithub

Correction to my earlier comment here: the pasted-image residual (#91705) is withdrawn and closed. The image block is byte-stable across requests; the collapse I attributed to it was a persisting cd rewriting the system prompt (#91706). The 2.1.259 fix for the tool_result collapse stands.

ArshansGithub avatar Sep 03 '26 09:09 ArshansGithub

Still reproducing on 2.1.270 and 2.1.283, i.e. after the 2.1.259 tool_result fix mentioned above, for the ToolSearch path (your B2/B3). The API's cache diagnostics report cache_miss_reason.type = "tools_changed" on every event below.

Trigger in every case: the first ToolSearch in a session that materializes mcp__claude-in-chrome__* tools (Claude in Chrome extension). The next request re-writes everything after the tools+system floor. macOS, OAuth login.

Version Model Good call → eviction cache_read before cache_read at eviction cache_creation
2.1.263 claude-fable-5-1 req_011CenUYkXxmcjfbQbmgA1es → req_011CenUZMf4z5KLuB5QKQo1d (2026-09-06 16:09:16Z) 218,735 32,932 191,162
2.1.270 claude-opus-5 req_011CezYwnxxrNWgr23TsfwgK → req_011CezYy5RwEJvCi1XU7JUqY (2026-09-13 01:14:04Z) 124,151 35,495 93,169
2.1.283 claude-opus-5-5 req_011CfSEpmSVC3zpquHRJ6D8F → req_011CfSEq55tZhusktYUigUVq (2026-09-26 14:51:39Z) 26,785 (first turn) 26,785 77,441

What narrows it down (local transcripts, one call per message.id):

  • Depends on the tool family. First Chrome-extension load per session, 2026-08-25 to 2026-09-08: 6 of 6 sessions whose startup deferred-tool list included the extension's tools broke on the next request. Other deferred families in the same window: Chrome DevTools MCP plugin 0/9, claude.ai connectors 0/67, built-in and local MCP 0/173. computer-use was 5/5.
  • Once per session. Later extension tool loads in the same session, same window: 0/16.
  • The toggle is free; materializing is not. On 2.1.270 (2026-09-13), enabling claude-in-chrome mid-session (its names enter the deferred list) produced no miss across four following requests (120,875 → 124,151 read). The break came only with the first ToolSearch that loaded one of its tools.

kylelovespi avatar Sep 26 '26 18:09 kylelovespi

The full rewrite is the visible cost. The secondary cost is whether the agent repeats work once its context goes cold, such as rereading screenshots, rerunning checks, or retrying an approach. We are exploring whether a fix can be enabled only after rerunning the session's own tests and confirming that billed costs fall.

jaclynsunardi avatar Oct 06 '26 21:10 jaclynsunardi

Another data point on 2.1.295 (macOS), from session transcripts 2026-10-10 → 10-15. On this version the trigger we see is specific to computer-use tools; the claude-in-chrome trigger reported above did not reproduce for us.

ToolSearch load (main loop, context ≥ 50k) Events Next request collapsed to the system+tools floor
materialized mcp__computer-use__* tools 11 11 (cache_read 43.4k–46.0k, cache_creation 43.8k–386.7k)
other deferred tools, incl. 16 loads of mcp__claude-in-chrome__* ~300 0

The query form doesn't matter: the computer-use keyword (9), select:mcp__computer-use__… (1), and a keyword search that happened to match computer-use tools (1). A second computer-use load later in the same session rewrote again.

Request IDs of rewriting calls (the request right after the ToolSearch result):

  • req_011Cg2TD6gWfqBpTeRoTVoVY (2026-10-14 16:34:12Z): read 46,032, created 142,066
  • req_011Cfz7UWLe4hCXahiAi7pYd (2026-10-13 10:54:13Z): read 44,118, created 229,281
  • req_011Cfwnkzbz9gn5SzLmFzg4S (2026-10-12 05:27:35Z): read 44,055, created 101,540

As a stopgap we deny the first computer-use ToolSearch in a session when the context is ≥ 80k (a tool.call hook in a local plugin), so the model can pick a browser tool or retry knowing the cost.

moneycode11101-svg avatar Oct 14 '26 18:10 moneycode11101-svg

Still reproducing on 2.1.299 (Claude Desktop 2.42304.0, Code tab, macOS 26.5.1), with the computer-use trigger from the 2026-10-14 comment above. Each time the API's diagnostics say cache_miss_reason.type = "tools_changed", and the request reads back only the 42–46k system+tools floor.

Since 2.1.289, the first ToolSearch load of mcp__computer-use__* in a session has rewritten the whole context in 6 of 6 sessions (Opus 5.5, main loop). The load's result held only tool_reference blocks, and the request below came within 8 s of it:

Request after the load Version cache_read cache_creation
req_011Cg7xNf2bG8yqZU86MRYTW (2026-10-17 14:19:38Z) 2.1.299 42,136 452,887
req_011Cg7mJxwUPNgj9L8dJ3H44 (2026-10-17 11:54:33Z) 2.1.299 42,134 559,913
req_011Cg2QnxBtLzWeEJV6Voi7R (2026-10-14 16:02:33Z) 2.1.295 41,947 145,192
req_011Cg1vCzHwVY7mjntYLp2KX (2026-10-14 09:47:39Z) 2.1.295 46,109 359,753
req_011CfrZ7DexzaEXw9B43bB8f (2026-10-09 11:05:41Z) 2.1.293 41,947 797,324
req_011CfpLYA7KQqm8rbnEnusrZ (2026-10-08 06:59:34Z) 2.1.289 41,759 176,027

Three details that may help narrow it down:

  • Only a session's first load. Later computer-use loads in the same session kept the cache in 11 of 11 cases. 8 of those came after a compaction, in the session reopened on a newer engine. This differs from the comment above, where a second load rewrote again.
  • Consistent since 2.1.289. On 2.1.271–2.1.286, 3 of 8 first loads rewrote the cache.
  • Other deferred tools are fine. Loads of every other tool family through ToolSearch kept the cache, with 1 rewrite in 1,392 loads since 2026-09-17.

These counts come from local transcripts: one API call per message.id, each load counted once (forked sessions copy rows), in contexts of 50k+ tokens, and only where the next request came within the hour on the same model.

thomasbachem avatar Oct 17 '26 19:10 thomasbachem

Adding a controlled repro that may narrow this down: the trigger appears to follow the tool's name, not its content or the batch size.

Setup: a short throwaway claude -p --chrome session, with ANTHROPIC_BASE_URL pointed at a local proxy that logged request bodies. The model loads WebFetch, then a second batch, through ToolSearch. Numbers are for the request right after the second load. Where noted, the proxy rewrote the request just before forwarding it, so Claude Code wasn't aware of the change.

Second load (2.1.308, Opus 5.5) cache_read cache_creation Result
2 mcp__claude-in-chrome__* tools, unmodified 2,317 16,375 rewrite
Same tools, renamed to mcp__browser-probe__* 12,151 963 kept
Original names, with description and schema replaced by stubs 2,317 10,088 rewrite
WebSearch, renamed to mcp__claude-in-chrome__websearch 2,317 10,429 rewrite

The earlier WebFetch load kept the cache in all four runs.

  • In the unmodified run, the two adjacent request bodies differed only in the tools array, the appended messages and the cache_control positions. system and the earlier message content were identical.
  • The batch here is 2 tools, with a 78-byte select: query, so the size threshold in #83756 doesn't explain this case.
  • It also reproduces on 2.1.319 with Sonnet 4.6: 6,824 read / 679 written after the WebFetch load, then 1,562 read / 6,642 written after the Chrome load.
  • Local transcripts agree. The first Chrome load in a session rewrote the cache in 11/11 VS Code, 5/5 terminal and 12/12 claude -p cases. Later loads in the same session rewrote it in 0/14.

Since this happens with an otherwise unchanged request, it looks server-side and keyed on the name. That might also explain the computer-use reports above, though I haven't tested those.

podollyb avatar Nov 06 '26 21:11 podollyb

I'm seeing the same warm-cache invalidation on Claude Code 2.1.319, running on Windows with Opus 5.5 and a Claude Ultra subscription.

I investigated my local JSONL transcripts with ChatGPT after noticing unusually rapid weekly usage consumption.

The key finding: a ToolSearch call immediately preceded a 781,235-token cache rewrite. The API explicitly reported tools_changed.

Evidence

Machine: Windows (ALEX-AMD)
Claude Code: 2.1.319
Model: claude-opus-5-5
Session: 54137c92-920c-4cfd-a2ea-5f2b7a423685

All timestamps below are UTC, November 6, 2026.

1. Successful cache hit, 20:27:44

  • Cache read: 808,203 tokens
  • Cache write: 11 tokens
  • Request ID: req_011CgnJcwZsLinJxcVgPUd2X

A ToolSearch invocation is recorded at 20:27:44.811Z.

2. Cache invalidation, 20:28:14.945

  • Cache read: 36,239 tokens
  • Cache write: 781,235 tokens
  • Request ID: req_011CgnJdN8jt8kU1pMRiFgdN
  • diagnostics.cache_miss_reason.type: tools_changed

3. Cache working again, approximately 20:28:32

  • Cache read: 817,474 tokens
  • Cache write: 502 tokens
  • Request ID: req_011CgnJfo6igrNwXjtZBZEjf

The cache was successfully used approximately 30 seconds before the invalidation, and the model remained Opus 5.5. This is not consistent with ordinary cache expiration.

The sequence is:

Warm cache → ToolSearch → tools_changed → 781K cache write → warm cache again.

Additional findings

Across this session, I identified 18 cache rewrites of at least 100K tokens, totaling approximately 4.95 million cache-write tokens.

Thirteen occurred within five minutes of a previous recorded response. However, I cannot attribute all 18 to the same cause because the diagnostics are incomplete.

I also investigated the session's agent and tool activity. The evidence did not show a widespread repetitive-command or failed-tool loop.

What I'd like Anthropic to investigate

  1. What exactly changed in the tools[] definition between these request IDs?
  2. Did the preceding ToolSearch cause Claude Code to change the cached tool prefix?
  3. Is this the same underlying issue reported here and in #83756?
  4. Why is this still occurring in 2.1.319, and is there a fix or mitigation?

I don't have the raw outgoing API request bodies, so I cannot independently identify the exact tool-definition difference or prove that ToolSearch alone caused it. But the timing and server-reported tools_changed reason make this a strong case for investigation.

This is particularly frustrating on a paid subscription because a single unexpected invalidation can turn hundreds of thousands of cached tokens into a full cache rewrite.

I'd appreciate someone checking the three request IDs above to identify the actual cause.

Freesoul1982 avatar Nov 08 '26 07:11 Freesoul1982

Still present on 2.1.322 and 2.1.323, including Fable 5.2 and Mythos 5.2. Ten first loads in four days, all in terminal CLI sessions, all with cache_miss_reason.type = "tools_changed":

Request after the load Version Model Tools loaded cache_read cache_creation
req_011Ch1GVR4wTsmmZ3TCqKJNs (2026-11-13 16:47:28Z) 2.1.323 fable-5-2 7 × claude-in-chrome 31,231 583,760
req_011Ch16ozbA4JXQZQafYWUf7 (2026-11-13 14:40:32Z) 2.1.323 opus-5-5 6 × claude-in-chrome 29,749 48,611
req_011CgzHAVrWk1zhK3kok5PF2 (2026-11-13 04:15:42Z) 2.1.323 fable-5-2 24 × computer-use 33,179 198,423
req_011Cgz5rD2wsYjp6cmz1ihM1 (2026-11-13 01:47:19Z) 2.1.322 mythos-5-2 8 × claude-in-chrome 32,879 470,776

The other six, with the full table, are in #109664.

Two things this adds to the comments above:

  • In the CLI, both servers trigger on the same version. claude-in-chrome 7 of 7 and computer-use 3 of 3 on 2.1.322/2.1.323. That differs from the 2.1.295 report above where only computer-use triggered, so the Chrome case has either come back or depends on something else about the session.
  • Controls from the same sessions. 29 other loads were free: 6 later loads from the same two servers, and 23 from elsewhere, including 20 tools at once from another MCP server.

podollyb's single-load claude -p --chrome reproduction also still works on 2.1.323 without a proxy, reading the verdict from the transcript: loading mcp__claude-in-chrome__navigate alone rewrote the history (req_011Ch1HKoRBrFJbWpXVdTHLt, tools_changed), and loading computer right after it was free.

jcll avatar Nov 13 '26 17:11 jcll