Skip to content

Prompt caching: how it works and what it saves

Prompt caching lets a provider reuse its work on the start of a prompt. Repeated prefixes are billed at a fraction of the input price if they never change.

Published
Updated
Reading time
10 min read

In short

  • Prompt caching stores the model's processing of a prompt prefix. Later requests that begin with the same text skip that work and pay a reduced rate for those tokens.
  • It matches exact prefixes only. One changed character early in the prompt turns everything after it into a miss.
  • At Anthropic's standard rates a cache write costs 1.25 times the input price and a read costs 0.1 times, so caching pays for itself on the second request.
  • Put stable content first and variable content last, keep each session on one provider account, and track the hit rate from the usage fields on every response.

Prompt caching is a provider feature that saves the model's processing of the beginning of a prompt and reuses it when a later request starts with the same text. You pay a reduced rate for the reused tokens, and the response starts sooner because that part of the prompt is not processed again.

A model has to read every input token before it writes the first output token. In most applications the bulk of that input is identical from one request to the next: the system prompt, the tool definitions, a set of reference documents. Caching stops you paying full price for the same text thousands of times a day.

CACHE BREAKPOINTRequest 1ToolsSystem promptDocumentsHistoryNew turnWRITTEN TO CACHEFULL PRICERequest 2ToolsSystem promptDocumentsHistoryNew turnREAD FROM CACHE AT A FRACTION OF THE PRICEFULL PRICE
Caching works on prefixes. Everything before the breakpoint must match exactly to be read back.

How prompt caching works

The provider stores its internal state for a prompt prefix and looks it up by the exact content of that prefix. A request whose opening tokens match a stored entry reads the entry. A request that differs anywhere inside the prefix does not.

That makes order the whole game. Anthropic builds the prefix from tools, then system, then messages, up to a marker you place called a cache breakpoint. A change to a tool definition therefore invalidates the cached system prompt and conversation as well, because they come later in the sequence.

Caching does not change the answer. The model sees the same prompt and produces output the same way. It only skips work it has already done.

What it saves

Cached input tokens are billed at a fraction of the normal input rate. These are the published terms in November 2026.

ProviderHow you turn it onMinimum prefixWrite priceRead priceLifetime
AnthropicA cache_control marker on a content block, or one at the top level of the request for automatic placement512 to 4,096 tokens, by model1.25x input for 5 minutes, 2x for 1 hour0.1x input on most models, lower on the newest5 minutes, renewed on every hit, or 1 hour
OpenAIOn by default1,024 tokens on GPT-5.6 and later1.25x input on GPT-5.6 and later, 2.5x for the 12-hour option, no write charge on earlier models0.1x input on most GPT-5.6 and later models, model-dependent before that30 minutes by default on GPT-5.6 and later, 12 hours optional
Google GeminiImplicit caching is on by default for Gemini 2.5 and newer. Explicit caches are created with caches.create on Gemini 2.5 and 3 models2,048 tokens on Gemini 2.5, 4,096 on newer modelsExplicit caches bill storage for the length of their time to liveReduced rate, listed per model on the pricing pageExplicit caches default to 1 hour

Prices move. Check the linked pages before you put a number in a budget.

The break-even point comes quickly. Take Anthropic's five-minute cache and a prefix you send twice. Without caching you pay for it twice, so 2.0 times its input price. With caching you pay 1.25 for the write and 0.1 for the read, so 1.35. Caching wins on the second request, provided that request arrives before the entry expires.

The one-hour cache needs a third request. Two requests cost 2.0 times input price uncached and 2.1 cached. Three cost 3.0 uncached and 2.2 cached.

There is a second saving that does not appear on the invoice. Anthropic's rate limit documentation says cache reads do not count toward the input-tokens-per-minute limit. A high hit rate raises the traffic you can send before you are throttled.

Caching is one cut among several, and LLM cost optimization ranks the other techniques by effort.

What breaks the cache

Anything that changes the text ahead of the breakpoint breaks the cache. These are the usual causes:

  • A timestamp, request ID, or user name in the system prompt.
  • Tool definitions serialized in a different order from one request to the next.
  • User-specific text placed ahead of content that every user shares.
  • Edits to earlier conversation turns, such as trimming or summarizing history.
  • A prompt shorter than the provider's minimum.
  • Changed thinking settings. Anthropic renders the thinking configuration into the prompt, so switching modes or budgets invalidates cached messages and can invalidate the system prompt and tools too.

