Skip to content
AgentsTrack

Cache read tokens in coding agents, explained

Cache read tokens are often the largest number in a Claude Code session, and beside a tiny input figure they can look like a bug. With caching on, that split is normal: over 7 days of Claude Code transcripts on one of our machines, 97% of all input-side tokens were cache reads. This guide is for engineers who want to know what the fields mean, what they cost, and what to check when the share drops.

Last updated

Key points

  • Cache read tokens are prompt prefix tokens the API reused from cache. They are priced below uncached input at API rates, and a small input_tokens figure is normal when caching works.
  • In Claude Code, the input side is input_tokens + cache_read_input_tokens + cache_creation_input_tokens. In Codex, cached_input_tokens already sits inside input_tokens, so adding it again double counts.
  • When the read share falls, check first for a changed prefix (model, effort, tools, compaction) and for an idle gap longer than the cache TTL.

What are cache read tokens?

Cache read tokens are input tokens the API served from a cache entry written by an earlier request, so the model did not process them again. Coding agents produce them constantly. Each model call resends the system prompt, the tool definitions and the conversation so far, and one user message often triggers several calls. The unchanged prefix is read from cache, and only the new tail is processed as uncached input.

Matching is exact and prefix-based: a change anywhere in the prefix recomputes everything after it. Anthropic's API docs also set a minimum cacheable length, from 512 to 4,096 tokens depending on the model. Shorter prompts are processed without caching, and the API returns no error.

Cache read vs cache creation vs input tokens

In Claude Code, the input side is input_tokens + cache_read_input_tokens + cache_creation_input_tokens; in Codex, input_tokens already includes cached tokens. Cache creation is also called "cache write". The table uses the transcript field names.

FieldToolWhat it countsInside input_tokens?
input_tokensClaude CodeUncached tokens after the last cache breakpointThis is the field itself
cache_read_input_tokensClaude CodeTokens served from cache on this requestNo, added separately
cache_creation_input_tokensClaude CodeTokens written to a new cache entryNo, added separately
cache_creation.ephemeral_5m_input_tokensClaude CodePart of creation written with a 5-minute TTLWithin creation total
cache_creation.ephemeral_1h_input_tokensClaude CodePart of creation written with a 1-hour TTLWithin creation total
output_tokensClaude CodeTokens the model generatedNot input side
input_tokensCodexAll input tokens, cached ones includedThis is the field itself (includes cached)
cached_input_tokensCodexInput tokens served from cacheYes, a subset
output_tokensCodexTokens the model generatedNot input side

Claude Code's input_tokens covers only what comes after the last cache breakpoint, usually the newest part of the request, so expect a small value. The 5-minute and 1-hour counts sum to cache_creation_input_tokens, so do not add them on top. For the full Codex accounting, see how Codex token usage is calculated.

Claude Code prompt caching cost: the formula

Cost is a sum of four terms, one per token type. R_input is the uncached input rate, R_read the cache read rate, R_write the cache creation rate and R_output the output rate, each per token and per model. Rates change, so take them from Anthropic's current cache pricing.

  • Uncached input: input_tokens × R_input
  • Cache reads: cache_read_input_tokens × R_read
  • Cache creation: cache_creation_input_tokens × R_write
  • Output: output_tokens × R_output

Anthropic prices reads below uncached input and writes above it. If you need the TTL split, replace the creation term with cache_creation.ephemeral_5m_input_tokens × R_write5m + cache_creation.ephemeral_1h_input_tokens × R_write1h, because 1-hour writes cost more.

For Codex, with input and cache reads only (no cache creation or output term), the input side is (input_tokens − cached_input_tokens) × R_input + cached_input_tokens × R_read, with the first term never below zero.

A figure from this formula is an estimate (ESTIMATED). On a subscription (SUBSCRIPTION), it is an API-equivalent estimate that was never invoiced.

A worked example with made-up numbers

These round numbers are invented for illustration. None of them was measured.

  • input_tokens: 1,000
  • cache_read_input_tokens: 900,000
  • cache_creation_input_tokens: 99,000
  • output_tokens: 10,000

The input side is 1,000,000 tokens and the read share is 90%. The cost is 1,000 × R_input + 900,000 × R_read + 99,000 × R_write + 10,000 × R_output.

Now assume a prefix change forces 100,000 cached tokens to be written again. The three input-side fields become 1,000, 800,000 and 199,000. They still add up to 1,000,000, so the total hides the change and you have to watch the read share, which falls to 80%. The cost moves by 100,000 × (R_write − R_read), with the other terms unchanged.

Check your own transcripts with jq

This command needs jq and reads Claude Code transcripts only. It produces one total for every transcript file modified in the last 7 days, across all projects, models and sessions. A recently modified file can hold older lines.

