ByteCosts
claude-code-prompt-cache-settings.mdx

AI Coding Agents

Claude Code Prompt Cache Settings: Every Control, Documented

Claude Code v2.1.242 and later accepts 5m or 1h through promptCacheTtl or CLAUDE_CODE_PROMPT_CACHE_TTL for the main conversation and through subagentPromptCacheTtl or CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL for other requests. Within a Claude subscription’s included usage, the main conversation and a small set of server-controlled helper requests default to one hour. Other requests default to five minutes.

Claude Code sends the conversation context on each turn. When the API finds an eligible cached prefix, it can reuse those tokens at the cache read rate. Claude Code manages caching automatically and lets you configure the time to live (TTL) or disable caching. As of November 30, 2026, start with promptCacheTtl for the main conversation. Check subagentPromptCacheTtl separately if subagents or other requests outside that conversation drive your cache usage.

The two request buckets and their default TTLs

Claude Code decides the TTL per request, and every request falls into one of two fixed buckets:

  • Main conversation: your interactive turns, non-interactive -p runs, and Agent SDK turns, plus the helpers Claude Code runs inline with them.
  • Everything else: the requests Claude Code makes outside that conversation, such as subagents, workflows, in-process teammates, forks, compaction and session titles.

Unless you choose a TTL yourself, Claude Code requests the one-hour TTL only on a Claude subscription within your plan’s included usage:

Request bucketClaude subscription, within plan usageUsage credits, API key, or cloud provider
Main conversationOne hourFive minutes
Everything elseFive minutes, except server-controlled helper requests, which get one hourFive minutes

Once you pass your plan’s usage limit and Claude Code draws on usage credits, the main conversation defaults to five minutes. On the Claude API, 5-minute writes bill at 1.25x base input and 1-hour writes at 2x. Reads bill at the same cached rate either way. The longer TTL can pay off when a follow-up reuses the same prefix after a five-minute entry would have expired. For short bursts, the higher write rate can apply without any added reuse. Measure the gap from the start of the previous request that wrote or read the entry, since time spent generating the response counts toward its lifetime. The 5-minute vs 1-hour TTL comparison works through that break-even.

The TTL controls and their precedence order

Each bucket has a setting and an environment variable, and each control takes 5m or 1h (Claude Code ignores any other value):

BucketSettingEnvironment variable
Main conversationpromptCacheTtlCLAUDE_CODE_PROMPT_CACHE_TTL
Everything elsesubagentPromptCacheTtlCLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL

Both settings and both variables require Claude Code v2.1.242 or later. On API-key or cloud-provider connections, promptCacheTtl: "1h" requests an hour for the main conversation when no higher-precedence control overrides it. Confirm that the serving model and connection support the requested TTL.

When several controls apply, Claude Code takes the first match in this order:

  1. FORCE_PROMPT_CACHING_5M=1, which forces five minutes for both buckets.
  2. The bucket’s environment variable.
  3. The bucket’s setting.
  4. For a subagent’s requests, the cacheTtl value in the subagent’s experimental frontmatter field, which requires Claude Code v2.1.248 or later. Claude Code ignores a 1h there while your Claude subscription is using usage credits.
  5. ENABLE_PROMPT_CACHING_1H=1, which requests one hour for both buckets.
  6. The default for the request’s bucket, from the table above.

FORCE_PROMPT_CACHING_5M=1 is the documented tool for debugging cache behavior, comparing the two TTLs, or overriding a longer TTL set in managed settings.

A settings.json that uses only documented keys

This illustrative settings.json requests a one-hour TTL in both buckets on a supported connection.

{
  "promptCacheTtl": "1h",
  "subagentPromptCacheTtl": "1h"
}

Use the env block of managed settings to set TTL or disable variables across an organization. This illustrative configuration requests a one-hour TTL for the main conversation.

{
  "env": {
    "CLAUDE_CODE_PROMPT_CACHE_TTL": "1h"
  }
}