Tool order deserves attention if you load tools from MCP servers. The 2026-07-28 revision of the MCP specification recommends that servers return their tool list in a deterministic order, specifically to protect cache hit rates.

The minimum length fails silently. Anthropic's minimum ranges from 512 to 4,096 tokens depending on the model, and a shorter prompt is processed normally with no error and no caching. If your hit rate is zero on a small prompt, check the length first.

History edits put caching in tension with context management. Every time you compact a conversation you rewrite the prefix and pay for a fresh write. Compact in large, infrequent steps instead of shaving a little off every turn.

How to structure a request

Order a request from the most stable content to the least stable: tools, system instructions, shared documents, conversation history, then the new message.

cached-request.ts
import Anthropic from "@anthropic-ai/sdk";
 
const anthropic = new Anthropic();
 
const response = await anthropic.messages.create({
  model: process.env.MODEL_ID!,
  max_tokens: 1024,
  system: [
    { type: "text", text: INSTRUCTIONS },
    // Everything up to and including this block is written to the cache.
    { type: "text", text: PRODUCT_DOCS, cache_control: { type: "ephemeral" } },
  ],
  messages: [{ role: "user", content: question }],
});

Anthropic allows up to four breakpoints in one request. Use more than one when parts of the prefix change at different rates, for example tools that change monthly and documents that change daily.

For multi-turn conversations, use the automatic mode. A single cache_control field at the top level of the request tells the API to place the breakpoint on the last cacheable block and move it forward as the conversation grows.

OpenAI's guidance is the same in different words: put stable instructions and shared reference material first, and move anything dynamic to the end. The same ordering also helps the model, which is why context engineering arrives at it from the quality side.

Prompt caching in agent loops

Agent loops are where prompt caching pays the most and breaks most easily. Every turn resends the system prompt, the tool definitions, and the whole history, so the shared prefix grows on each step. The content that changes on every turn, mostly fresh tool results, belongs after the last breakpoint, not inside a cached block that no later request will read.

Part of the requestHow often it changesHow to cache it
Tool definitionsRarely, if the order is fixedInside the first breakpoint
System promptPer releaseInside the first breakpoint
Shared documentsDaily or weeklyBehind their own breakpoint
Conversation historyAppended every turnBehind a breakpoint that moves forward
Latest tool resultsEvery turnAfter the last breakpoint
New messageEvery turnAfter the last breakpoint

A 2026 study of web-search agents on OpenAI, Anthropic, and Google, across more than 500 sessions with 10,000-token system prompts, found that caching cut API costs by 41 to 80 percent. Caching the entire context, tool results included, was less consistent than excluding dynamic tool results, and could even increase latency.

Long turns have one more trap on Anthropic. The API looks back at most 20 content blocks from a breakpoint for an earlier entry, so a turn that pushes the breakpoint 20 or more blocks past the last cache write pays for a fresh write. An agent that appends many tool calls in one turn can hit this. Add a second breakpoint earlier in the history when turns are that large.

Keep a session on one provider and key

Prompt caches belong to one account at one provider, so every request in a session should go to the same place. Anthropic isolates caches between organizations and, on the Claude API, between workspaces. OpenAI does not share caches across organizations or across regional processing boundaries. A load balancer that alternates keys turns every read into a write.

This matters most when a gateway sits in front of the providers. A router that sends each turn of a 40-turn agent session to whichever key has spare capacity pays a full cache write on every switch. The fix is session affinity: route the first request, then keep the rest of the session on the same provider and key. LLM routing covers how to combine affinity with weighted splits.

OpenAI also offers a prompt_cache_key parameter. On models before GPT-5.6, a stable key influences routing toward the same cache, though it does not guarantee a hit. On GPT-5.6 and later it keeps cache accounting separate per customer or user and helps prevent cache-hit probing across users.

Gateways can handle both halves for you. Bifrost, an open-source AI gateway by Maxim AI, keeps a conversation on the provider and key that served it through session affinity. Its optional auto prompt caching adds breakpoints for clients that send none. Injection is off by default, and markers the caller already set always take precedence.