find -L ~/.claude/projects -name '*.jsonl' -mtime -7 -exec cat {} + \
  | jq -cR 'fromjson? | select(.type == "assistant" and .message.usage != null)
      | select((.message.model // "<") | startswith("<") | not)
      | {id: (.message.id // .requestId), model: .message.model, u: .message.usage}' \
  | jq -s '{lines: length,
            naive_output: (map(.u.output_tokens) | add),
            messages: (unique_by(.id) | length),
            output: (unique_by(.id) | map(.u.output_tokens) | add),
            input: (unique_by(.id) | map(.u.input_tokens) | add),
            cache_read: (unique_by(.id) | map(.u.cache_read_input_tokens) | add),
            cache_write: (unique_by(.id) | map(.u.cache_creation_input_tokens) | add),
            cache_write_1h: (unique_by(.id) | map(.u.cache_creation.ephemeral_1h_input_tokens // 0) | add)}'

The command deduplicates by message ID because one API response can appear on several lines. messages can therefore be lower than lines, and naive_output can overstate output. Read share is cache_read divided by input + cache_read + cache_write. The 1-hour share is cache_write_1h divided by cache_write.

For what each transcript line holds, see Claude Code token usage from JSONL.

Over 7 days of Claude Code transcripts on one of our machines, 97% of all input-side tokens were cache reads, under 3% cache creation, and uncached input about 0.001%; 22% of cache-creation tokens were 1-hour writes. One machine is not a benchmark, so compare your result with your own earlier runs.

How long the cache lasts

Per Anthropic's docs, the default cache lifetime on the Anthropic API is 5 minutes. Each use refreshes it at no extra cost, and the clock starts when the request starts, so generation time counts against it. A 1-hour lifetime is available at additional cost. In Claude Code, the TTL depends on how you authenticate and which request it is:

  • Main conversation on a Claude subscription, within plan usage: 1 hour.
  • Main conversation on an API key, a cloud provider, or a subscription drawing usage credits after plan limits: 5 minutes.
  • Subagents and side requests, such as compaction and session titles: 5 minutes. Some server-controlled helpers get 1 hour on a subscription.

In Claude Code v2.1.242 and later, you can set promptCacheTtl (or CLAUDE_CODE_PROMPT_CACHE_TTL) and subagentPromptCacheTtl (or CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL) to 5m or 1h. To see which TTL a request wrote, run:

claude -p "hello" --output-format json

and read usage.cache_creation. A 1-hour TTL helps when your pauses run past 5 minutes. During continuous work, the costlier 1-hour write may buy little.

What invalidates the cache

Anthropic builds the prefix in the order tools, system, then messages. A change at one level invalidates that level and every level after it. Claude Code's prompt caching docs list these as rebuilding the cache:

  • Model switches, including opusplan toggles and automatic fallback.
  • Effort changes on most models. Fable 5.1 and later, Opus 5.5, Sonnet 5.5 and Haiku 5.5 keep the cache on an API key or subscription.
  • Turning on fast mode for the first time in a conversation.
  • MCP servers connecting or being removed, but only when tools load upfront. A server that drops out on its own keeps the cache. With tool search, the default on supported models, a mid-session connect does not disturb the cache.
  • Denying a whole tool by bare name, such as Bash, when tool search is off or unavailable. Scoped rules such as Bash(rm *) do not count.
  • Compaction, dropping old images at image limits, and the first conversation after a Claude Code upgrade.

These keep the cache intact:

  • Editing files Claude Code already read.
  • Editing the project-root or user CLAUDE.md mid-session, which applies after /clear, /compact or a restart.
  • Permission mode changes (except the opusplan switch), output style changes, skills and commands (unless they switch model), /recap and /rewind.

Subagents build their own prefix, so their first request does not read the parent's cache, while a fork does. Different directories build different prefixes. If you call the API directly, a tool definition change invalidates everything, and volatile values such as timestamps break matching.

What to check when the read share falls

Work through these in order.

  1. Run /usage and read the "Prompt cache (main)" line (v2.1.251 or later). It shows the hit ratio, the misses and whether the cache is warm. From v2.1.260 it also names a likely cause for the last miss.
  2. Check current_usage in the statusline. If cache_creation_input_tokens stays high turn after turn, the prefix keeps changing.
  3. Look for an idle gap longer than the TTL, or a switch to usage credits that dropped the main conversation to 5 minutes. If your pauses run past 5 minutes, consider the 1-hour setting.
  4. Look for a model, effort or fast mode switch in that session. Choose all three at the start of the next one.
  5. Look for compaction or a Claude Code upgrade in the window. Expect one rebuild, then check that reads recover.
  6. Look for upfront-loaded MCP or tool changes. Connect servers before the session starts.
  7. Look for a new working directory or extra subagent work. Expect a separate prefix for each.

After a Claude Code fix, check /usage right away. Rerunning the jq command at once still counts lines from before the fix, so compare its totals when those older files have left the 7-day window. A resumed file can still carry old lines.

Codex needs its own check. OpenAI's prompt caching guide says reuse requires the entire rendered prefix to match, and that compaction or truncation can reset it. Divide cached_input_tokens by input_tokens for the hit rate. OpenAI's retention rules differ from Anthropic's TTLs.

What AgentsTrack automates

AgentsTrack applies the four-term version of this formula to your sessions. It prices input, cache read, cache creation and output separately, and it shows the cached share of input. It uses one cache-creation rate and does not price 5-minute and 1-hour writes differently, so read the TTL fields directly when that split matters. Costs are labelled REPORTED, ESTIMATED or SUBSCRIPTION, and a SUBSCRIPTION figure is an API-equivalent estimate that was never invoiced. Install the collector with Node 24 or newer using npm i -g @agentstrack/collector, then agentstrack login and agentstrack start. The default analytics mode uploads a title (usually the prompt's first line) and secret-redacted commands. Metadata mode drops the title and reduces each command to its binary name. Details are in the documentation. To see this in the dashboard, start a 14-day Pro trial; the collector key comes from onboarding.

Read on

Check it against your own sessions

14 days of Pro, no card. The collector is open source, and a dry run prints exactly what it would upload.