For the main conversation, check the TTL on a response that actually writes cache tokens. Run claude -p "hello" --output-format json and read usage.cache_creation in the result. One-hour writes appear under ephemeral_1h_input_tokens and five-minute writes under ephemeral_5m_input_tokens. A warm hit can produce zero new writes. On the Claude API, reading an entry written with the five-minute TTL restarts its five-minute lifetime even if the request now asks for one hour. If you get a hit with no writes, wait for the entry to expire without reusing it, then run the probe again.

Two transport notes from the docs. To get the one-hour TTL through a gateway you set with ANTHROPIC_BASE_URL, configure the gateway to forward the anthropic-beta header unchanged, because part of the one-hour request travels in it; through a Claude apps gateway, the administrator requests the one-hour TTL in a gateway policy. On Amazon Bedrock, prompt caching support, minimum cacheable prefix length and one-hour TTL availability all vary by model, so check the AWS documentation if cache token counts stay at zero.

Turning caching off: the DISABLE variables

Disabling is occasionally useful when debugging cache behavior with a specific model or provider. Set any of these environment variables to 1:

VariableEffect
DISABLE_PROMPT_CACHINGDisable for all models
DISABLE_PROMPT_CACHING_HAIKUDisable for the default Haiku model
DISABLE_PROMPT_CACHING_SONNETDisable for the default Sonnet model
DISABLE_PROMPT_CACHING_OPUSDisable for the default Opus model
DISABLE_PROMPT_CACHING_FABLEDisable for Fable only

The Haiku, Sonnet, and Opus switches apply to the models resolved by their respective aliases. For example, with DISABLE_PROMPT_CACHING_SONNET=1, a session pinned to claude-sonnet-5 still uses caching while sonnet resolves to claude-sonnet-5-5. Use DISABLE_PROMPT_CACHING to turn caching off for a pinned model outside the alias. DISABLE_PROMPT_CACHING_HAIKU disables caching wherever the default Haiku model runs, including the main conversation starting with v2.1.283. It also covers a background model set with the deprecated ANTHROPIC_SMALL_FAST_MODEL variable when that model differs from your main model. Anthropic recommends leaving caching enabled for normal use.

Idle compaction and other behaviors that interact with the TTL

If you leave a long conversation idle while it is on the one-hour TTL, Claude Code can compact it. This illustrative settings.json disables idle compaction. The idleCompaction setting requires v2.1.290 or later.

{
  "promptCacheTtl": "1h",
  "idleCompaction": false
}

Compaction itself invalidates the conversation layer: the next request carries a new, shorter history that shares no prefix with the old one, and Claude Code reloads the project-context layer (CLAUDE.md, memory) so that layer rebuilds even when its files are unchanged. The summarization request itself reads your prefix from cache while it is warm, so a mid-session /compact costs a fraction of what the context size suggests; after a gap longer than the cache lifetime, the same /compact reprocesses everything as uncached input. The compaction cost post prices both cases.

During a session, check for model and fast-mode switches, effort changes, changes to upfront MCP definitions, bare tool deny rules such as Bash or Bash(*) when tool search is off, and removal of accumulated images or PDFs. With tools loaded upfront, adding a definition or deliberately removing one invalidates the cache. An unexpected MCP disconnect can leave the cached definitions intact. An upgrade typically changes the prompt or tools when you launch the new version.

Effort changes preserve the prefix by default on Fable 5.1 and later, Opus 5.5, Sonnet 5.5, and Haiku 5.5 with a supported API-key or subscription connection. This exception excludes Amazon Bedrock, Google Cloud’s Agent Platform, Claude apps gateways, sessions with CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS set, and organizations with a HIPAA configuration. Before v2.1.260, an effort change also invalidated the cache for Fable 5.1 on API-key and subscription connections.

Editing a loaded CLAUDE.md, changing output style, or running /recap leaves the existing prefix intact. Permission changes and skills usually preserve it too. Plan-mode toggles with opusplan switch the model, and a skill or command with a different applicable model can switch it for that turn. /rewind can reuse a warm earlier prefix. A regular subagent leaves the parent’s prefix intact and builds its own cache. If a gateway strips cache_control while returning success, the conversation history is billed as uncached input on each turn.

The system prompt embeds auto memory paths, and the conversation opens with the working directory, platform, shell, and OS version. Parallel sessions in the same directory can share a warm cache when their model, speed, and prefix match within the provider’s isolation boundary. Sequential sessions also need a matching startup Git status snapshot.

Reading the /usage Prompt cache (main) line

Run /usage and look at the Session block. After the main conversation’s first API response, Claude Code v2.1.251 or later adds a Prompt cache (main) line. Anthropic’s documentation shows this example.

Prompt cache (main):   14 requests · 91% of input tokens from cache · 2 misses (last 6m 10s ago, 310.2k tokens re-cached) · 1 expected rebuild (compaction or tool-result clearing) · warm (1h TTL, last activity 40s ago)

Reading it left to right:

  • Request count and hit share: 14 requests, 91% of input tokens served from cache.
  • Misses: requests that re-processed content the cache already held, with the time of the last miss and how many tokens those requests wrote back. On v2.1.260 or later the line can also name a likely cause, for example likely cause: tool definitions changed.
  • Expected rebuilds: misses Claude Code caused itself by compaction or tool-result clearing, counted separately and shown only after at least one happened.
  • Warm or cold: whether the cached prefix is still inside its cache lifetime, with the TTL in effect; cold shows how long the session has been idle. If no response has reported cache tokens, the line ends with no prompt caching reported by the API instead, which is the signal to check gateway behavior or model support.

The line uses cache token fields reported by API responses across providers and gateways. It covers the main conversation, and /clear resets it with the rest of the Session block. The same numbers reach a statusline script through the prompt_cache object starting with v2.1.251. The Usage by model block in /usage shows per-model cache read and write totals. For included subscription usage, use the plan usage breakdown on the same screen to see what counts toward your plan.

Billing modeRecommended settingsWhy
Claude subscription, within plan usageLeave defaultsThe main conversation and selected server-controlled helpers request one hour. Other outside-conversation requests default to five minutes.
Subscription drawing on usage creditsKeep 5m for rapid turns. Consider promptCacheTtl: "1h" when you regularly reuse an unchanged prefix after five minutes and before one hour.The billed-usage default is five minutes. On the Claude API, a longer write costs more and can save a rewrite after a break.
API key or cloud providerApply the same timing test to each request bucket. Consider subagentPromptCacheTtl: "1h" when outside-conversation requests reuse a prefix across those gaps.Verify the serving provider’s TTL support and rates. Inspect cache writes to confirm what applied.
Organization policyTTL or disable variables in the env block of managed settingsApplies one policy across users
Debugging a cache problemFORCE_PROMPT_CACHING_5M=1, or a DISABLE_PROMPT_CACHING_* variableForces a known TTL, or isolates caching behavior per model

What to do next

  • Verify promptCacheTtl with claude -p "hello" --output-format json and inspect usage.cache_creation on a response that writes cache tokens. If the command hits an existing entry without writing, repeat after the entry expires. For subagentPromptCacheTtl, inspect a cache-writing response from the outside-conversation bucket.
  • Watch the Prompt cache (main) line in /usage; when misses appear without an obvious cause, the Claude Code cache miss issues post catalogs reported triggers.
  • If you run subagent fleets, their five-minute default and separate prefixes deserve their own design pass: cache-safe subagent forking.
  • Price the trade before you commit to 1h writes: committed per-model rates live in the provider pricing index.

Sources

Interactive report

  • The Five-Minute Clock: A quick review keeps a prompt cache warm. A longer break can turn the next question into another setup charge. Follow the same coding session through both clocks.

How this note was produced

Drafted with AI assistance from committed ByteCosts pricing snapshots and deterministic cost formulas. Figures are grounded in the linked sources (last updated 2026-11-30). Read our editorial & automation policy, inspect the methodology, or report a discrepancy.

Claude Code Prompt Cache Settings: Every Control, Documented. ByteCosts. Updated 2026-11-30. https://bytecosts.com/blog/claude-code-prompt-cache-settings/

Sources