• AgentArmorHQ teamTeam4d agoNeeds helpSolved

    Why does Claude Code's prompt cache get invalidated mid-session and cache writes spike, and what helped?

    In a long Claude Code session on Opus 4.6 with a Max 20x plan (binary v2.1.87 mentioned in the thread), cache reads dropped to roughly the system prompt size and each later turn wrote a large cache of around 300k tokens, using up quota quickly. The reporter asked why history was being invalidated and whether that cost could be avoided.

    Originally asked on GitHub: https://github.com/anthropics/claude-code/issues/40524

    4 answers

    • Best answerAgentArmorHQ teamTeam4d ago

      Status: the issue was closed as completed, but the thread gives no official maintainer fix or explanation for the cache invalidation.

      A commenter who reverse engineered the standalone binary reported that a native HTTP layer replaces the value of a cch=00000 billing marker in the request body when the request has an anthropic-version header, the URL contains /v1/messages, and the body contains that marker. This analysis was not confirmed by maintainers, and the thread does not show that it causes the cache loss.

      Another commenter said that patching the cch behaviour alone was not enough. They then added "ENABLE_TOOL_SEARCH": "false" under an env key, and cache reads recovered. They applied both changes together, so they could not tell which one mattered, and server-side changes may have happened at the same time.

      json
      {
        "env": {
          "ENABLE_TOOL_SEARCH": "false"
        }
      }
      

      A different commenter noted that tools injected dynamically into early conversation rows may be a separate factor, and that disabling tool search may help with it.

      General advice: watch the cache read and cache write columns in your usage logs. If cache writes jump after a turn, compare against a fresh session.

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What a GitHub user tried on GitHub (Outcome unclear)

      Reporter analysed per-turn token usage and saw cache reads drop and cache writes jump to around 300k tokens mid-session.

      Source: https://github.com/anthropics/claude-code/issues/40524 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @jmarianski tried on GitHub (Outcome unclear)

      Commenter used MITM proxy capture and Ghidra on the standalone binary and identified a replacement of the cch=00000 value in the request body on /v1/messages calls, which they proposed as the cause of cache invalidation.

      Source: https://github.com/anthropics/claude-code/issues/40524#issuecomment-4153829840 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @RebelSyntax tried on GitHub (Partly worked)

      Commenter patched the cch billing header behaviour, which was not enough alone. They then added ENABLE_TOOL_SEARCH set to false in an env key, and cache reads returned only after both changes were applied together. They were unsure which change mattered and noted possible server-side changes at the same time.

      Source: https://github.com/anthropics/claude-code/issues/40524#issuecomment-4155845032 (opens external site)

      Did this work for you?

    Add your answer or comment