Skip to content

[BUG] Conversation history invalidated on subsequent turns #40524

@jmarianski

Description

@jmarianski

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

While investigating huge token usage I've noticed it come due to fact suddenly my conversation history gets invalidated and all subsequent turns revert to only caching system prompt and huge cache writes.

What Should Happen?

Cache shouldn't drop due to history changes. History should not be updated. Or we shouldn't be charged for historical updates.

Error Messages/Logs

Analysis of token usage from the start of my analysis:

time        cache_read  cache_cr    input    out    model               stop        
----------  ----------  ----------  -------  -----  ------------------  ------------
22:22:48    312377      1944        1        215    opus-4-6            end_turn    
22:23:39    314321      493         3        159    opus-4-6            end_turn    
22:24:19    314814      172         3        108    opus-4-6            end_turn    
22:33:42    0           0           8        1      haiku-4-5-20251001  max_tokens  <-- resume
22:34:26    11428       305735      3        213    opus-4-6            tool_use    <-- irrelevant cache rewrite after restart
22:34:35    317163      579         1        239    opus-4-6            tool_use    
22:34:43    317742      566         1        152    opus-4-6            end_turn    
22:37:13    318308      245         3        96     opus-4-6            end_turn    
07:55:55    0           0           8        1      haiku-4-5-20251001  max_tokens  <-- resume
07:56:22    11428       163547      3        143    opus-4-6            tool_use    <-- partial cache regenerate (wth?)
07:56:40    174975      358         1        90     opus-4-6            end_turn    
07:57:25    11428       307626      3        87     opus-4-6            end_turn    <-- full cache regenerate
07:57:51    319054      108         3        89     opus-4-6            tool_use    
07:58:05    319162      712         1        448    opus-4-6            tool_use    
07:58:21    319874      833         1        367    opus-4-6            end_turn    
07:59:21    320707      393         3        414    opus-4-6            tool_use    
07:59:34    321100      609         1        560    opus-4-6            tool_use    
07:59:47    321709      948         1        512    opus-4-6            tool_use    
08:00:10    322657      615         1        348    opus-4-6            end_turn    
08:03:00    323272      426         3        530    opus-4-6            tool_use    
08:03:12    323698      972         1        468    opus-4-6            tool_use    
08:03:22    324670      529         1        167    opus-4-6            end_turn    
08:03:29    325199      215         3        28     opus-4-6            end_turn    
08:05:30    0           0           8        1      haiku-4-5-20251001  max_tokens  <-- resume
08:05:48    11428       187695      3        155    opus-4-6            tool_use    
08:06:06    199123      876         1        780    opus-4-6            tool_use    
08:06:25    199999      2199        1        1285   opus-4-6            tool_use    
08:06:38    202198      1633        1        302    opus-4-6            end_turn    
08:08:06    203831      481         3        88     opus-4-6            tool_use    
08:08:17    204312      408         1        175    opus-4-6            end_turn    
08:09:16    204720      206         3        154    opus-4-6            end_turn    
08:10:25    204926      228         3        503    opus-4-6            tool_use    
08:10:34    205154      1193        1        507    opus-4-6            tool_use    
08:10:45    206347      1007        1        247    opus-4-6            end_turn    
08:10:54    0           0           8        1      haiku-4-5-20251001  max_tokens  <-- resume
08:11:16    11428       195983      3        136    opus-4-6            tool_use    
08:11:37    207411      616         1        1207   opus-4-6            tool_use    
08:11:49    208027      1457        1        290    opus-4-6            end_turn    
08:12:02    209484      323         3        270    opus-4-6            end_turn    
08:12:27    209807      284         3        190    opus-4-6            tool_use    
08:12:39    210091      314         1        278    opus-4-6            tool_use    
08:13:01    210405      728         1        1219   opus-4-6            tool_use    
08:13:18    211133      1465        1        449    opus-4-6            end_turn    
08:15:28    212598      599         3        325    opus-4-6            end_turn    
08:16:26    213197      334         3        209    opus-4-6            end_turn    
08:18:00    213531      288         3        137    opus-4-6            tool_use    
08:18:07    213819      1131        1        122    opus-4-6            tool_use    
08:18:15    214950      140         1        193    opus-4-6            tool_use    
08:18:29    215090      1114        1        269    opus-4-6            tool_use    
08:18:54    216204      10504       1        336    opus-4-6            tool_use    <-- cache starts breaking down due to history change*
08:19:07    216204      11815       1        228    opus-4-6            tool_use    
08:19:17    216204      12990       1        134    opus-4-6            tool_use    
08:19:38    216204      13341       1        301    opus-4-6            tool_use    
08:20:04    216204      13758       1        426    opus-4-6            tool_use    
08:20:18    216204      15278       1        154    opus-4-6            tool_use    
08:20:46    216204      15778       1        508    opus-4-6            tool_use    
08:22:25    216204      17092       1        208    opus-4-6            tool_use    
08:22:51    216204      17894       1        660    opus-4-6            tool_use    
08:23:22    11428       224502      1        315    opus-4-6            end_turn    <-- cache cannot get regenerated, reverting to full cache write
08:24:47    11428       224953      3        871    opus-4-6            tool_use    
08:25:10    11428       227259      1        597    opus-4-6            tool_use    
08:25:24    11428       228249      1        356    opus-4-6            tool_use    
08:25:43    11428       228669      1        825    opus-4-6            tool_use    
08:26:01    11428       229763      1        468    opus-4-6            tool_use    
08:26:22    11428       230278      1        339    opus-4-6            end_turn    
08:28:07    11428       230642      3        442    opus-4-6            end_turn    
08:37:30    11428       231432      3        430    opus-4-6            end_turn    

