Learning resourcesReviewed · Anthropic

How Claude Code uses prompt caching

This guide explains automatic prompt caching for Claude Code users and administrators, but the behavior depends on the model, connection and software version. It covers why cached context stops being reusable, how cache lifetime affects costs, and where to find cache usage metrics.

Researched 14 Nov 2026

In short.

This guide helps Claude Code users and automation teams understand prompt caching, but its advice depends on their model, connection and client version. It explains how Claude Code reuses an unchanged beginning of a request, why model switches and other changes can force uncached processing, and how compaction differs from ordinary conversation growth. It also documents cache lifetime controls, gateway compatibility and usage counters. It is useful for diagnosing unexpected input usage and planning session settings. Exact prices, measured savings and actual behavior in a particular deployment are not established; no software was executed.

Researched and written by an AI model and checked by a second AI model. How it works

Covers the complete selected documentation page at [https://code.claude.com/docs/en/prompt-caching.md], including its text, tables, supplied diagram description, version notices and provider exceptions. Linked documentation, pricing pages and external image assets are excluded. No software was executed; performance, charges and deployment-specific compatibility remain unassessed.

The overview

Resource and scope

How Claude Code uses prompt caching is a documentation guide on the Claude Code documentation site at [https://code.claude.com/docs/en/prompt-caching.md]. It explains a feature Claude Code manages automatically rather than providing a separate tool to install.

Its scope includes request structure, cache invalidation, session resumption, cache lifetime, provider connections, usage monitoring and subagents. It links to API references and other guides, whose contents are outside this assessment.

Practical value

The guide is useful when a Claude Code session suddenly becomes slower or consumes more input tokens. It connects specific actions, such as switching models or compacting history, to the parts of the request that must be processed again.

Its strongest feature is explaining exceptions: an MCP connection does not always invalidate caching, plugin behavior depends on its components, and an instruction-file edit can preserve the cache while leaving the running session's instructions unchanged. These are documented explanations, without measured performance results.

Who it helps

Beginners can use the guide to understand why Claude Code sends conversation history repeatedly, why a model switch can be expensive, and why editing a root CLAUDE.md file does not immediately update an active session.

Experienced developers, Agent SDK users and administrators get more detailed guidance on cache lifetime settings, gateway handling of cache markers, organization-wide configuration and token counters. Readers seeking a complete installation guide, current pricing table or contractual data-handling terms will need additional sources.

What the guide explains

The guide covers model and effort changes, fast mode, MCP tools, plugins, permission rules, compaction, image removal and upgrades. It distinguishes these from actions that generally preserve the cached beginning of a request, including ordinary file edits, appended skill instructions, output-style changes and /recap.

It also explains /usage statistics, status-line fields and organization-level monitoring. For automated workloads, it distinguishes ordinary subagents, which start separate caches, from forks that can reuse the parent's matching context. These are capabilities described by the documentation, not independently demonstrated results.

Using it

Prerequisites and dependencies

Applying the guidance requires a Claude Code session or related automated workload and a connection that supports the relevant caching behavior. Claude Code normally handles caching without separate configuration unless it has been disabled.

The cache runs in the infrastructure serving the model. That can be Anthropic, a cloud provider or a destination behind a gateway. Gateway behavior and provider support therefore matter. The guide specifically says Amazon Bedrock support, minimum cacheable prefix length and one-hour availability vary by model; it does not provide a complete compatibility matrix.

Several controls require particular Claude Code versions. The page does not establish a single minimum version covering every behavior it describes.

How caching works

Claude Code sends the model its context on each request. The API can reuse an identical beginning of that request, called a prefix, instead of processing it again at the normal input rate. This is server-side reuse of previously processed input; the model does not independently remember the conversation between requests.

The request places system instructions and tool definitions first, then project context, then conversation history. An exact match is required. Changing content early in the request makes the content after that point ineligible for the previous prefix match. The guide explicitly distinguishes this from caching individual files or independent segments.

Models and speed modes have separate caches. Effort changes also invalidate caching on most models, with exceptions depending on the model and connection. In ordinary Claude Code use, machine and directory details affect matching too. The underlying API isolates caches between organizations and, on some providers, between workspaces.

Configuration and inspection

For ordinary use, the guide recommends leaving caching enabled and choosing the model, effort level and fast-mode setting early in a session. To inspect main-conversation behavior, /usage reports the hit ratio, miss count and whether the cache is warm. Status-line integrations can read cache_read_input_tokens and cache_creation_input_tokens.

The main-conversation lifetime can be set through promptCacheTtl or CLAUDE_CODE_PROMPT_CACHE_TTL. Other requests use subagentPromptCacheTtl or CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL. Accepted values are 5m and 1h. Environment variables take precedence over their corresponding settings, while FORCE_PROMPT_CACHING_5M=1 overrides both request groups.

These lifetime controls require Claude Code v2.1.242 or later. The session cache statistics require v2.1.251 or later, and likely-cause messages for misses require v2.1.260 or later. No settings were changed and no diagnostic commands were run.

Illustrative session

Suppose a developer asks Claude Code to inspect a file, then asks a follow-up before the cache expires, keeping the same model and request configuration. According to the guide, the unchanged prefix can be read from cache while the new conversation content is processed. Switching models then requires the next request to process the full conversation without using the previous model's cache.

If the developer instead edits the project-root CLAUDE.md file, the current cached prefix remains intact, but the running session keeps its previously loaded instructions. The updated file loads after /clear, /compact or a restart. This is an illustrative application of the documentation, not an executed session.

Assessment & evidence

Limits and failure conditions

Caching depends on matching content, compatible request settings and an unexpired entry. It does not prevent additional input processing when the conversation grows. Compaction replaces history, image-limit handling can remove older content, and tool-definition changes can invalidate part or all of a prefix.

Gateway failures deserve particular attention. The guide says silently removing cache_control markers can make the entire conversation bill as uncached input on every turn. When a gateway instead rejects the marker with the specified error, Claude Code can relocate it to preserve conversation caching while leaving the affected system block uncached.

Actual latency, savings, cache hit rates and compatibility with a particular gateway or deployment are unknown. The page contains no independent runtime evidence establishing those outcomes.

How to interpret the advice

The guide's recommendations follow from the position of each change in the request. New conversation messages leave earlier content unchanged, while changes to system instructions or tool definitions can affect everything after them. This explains why loading a skill can preserve caching while adding an upfront tool definition can invalidate it.

It also explains why preserving the cache is not sufficient evidence that a configuration change has taken effect. Root CLAUDE.md edits preserve caching because the active session retains the old content. Conversely, compaction deliberately rebuilds context and may still be useful when discarded history is no longer needed. A high cache hit rate is therefore one input to session management, rather than the sole objective.

Related approaches and reading

Within Claude Code, the guide distinguishes /recap for a display summary, /rewind for returning to an earlier conversation prefix, and /compact for replacing history with a shorter summary. These serve different needs and have different caching consequences.

For deeper reading, the page links to the underlying API caching reference [https://platform.claude.com/docs/en/build-with-claude/prompt-caching], context-window guidance [https://code.claude.com/docs/en/context-window.md] and Agent SDK cost tracking [https://code.claude.com/docs/en/agent-sdk/cost-tracking.md]. Those linked resources were not assessed. The page provides no comparison with competing coding assistants.

Evidence and confidence

The evidence is the complete documentation page at [https://code.claude.com/docs/en/prompt-caching.md]. It provides detailed behavioral explanations, configuration names, diagnostic fields and version-specific caveats.

This supports describing intended behavior and documented configuration choices. It does not establish observed performance, actual charges or successful operation in any particular environment. No software was executed, and linked pricing, provider and data-usage pages were not assessed.

Access & freshness

Costs and tradeoffs

Cached input is still billed, at the model's cached-input rate. Cache creation also has a cost. The guide says one-hour cache writes cost more than five-minute writes, so a longer lifetime can avoid repeated processing after breaks while costing more during short sessions that would not benefit from the extension. Exact prices and absolute savings are unknown from this page.

By default, main-conversation requests use a one-hour lifetime within a Claude subscription's included usage. Usage credits, API-key billing and cloud providers default to five minutes. Requests outside the main conversation generally default to five minutes, with specified subscription helper exceptions.

Compaction has two effects: a separate request generates the summary, then the shortened conversation establishes a new cache. Summarizing a long conversation after its cache has expired incurs uncached input processing. The page provides no benchmark or complete cost estimate for a particular session.

Licensing and data terms

The page does not state a documentation license or an explicit redistribution grant. It also does not establish licensing or service terms for Claude Code, the Claude API or connected cloud providers.

It identifies where caching happens for different connection types and points to separate data-usage documentation. Detailed retention guarantees, residency commitments and contractual data protections are not established by this page.

Version sensitivity

The document snapshot was captured on 14 November 2026. The page's publication and last-update dates are not stated.

Its version notes show that behavior has changed across releases. Examples include cache-aware model-switch confirmation from v2.1.238, immediate mid-session output-style application from v2.1.251, and the idleCompaction setting from v2.1.290. Readers should match individual instructions to their installed version. The latest available release and behavior in versions beyond the documented claims are unknown.

Useful next steps

Check the installed Claude Code version and connection type, then use /usage or cache token counters to inspect the session. If cache creation remains high across turns, review recent model, speed, effort, tool and plugin changes, along with idle periods long enough for expiry.

Choose a longer lifetime when actual work includes breaks that justify the higher write rate. Check for overriding environment variables before changing lifetime settings. Through a gateway, establish whether cache_control markers and the required headers are preserved.

Use natural task breaks for compaction, and reload root instruction-file changes through an appropriate session reset or compaction. Compare actual usage and charges before concluding that a configuration has reduced costs.

Evidence for this report

selected-page.txtselected · checked 2026-11-14