What it does to latency

A cache hit shortens time to first token, because the provider skips processing the cached prefix. It does not change how fast output tokens arrive afterward. The gain grows with the length of the prefix, so long documents and long agent histories benefit most, and a short prompt sees little difference.

Anthropic states that cached requests generally see improved time to first token for long documents and that caching has no effect on output token generation. The 2026 agent study measured time-to-first-token improvements of 13 to 31 percent across providers.

Concurrency is the catch. On Anthropic, a cache entry becomes available only after the first response that writes it starts. Send 50 requests with the same new prefix at once and all 50 miss. Send one first, wait for its first token, then fan out the rest.

Time to first token explains how to measure the effect and why the client, the gateway, and the provider each report a different number.

How to measure it

Measure the cache hit rate: cached input tokens divided by all input tokens. Every response gives you the numbers.

hit-rate.ts
const u = response.usage;
 
// On Anthropic, total input is the sum of three fields.
const totalInput = u.input_tokens + u.cache_creation_input_tokens + u.cache_read_input_tokens;
const hitRate = u.cache_read_input_tokens / totalInput;

OpenAI reports the same idea as usage.input_tokens_details.cached_tokens.

Record the hit rate on every request and chart it by prompt version. A rate that falls right after a deploy means someone changed the prefix. That is a cost regression, and it is worth an alert. What to record on every request lists the standard attribute names for these counts.

Prompt caching is not response caching

Prompt caching reuses the processing of an input. Response caching, often sold as semantic caching, reuses an output: it returns a stored answer when a new question looks similar to an old one, and never calls the model.

The two carry different risks. Prompt caching cannot return a wrong answer, because the model still runs on the full prompt. Semantic caching can, because similar questions do not always share an answer.

Turn prompt caching on everywhere. Use semantic caching only for traffic where the same questions repeat and a slightly stale answer does no harm, such as a public FAQ bot. Semantic caching covers how to set its threshold and scope so that it fails less often.

Frequently asked questions

What is prompt caching?

Prompt caching is a provider feature that stores the model's processing of the start of a prompt. When a later request begins with exactly the same tokens, the provider reuses that work. The cached tokens are billed at a fraction of the normal input price, often a tenth, and the response starts sooner because less input is processed.

Does prompt caching change the model's output?

No. The model still receives the full prompt and generates its answer the same way. Anthropic states that the response is identical to what you would get without caching. Only the processing of the matching prefix is skipped, which is why prompt caching, unlike semantic caching, cannot return a stale or wrong answer.

How long does a prompt cache last?

It depends on the provider. Anthropic's default entry lives five minutes and renews on every hit, with a one-hour option at a higher write price. OpenAI's GPT-5.6 and later models keep entries for 30 minutes by default, with an optional 12-hour lifetime. Gemini's explicit caches default to one hour and bill storage while they exist.

What is the difference between prompt caching and context caching?

They are the same mechanism under different names. Google calls it context caching: Gemini caches matching prefixes automatically through implicit caching, and also lets you create an explicit cache ahead of time with its own lifetime and storage charge. Anthropic and OpenAI call it prompt caching. In every case, only exact prefixes are reused.

Why is my prompt cache hit rate zero?

Check five things in order. The prompt may be shorter than the provider's minimum, which fails silently. A timestamp or other dynamic value may sit early in the prefix. Tool definitions may arrive in a different order each time. Requests may be spread across organizations or keys. Or parallel requests may all start before the first has written the cache.

Is a prompt cache shared between users?

Within one organization, or one workspace on the Claude API, yes: any request with the same prefix can read the entry, regardless of which end user sent it. Caches are never shared across organizations. On OpenAI's GPT-5.6 and later models, setting prompt_cache_key per user keeps cache accounting separate and helps prevent one user from probing for another's cached prompts.

Sources

  1. 01Prompt cachingClaude API docs
  2. 02Prompt cachingOpenAI API docs
  3. 03Context cachingGoogle AI Studio docs
  4. 04Rate limitsClaude API docs
  5. 05Specification changelog, revision 2026-07-28Model Context Protocol
  6. 06Don't Break the Cache: An Evaluation of Prompt Caching for Long-Horizon Agentic TasksarXiv:2601.06007
  7. 07Auto prompt cachingBifrost docs