---
(Ignore hour, it's another day)
When running "npx @anthropic-ai/claude-code"
21:28:59    11374       46622       1        473    opus-4-6            tool_use    <-- still on standalone binary
22:02:13    0           0           8        1      haiku-4-5-20251001  max_tokens  <-- I tried resuming a couple of times
22:02:23    0           0           340      11     haiku-4-5-20251001  end_turn    
22:02:25    11374       15278       3        21     opus-4-6            end_turn    
22:04:51    0           0           8        1      haiku-4-5-20251001  max_tokens  
22:04:58    0           0           341      11     haiku-4-5-20251001  end_turn    
22:05:00    11374       15194       3        20     opus-4-6            end_turn    
22:09:20    0           0           8        1      haiku-4-5-20251001  max_tokens  
22:09:35    0           0           8        1      haiku-4-5-20251001  max_tokens  
22:12:16    0           0           341      11     haiku-4-5-20251001  end_turn    
22:12:18    11374       15194       3        21     opus-4-6            end_turn    
22:15:36    0           0           8        1      haiku-4-5-20251001  max_tokens  
23:22:46    0           0           8        1      haiku-4-5-20251001  max_tokens  
23:23:06    0           0           341      12     haiku-4-5-20251001  end_turn    
23:23:09    11374       17262       3        19     opus-4-6            end_turn    
23:23:26    28636       27          3        12     opus-4-6            end_turn    
23:31:41    0           0           8        1      haiku-4-5-20251001  max_tokens  
23:31:50    0           0           345      13     haiku-4-5-20251001  end_turn    
23:31:54    11374       17188       3        32     opus-4-6            end_turn      <-- start of npx trials
23:32:25    28562       51          3        167    opus-4-6            end_turn    
23:33:52    28613       320         3        666    opus-4-6            end_turn    
23:34:55    0           0           8        1      haiku-4-5-20251001  max_tokens  
23:35:12    0           0           355      15     haiku-4-5-20251001  end_turn  
23:35:22    11374       17198       3        328    opus-4-6            end_turn    
23:36:50    28572       367         3        500    opus-4-6            end_turn    
23:37:15    28939       506         3        143    opus-4-6            tool_use    
23:37:19    29445       523         1        91     opus-4-6            tool_use    
23:37:56    29968       4869        1        1284   opus-4-6            tool_use    
23:38:06    34837       1343        1        173    opus-4-6            end_turn    
23:38:19    36180       219         3        151    opus-4-6            tool_use    
23:38:27    36399       9511        1        341    opus-4-6            tool_use    
23:38:33    45910       442         73       77     opus-4-6            tool_use    
23:38:37    46352       250         1        77     opus-4-6            tool_use    
23:38:42    46602       1161        1        134    opus-4-6            tool_use    
23:38:59    47763       415         1        369    opus-4-6            tool_use    
23:39:06    48178       427         1        96     opus-4-6            tool_use    
23:39:09    48605       393         1        77     opus-4-6            tool_use    
23:39:13    48605       639         1        152    opus-4-6            tool_use    
23:40:17    11374       38207       3        362    opus-4-6            end_turn    
23:41:35    49581       438         3        766    opus-4-6            end_turn    
23:43:02    0           0           8        1      haiku-4-5-20251001  max_tokens  <-- another session
23:43:23    11374       40201       3        97     opus-4-6            tool_use    
23:43:30    51575       122         1        310    opus-4-6            tool_use    
23:43:35    51697       408         1        152    opus-4-6            tool_use    
23:43:41    52105       219         93       170    opus-4-6            tool_use    
23:43:49    52324       442         1        259    opus-4-6            tool_use    
23:43:54    52766       558         1        102    opus-4-6            tool_use    
23:44:08    53324       2593        1        403    opus-4-6            end_turn    
23:51:33    0           0           8        1      haiku-4-5-20251001  max_tokens  
23:52:30    55917       431         3        187    opus-4-6            tool_use    
23:54:20    56348       292         37       284    opus-4-6            tool_use    
23:54:29    56640       612         158      492    opus-4-6            tool_use    
23:54:54    60657       13          3        508    opus-4-6            end_turn    
23:58:58    60670       717         2        454    opus-4-6            tool_use    
23:59:05    61387       847         1        336    opus-4-6            tool_use    
23:59:23    62234       1282        1        674    opus-4-6            tool_use    
23:59:34    63516       1024        1        506    opus-4-6            tool_use    
23:59:45    64540       583         1        264    opus-4-6            tool_use    
23:59:53    65123       284         1        393    opus-4-6            tool_use    
00:00:10    65407       470         1        887    opus-4-6            tool_use    
00:03:07    65877       1024        1        871    opus-4-6            tool_use    
00:03:16    66901       2098        1        538    opus-4-6            tool_use    
00:03:25    68999       1492        1        379    opus-4-6            tool_use    
00:03:36    70491       1043        1        640    opus-4-6            tool_use    
00:03:43    71534       704         1        233    opus-4-6            tool_use    
00:03:51    72238       250         1        148    opus-4-6            tool_use    
00:03:58    72488       355         1        249    opus-4-6            tool_use    
00:04:03    72843       396         1        259    opus-4-6            tool_use    
00:04:10    73239       435         1        278    opus-4-6            tool_use    
00:04:31    73674       359         1        941    opus-4-6            tool_use    
00:04:45    74033       1595        1        662    opus-4-6            tool_use    
00:05:00    75628       914         1        830    opus-4-6            tool_use    
00:05:18    76542       1610        1        963    opus-4-6            tool_use    
00:05:31    78152       1379        1        640    opus-4-6            tool_use    
00:05:41    79531       1374        1        549    opus-4-6            tool_use    
00:05:51    80905       1576        1        550    opus-4-6            tool_use    
00:06:06    82481       629         1        986    opus-4-6            tool_use    
00:06:20    83110       1102        1        994    opus-4-6            tool_use    
00:06:30    84212       1074        1        578    opus-4-6            tool_use    
00:06:48    85286       2418        1        854    opus-4-6            tool_use    
00:07:01    87704       880         1        555    opus-4-6            tool_use    
00:07:13    88584       818         1        601    opus-4-6            tool_use    
00:07:30    89402       2165        1        520    opus-4-6            end_turn    
00:11:02    91567       542         3        691    opus-4-6            tool_use    
00:11:12    92109       777         1        733    opus-4-6            tool_use    
00:11:25    92886       1042        1        578    opus-4-6            tool_use    
00:11:39    93928       3347        1        694    opus-4-6            tool_use    
00:12:21    98211       39          3        442    opus-4-6            end_turn    
00:13:24    98250       462         3        161    opus-4-6            tool_use    
00:13:32    98712       274         1        178    opus-4-6            end_turn    
00:15:06    98986       190         3        237    opus-4-6            tool_use    
00:15:37    99176       362         1        1202   opus-4-6            tool_use    
00:15:42    6637        16169       2        114    opus-4-6            tool_use    
00:15:44    99538       1427        1        280    opus-4-6            tool_use    
00:15:47    22806       3503        1        160    opus-4-6            tool_use    
00:15:49    100965      2929        1        52     opus-4-6            end_turn    <-- my joy is great at this point
00:15:50    26309       191         1        91     opus-4-6            tool_use    
00:15:54    26500       217         1        92     opus-4-6            tool_use    
00:15:57    26717       152         1        88     opus-4-6            tool_use    
00:16:02    26869       607         3        125    opus-4-6            tool_use    
00:16:06    27476       146         1        166    opus-4-6            tool_use    
00:16:09    27622       430         1        105    opus-4-6            tool_use    
00:16:14    28052       123         1        109    opus-4-6            tool_use    
00:16:19    28052       542         1        176    opus-4-6            tool_use    
00:16:23    28594       440         1        95     opus-4-6            tool_use    
00:16:28    29034       120         1        112    opus-4-6            tool_use    
00:16:32    29154       139         1        180    opus-4-6            tool_use    
00:16:37    29293       256         1        206    opus-4-6            tool_use    
00:17:09    29549       1065        1        82     opus-4-6            tool_use    
00:17:12    30614       131         1        117    opus-4-6            tool_use    
00:17:24    30745       135         1        79     opus-4-6            tool_use    
00:17:28    30745       237         1        100    opus-4-6            tool_use    
00:17:35    30982       140         1        138    opus-4-6            tool_use    
00:17:39    31122       222         1        112    opus-4-6            tool_use    
00:17:46    31344       226         1        257    opus-4-6            tool_use    
00:17:51    31570       288         1        125    opus-4-6            tool_use    
00:17:56    31858       214         1        128    opus-4-6            tool_use    
00:18:01    32072       316         1        139    opus-4-6            tool_use    
00:18:04    32388       487         1        116    opus-4-6            tool_use    
00:18:09    32875       134         1        125    opus-4-6            tool_use    
00:19:29    33009       143         1        5925   opus-4-6            tool_use    
00:19:35    33152       5912        1        113    opus-4-6            tool_use    <-- yup, was at 100% usage at this point
00:19:36    0           0           0        0      -                   -           
00:19:36    0           0           0        0      -                   -           
10:08:54    11374       58355       3        270    opus-4-6            tool_use    <-- costly resume, but cache TTL = 1h in claude code
10:09:00    69729       538         1        277    opus-4-6            tool_use    
10:09:07    70267       7086        180      188    opus-4-6            tool_use    
10:09:14    77353       436         1        201    opus-4-6            tool_use    
10:09:18    77789       219         1        118    opus-4-6            tool_use    
10:09:34    78008       518         1        183    opus-4-6            tool_use    
10:10:40    78526       444         1        95     opus-4-6            tool_use    
10:10:46    78970       1213        1        185    opus-4-6            tool_use    
10:10:55    80183       996         1        270    opus-4-6            tool_use    
10:13:03    81179       602         1        268    opus-4-6            tool_use    
10:18:08    81781       675         1        121    opus-4-6            tool_use    
10:18:14    82456       148         1        226    opus-4-6            tool_use    
10:29:13    82604       823         1        184    opus-4-6            tool_use    
10:29:19    83427       889         1        239    opus-4-6            tool_use    
----------  ----------  ----------  -------  -----  ------------------  ------------


If you provide me with means, I can send you full request/response dumps

*- no idea if this cache breaking was due to me inspecting binary or some historical tool change happened on the background level.

Steps to Reproduce

Write "cch=00000" in command line and ask claude what does he see. He still should see "cch=00000". And token usage should be all "cache read" mostly, not "cache write" for subsequent requests.

Step to temporarily fix: npx @anthropic-ai/claude-code@2.1.34 // you need to fix it on older version to benefit from it

Claude Model

Opus

Is this a regression?

Yes, this worked in a previous version

Last Working Version

Based on reports: 2.1.67

Claude Code Version

2.1.86 (Claude Code)

Platform

Anthropic API

Operating System

Ubuntu/Debian Linux

Terminal/Shell

Other

Additional Information

Similar issue: #34629 - this one relates to immediate start of conversation

Tool I wrote for debugging: https://gitlab.com/treetank/cc-diag

Verification script: https://gitlab.com/treetank/cc-diag/-/raw/c126a7890f2ee12f76d91bfb1cc92612ae95284e/test_cache.py

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions