claude-code - 💡(How to fix) Fix `--continue`/`--resume` cache invalidation still reproduces on v2.1.116 — TTL-matched control shows 41–99pp hit-rate delta [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#51764Fetched 2026-04-22 07:53:28
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Timeline (top)
labeled ×5commented ×1

Issue #42338 was closed after v2.1.90/91/92 attempted to fix session-resume prompt-cache invalidation. I can reproduce the same behavior on v2.1.116, 24 versions later. This report contributes a new methodology: TTL-matched control pairs that isolate the resume event itself as the cause, independent of cache TTL expiry.

Root Cause

Any cache-hit delta between the two sets at matched gap duration cannot be explained by TTL, because the gap is identical. The delta is therefore attributable to whatever differs between the two cases — the resume event itself (new process, rollout replay, deferred-tools/attachment reconstruction).

Fix Action

Fix / Workaround

Code Example

$ claude --version
2.1.116 (Claude Code)
RAW_BUFFERClick to expand / collapse

Summary

Issue #42338 was closed after v2.1.90/91/92 attempted to fix session-resume prompt-cache invalidation. I can reproduce the same behavior on v2.1.116, 24 versions later. This report contributes a new methodology: TTL-matched control pairs that isolate the resume event itself as the cause, independent of cache TTL expiry.

Version

$ claude --version
2.1.116 (Claude Code)

Why a new issue (not #42338)

The bot auto-locked #42338 on 2026-04-11 after 7 days of inactivity with the standard "please file a new issue" message. Filing here per that instruction.

Methodology — TTL-matched control pairs

Prior reports in #42338 measured single-case token counts (e.g. "2-12s exit→resume produces 470–512k cache_creation"). A skeptic could still argue "maybe the 5-minute TTL happened to tick over." To fully eliminate TTL as a confound, I pulled per-turn usage from 14 jsonl session files for one long-running Claude Code agent on the same machine, deduplicated by requestId, and built two comparison sets:

  • Baseline: consecutive turns inside the same jsonl separated by idle gaps of 3–28 min.
  • Test: transitions where one jsonl ends and a new jsonl begins (i.e. a resume / new process) with the same 3–28 min idle gap.

Any cache-hit delta between the two sets at matched gap duration cannot be explained by TTL, because the gap is identical. The delta is therefore attributable to whatever differs between the two cases — the resume event itself (new process, rollout replay, deferred-tools/attachment reconstruction).

Observation 1 — Claude Code uses the 1-hour cache internally

All cache_creation.ephemeral_5m_input_tokens are 0; all cache_creation flows into ephemeral_1h_input_tokens. So within-session idle up to ~60 min should stay warm.

Observation 2 — Baseline (mid-session, same jsonl)

gapcache_creationcache_readhit %
3.3m389155,49299.8%
3.3m37680,99099.5%
3.7m2,123159,70098.7%
4.0m3,209107,45697.1%
4.0m5,112173,72197.1%
4.3m2,015220,49299.1%
4.8m84078,45798.9%
5.7m26482,61299.7%
5.8m3,30285,54996.3%
5.9m1,283240,81899.5%
6.0m2,385161,82398.5%
7.6m4,945114,76395.9%
27.4m1,559150,59399.0%

Average ≈ 98% cache hit. The 1-hour cache works correctly when the session process stays alive.

Observation 3 — Cross-session transitions (same agent, new jsonl file)

transitiongapnext ccnext crhit %
f3f5c819 → e17536e728.2 min40,26000.0%
e17536e7 → 096d96fa4.1 min23,84916,70241.2%
c0eb34ad → b6e07fac2423.6 min (>1h TTL)20,06100.0% (expected)
b6e07fac → f3f5c8192059.3 min (>1h TTL)41,76600.0% (expected)

Observation 4 — Direct TTL-matched pairs

gap (both rows)same-session (baseline)cross-sessiondelta
~4 min97–99% hit (cr > 100k, cc ≈ 3k)41% hit (cr 16.7k, cc 23.8k)−56 pp
~28 min99% hit (cr 150k, cc 1.5k)0% hit (cr 0, cc 40.3k)−99 pp

Same idle duration, same process workload, same codebase state. The only variable is "session boundary crossed vs not." The delta isolates the resume event itself as the cache-breaker.

Alignment with the v2.1.69 deferred_tools_delta root cause

The 4.1-min case shows partial prefix match (16,702 tokens of cr + 23,849 tokens of cc) rather than a clean 0/full miss. This pattern — "prefix matches until block N, then a reshuffle breaks it" — is consistent with the attachment-reordering / tool-result shuffling hypothesis from #42338. A pure TTL miss would show 0% hit with no partial matching at all.

The 28.2-min case shows 0% — consistent with a reshuffle occurring earlier in the prefix (e.g. skill_listing, todo_reminders, or nested_memory injection at a different position, as simpolism catalogued in https://gist.github.com/simpolism/302621e661f462f3e78684d96bf307ba).

Quota impact (one 11h 52m session, 197 unique turns)

metricvalue
cache_read total60,869,087
cache_creation total2,017,110 (all 1-hour, 2× write premium)
input_tokens total9,923
output_tokens total446,687
cc / output ratio4.52×

For context, well-cached sessions typically run at 1–2× cc/output. The 4.5× on this session was dominated by a single cross-session 28.2-min transition (40k cc that a TTL-preserving resume would have saved) plus two honest >1h TTL misses.

For subscription-plan users, this directly consumes the rolling 5-hour quota. On this agent's overnight watchdog-driven restarts, the bug caused ~20 % of a 5-hour quota to be burned on resume-induced cc alone.

Reproduction

  1. Start a Claude Code session, accumulate ≥ 100k tokens of context.
  2. Exit (/exit) and immediately restart with claude --continue within 1–5 min.
  3. On the first turn of the resumed session, check usage.cache_read_input_tokens vs cache_creation_input_tokens in your monitoring proxy or in the jsonl under ~/.claude/projects/<slug>/<sid>.jsonl.
  4. Compare to a control: take a turn inside the original session after the same idle gap (no exit). Baseline is ≥ 95% cache_read.

Environment

  • macOS 15.x (Darwin 25.4.0), Apple Silicon (M4)
  • Claude Code 2.1.116
  • Subscription plan
  • Active features during affected sessions: custom agents, MCP servers (fetch, sequential-thinking, filesystem, memory, discord plugin, computer-use, gmail, google-calendar), hooks (SessionStart, SessionEnd, PreCompact, PostToolUse), skills, CLAUDE.md

Asks

  1. Reopen or fold this into an active tracking issue so the status is discoverable.
  2. Confirm whether the known attachment-reordering fixes (v2.1.90/91 et al.) are expected to cover custom-agent + MCP + skill combinations, or if there are known unfixed paths.
  3. If a partial/staged fix is planned, consider exposing a --no-deferred-tools-delta or --strict-prefix opt-out flag so harness users can trade feature completeness for cache stability.

References

extent analysis

TL;DR

The issue can be mitigated by implementing a fix for the attachment-reordering issue that causes cache invalidation on session resume, potentially through exposing an opt-out flag like --no-deferred-tools-delta or --strict-prefix.

Guidance

  • Verify that the issue is indeed caused by the attachment-reordering problem by checking for partial prefix matches in the cache read and creation tokens, as seen in the 4.1-min case.
  • Investigate whether the known fixes for attachment-reordering (v2.1.90/91 et al.) cover custom-agent + MCP + skill combinations, and if there are known unfixed paths.
  • Consider exposing an opt-out flag like --no-deferred-tools-delta or --strict-prefix to allow users to trade feature completeness for cache stability.
  • Review the provided references, including #42338, #34629, and #46829, to understand the history and context of the issue.

Example

No code snippet is provided as the issue is more related to the underlying logic and configuration of the Claude Code system rather than a specific code error.

Notes

The issue is complex and involves the interaction of multiple components, including custom agents, MCP servers, and skills. The provided methodology and observations suggest that the issue is related to the attachment-reordering problem, but further investigation is needed to confirm the root cause and develop a comprehensive fix.

Recommendation

Apply a workaround by implementing a fix for the attachment-reordering issue, potentially through exposing an opt-out flag, as this is likely to mitigate the cache invalidation problem on session resume.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING