diffox
Blog

Prompt caching: 8 best practices for Claude, OpenAI and Gemini

Prompt caching serves a prompt’s repeated prefix from a cache. Here is how to turn it on at four providers, how long it stays warm and how to raise the hit rate.

ShareXLinkedIn

Summary. Prompt caching processes the unchanging start of a request (its prefix) once, then reads it from the cache on later requests that begin with the same prefix. On 15 October 2026 we read the rules of Anthropic, OpenAI, Google and Amazon Bedrock in their own documentation, along with how Claude Code manages its cache.

  • The cache holds the prefix, not the response. The model still generates a new response every time; it just skips reprocessing the prefix. Semantic caching, which returns an old response, is a different technique.
  • On most models a cache read costs a tenth of the base input price. With Anthropic’s five-minute cache a write costs 1.25 times that price, and so it does on OpenAI’s newer models. Two requests then cost 1.25 + 0.1 = 1.35 times the base price against 2 without a cache, so the cache pays for itself on the first read.
  • A cache only helps while it is warm. Depending on the provider and the setting, that lasts minutes to hours; on Anthropic, OpenAI and Bedrock every hit resets the clock.
  • Hits depend on a prefix that does not change. Put stable content first and changing content last; a single timestamp in the prefix makes every request write the cache again.

What does prompt caching store?

4providers: Anthropic, OpenAI, Google, Bedrock
0.1×cache read price on most models
8rules for cache hits

A language model remembers nothing between requests. Send the same system instructions, the same document or the same code context a hundred times, and the model processes it a hundred times and you pay a hundred times. Prompt caching changes that: the provider processes the unchanging start of the request once and stores it; later requests that begin the same way read it from the cache.

What gets stored is not the text itself but the intermediate states the model computes while processing it; we unpack this in the next section. That is why the match has to be exact: if the prefix is not identical byte for byte, it cannot be read from the cache.

Three ideas often get mixed up:

KV cache, prompt caching and semantic caching: what each stores and how it affects the response
ConceptWhat it storesWhere it runsResponse
KV cacheIntermediate states computed for one request’s tokensInside the model, for a single responseEvery response is generated afresh
Prompt caching (prefix caching)A shared prefix’s intermediate states, across requestsIn the provider’s API, or in a server such as vLLMGenerated afresh; the cache does not change the output
Semantic cachingEarlier responses, matched by the meaning of the questionIn the application’s own layer, as in GPTCacheAn old response comes back; it can be a false match

Source: the documentation of OpenAI, Anthropic, vLLM and GPTCache, 15 October 2026.

We deal with this every day: when diffox reviews a pull request, many specialist agents read the same context in separate calls (how that works is covered in another post).

How the cache works

Before a language model starts generating a response, it reads the whole input; this phase is called prefill. Along the way it computes intermediate states for every token, called keys and values. OpenAI’s documentation calls them KV states: the model uses them to look back at earlier tokens while it generates the response. That is what prompt caching stores: not the text itself, but the tensors the model computed while processing it.

The cache helps up to the first change. Request 1: system + doc + question A. Processed from scratch: All of it. The prefix is written to the cache. Request 2: system + doc + question B. Processed from scratch: Only question B. System and doc are read from the cache. Request 3: date + system + doc…. Processed from scratch: All of it. A date added at the start breaks the match. Example: three requests for the same document.
Example; the block names are illustrative. The matching rule is from vLLM’s design document, 15 October 2026.

That is where the gain comes from. If the prefix is in the cache, the model skips computing that part and processes only the new tokens. vLLM’s documentation also says where the gain stops: the cache speeds up processing the input, not generating new tokens. So the work that gains most from caching has a long prefix and a short response. The Prompt Cache paper measured time-to-first-token improvements of up to 8× on GPU and 60× on CPU.

This structure also explains why the prefix must be identical byte for byte. vLLM’s design document identifies each block in the cache by two things: the block’s own tokens and the tokens of the prefix before it. So the same sentence is a different block when different text comes before it. Add a single date at the start and every block after it becomes a new block; the cache only helps up to the point where something changed.

These states are not small, either. The PagedAttention paper notes that the KV cache memory of each request is huge and grows and shrinks over the course of the request. That is why providers keep caches for only minutes: according to Google Cloud’s documentation, in-memory cache is limited and entries are evicted as capacity is reclaimed. Anthropic also says these representations are held in memory only and are not stored at rest.

How to enable prompt caching at four providers, and what it saves

All four providers support caching, but they differ in how you turn it on, how long the prefix must be and what it costs. We took the table from each provider’s own documentation on 15 October 2026.

Prompt caching on four providers: how to enable it, the shortest prefix, and the write and read price
ProviderHow to enableShortest prefixWriteRead
Anthropiccache_control: one field on the request, or up to 4 breakpoints on blocks512–4,096 tokens, by model1.25× (5 min), 2× (1 h)0.1×; 0.05× or 0.025× on newer models
OpenAIOn by default; GPT-5.6 and later also take explicit breakpoints1,024 tokens (GPT-5.6 and later)1.25× (GPT-5.6 and later); no extra charge on earlier models0.1× on most GPT-5.6+ models
Google GeminiImplicit cache on by default in 2.5 and later; explicit cache with cachedContents2,048–4,096 tokens, by modelStandard input price; an hourly storage fee on the explicit cacheBy model; a 90% discount on Google Cloud
Amazon BedrockImplicit cache (not guaranteed) and the cachePoint marker512–4,096 tokens, by modelBy model; can be higher than standard inputThe model’s cache-read price

Source: the providers’ documentation, 15 October 2026. Multipliers are relative to the uncached input price. The Google Cloud row covers Gemini 2.5 and later.

On Anthropic and on OpenAI’s GPT-5.6 and later models, writing to the cache costs more than sending the same tokens uncached, and reading costs far less. So a cache that writes at 1.25× pays for itself on the first read, and Anthropic’s one-hour cache, which writes at 2×, on the second. On Gemini’s explicit cache a write costs the standard price, but a storage fee is charged for every hour the cache is kept; whether it pays off depends on how many reads you get and how long you keep the cache.

In the Claude API, when many calls read the same context, put the breakpoint on the shared block and each call’s own instruction after it. ephemeral is currently the only supported cache type; the model name comes from the documentation’s own example:

{
  "model": "claude-opus-5-5",
  "max_tokens": 1024,
  "system": [
    {
      "type": "text",
      "text": "<shared context: the same text on every call>",
      "cache_control": {"type": "ephemeral"}
    },
    {
      "type": "text",
      "text": "<this call’s own instruction>"
    }
  ],
  "messages": [
    {"role": "user", "content": "<this call’s question>"}
  ]
}

How long does the cache stay warm?

An entry in the cache does not last forever: it has a lifetime, its TTL (time to live). Claude Code’s documentation describes the state in two words: while the entry is within its TTL the cache is warm, and once the TTL has run out it is cold. On a warm cache the next request reads the prefix cheaply; on a cold one it processes the prefix from scratch and pays the write price.

How long the cache stays warm, and whether a hit refreshes it, by provider
ProviderDefault lifetimeLonger optionDoes a hit refresh it
Anthropic5 minutes1 hour ("ttl": "1h", write 2×)Yes, free of charge
OpenAI, GPT-5.6 and laterAt least 30 minutesNone; the only value is 30mYes, with no new write charge
OpenAI, earlier models24h; in organisations with zero data retention (ZDR), in_memory: typically 5–10 minutes, up to 1 hour24h: typically 30 minutes, up to 24 hoursNot stated in the documentation
Google Gemini1 hour on the explicit cacheAny lifetime you set with ttl or expire_timeNot stated in the documentation; you extend it by hand
Amazon Bedrock5 minutes1 hour, on supported modelsYes

Source: the providers’ documentation, 15 October 2026. The documentation does not state the lifetime of Gemini’s implicit cache; on Google Cloud it is at most 24 hours, and most entries are evicted sooner.

The TTL works like a timer, and two details matter. On Anthropic the timer runs from the start of the request, not from the end of the response: if a response took 4 minutes to stream, the next request that reads the same prefix must start within about 1 minute. And on Anthropic, OpenAI and Bedrock every hit restarts the timer, so steady traffic keeps the cache warm on its own.

Most providers have no endpoint that tells you whether the cache is still warm. The exception is Gemini’s explicit cache: cachedContents.get always returns the expiry time (expireTime), though not the content. On Anthropic, OpenAI and Bedrock the signal is the next response: if the number of tokens read from the cache has dropped to zero, either the prefix changed or the entry went cold. Anthropic’s cache diagnostics tell the two apart: if the requests are identical and the read is zero, the entry has expired.

In Claude Code, the TTL depends on how you pay. According to the documentation, on a Claude subscription the main conversation uses a 1-hour cache as long as you stay within your plan’s included usage. Once you move to usage credits, or if you use an API key or a cloud provider, it uses a 5-minute cache. Subagents get 5 minutes even on a subscription. You can change the TTL with the promptCacheTtl setting or the CLAUDE_CODE_PROMPT_CACHE_TTL variable.

You can see the state in three places. /usage says whether the cache is warm or cold, along with the TTL in effect. In the JSON the status line receives, the prompt_cache object has the fields warm, ttl and expires_at; the last one is the moment the cache goes cold. And the cache clock in the VS Code extension counts down from a 5-minute or 1-hour lifetime and shows the minutes left; on a 1-hour cache it starts at 60 minutes.

Three things to watch:

  • The gap between requests must stay under the TTL. If requests come less often than every 5 minutes, each one writes the prefix again. Anthropic recommends staying on the 5-minute cache if the prefix is used more often than every 5 minutes, because refreshing it is free.
  • A longer TTL has a price. On Anthropic a 1-hour write costs 2×, and it does not pay for itself until the prefix has been read twice; Gemini’s explicit cache charges storage for every hour.
  • The first request after a long gap is expensive. On a cold cache the first request writes the whole prefix. You can warm the cache before the real request arrives: on Anthropic with max_tokens: 0, and on OpenAI’s GPT-5.6 and later models with prompt_cache_options.prewarm.

In a ten-call example, the prefix costs 78.5% less

The numbers are round and hypothetical; they only illustrate the documented multipliers. Say 10 calls read a shared prefix of 50,000 tokens. The unit is one token billed at the base input price; we take a write as 1.25× and a read as 0.1×. Anthropic’s 5-minute cache and most of OpenAI’s GPT-5.6 and later models use these multipliers.

uncached       10 × 50,000 × 1      = 500,000

cached          1 × 50,000 × 1.25   =  62,500   first call writes
              + 9 × 50,000 × 0.1    =  45,000   nine calls read
                                    = 107,500
Example: a shared 50,000-token prefix, 10 calls. Unit: one token billed at the base input price. Uncached (10 calls, all at full price): 500,000 tokens; One first, then nine (1 write + 9 reads): 107,500 tokens; All at once (worst case: 10 writes): 625,000 tokens.
A worked example, not a measurement: what 10 calls reading a shared 50,000-token prefix pay for that prefix, at the documented multipliers (15 October 2026). Each call’s own part and the output tokens are left out.

Ten calls started one after another pay the equivalent of 107,500 tokens for the prefix: 78.5% less than the 500,000 they pay without the cache. On a model whose read multiplier is 0.05× the total falls to 85,000. On the 1-hour cache the write rises to 100,000 and the total to 145,000.

The third bar is the worst case: the same ten calls all start at the same moment. None of them can read what another wrote, so if each one writes the prefix itself, the total is 625,000. That is 25% more than not turning the cache on at all; the section on parallel calls below explains why.

8 rules for a high cache hit rate

The four providers give the same advice on cache hits: each rule below appears in the documentation of at least two of them.

8 rules for cache hits. Each is in at least two providers’ docs. Stable content first, changing last: The breakpoint goes on the stable part’s last block. No timestamp in the prefix: No date, request ID or user-specific data. Keep the tools stable: Same names, schemas and order; disable, don’t remove. Don’t switch model or settings: Choose them at the start, not mid-conversation. Don’t rewrite the history: Append the new message; summarising resets the cache. Keep the cache warm: Send the same prefix regularly, within the TTL. Reach the minimum length: A shorter prefix silently stays out of the cache. Measure your hits: Read from the cache / total input.
Source: the documentation of Anthropic, OpenAI, Google and Amazon Bedrock, and Claude Code’s documentation and release notes, 15 October 2026.

The second rule is the one most often skipped. One line in Claude Code’s release notes shows why it matters: moving the date out of the system prompt raised the hit rate. The date changes every day, and if it sits at the start of the prefix, the first request of each new day rewrites the whole prefix.

Two rules are specific to one provider. On OpenAI’s models before GPT-5.6, you give requests that share a prefix a stable prompt_cache_key. It helps route them to the same cache, and the documentation recommends about 15 requests per minute per key. Anthropic asks for a stable order of JSON keys in tool calls: according to its documentation, some languages such as Swift and Go randomise key order, so the same content goes out as different bytes.

What invalidates the cache

On Anthropic and Bedrock the cache follows a hierarchy: tools, system, messages. A change at one level invalidates that level and every level after it. Here are six of the cases Anthropic’s documentation lists:

Six things that break the cache. The earlier the change, the more is lost. tools Tool definition changed: The whole cache is invalidated. system Web search turned on: system, messages invalidated. messages tool_choice changed: Only messages invalidated. breakpoint On a changing block: Requests write but never read. ttl The TTL ran out: The next request writes again. length Prefix too short: Not cached, and no error.
Source: Anthropic’s prompt caching documentation, 15 October 2026. The first three cards come from its “What invalidates the cache” table.

Turning citations on or off has the same effect as web search, and adding or removing images in the prompt has the same effect as tool_choice. The fourth card is what the documentation calls a “common mistake”: if the breakpoint sits on a block that contains a timestamp or the user’s message, every request writes a new entry and no request can read one. The system looks back at most 20 blocks, but it is looking for an entry an earlier request wrote, not for unchanged content. The cache is written only at the breakpoint. So the breakpoint goes on the last block that stays identical across requests.

Caching with parallel calls

It is tempting to start all the calls that read the same prefix at once. Anthropic’s documentation is clear on this: for concurrent requests, a cache entry only becomes available after the first response begins; if you need parallel requests to read from the cache, wait for the first response before sending the rest. OpenAI also notes that its cache lives on individual machines, and that more than 15 requests a minute can be routed to other machines.

How many of ten calls read the cache?. Start: 10 calls at once. Read from the cache: 0 calls. With no entry yet, each call processes the prefix. Start: 1 call, then 9. Read from the cache: 9 calls. Once the first response begins, the nine go together. Start: max_tokens: 0, then 10. Read from the cache: 10 calls. The warm-up writes and returns without output. Example: 10 calls reading the same prefix. The first card is the worst case.
Source: Anthropic’s documentation, the “Cache limitations” and “Pre-warming the cache” sections, 15 October 2026. The call counts are an example; the first card shows the worst case.

The first response does not have to finish, only to begin. The documentation offers three ways to do this:

  • Send the first call on its own. Claude Code does this itself when a workflow starts several agents with the same prefix at once: it holds all but the first until the first agent’s response begins, for at most 5,000 milliseconds by default.
  • Warm the cache in advance. A request sent with max_tokens: 0 writes the cache at the breakpoint and returns without generating output; no output tokens are billed. The breakpoint must sit on the shared block, and the thinking setting and the output_config.effort value must match the real calls. The request is rejected if it comes with settings that need output (stream: true, for example).
  • Use the 1-hour cache for batch work. In the Batch API, requests are processed in any order and a read is not guaranteed. The documentation recommends sending the single request that carries the shared prefix first, on the 1-hour cache, and the rest once it completes.

How to measure your hit rate

Don’t assume the cache is working; check the response. All four providers report the tokens read from the cache in a separate field of the response’s usage data:

The response field for tokens read from and written to the cache, by provider
ProviderRead from the cacheWritten to the cache
Anthropiccache_read_input_tokenscache_creation_input_tokens
OpenAIinput_tokens_details.cached_tokens (prompt_tokens_details in Chat Completions)input_tokens_details.cache_write_tokens
Google GeminiusageMetadata.cachedContentTokenCountNo separate field
Amazon BedrockcacheReadInputTokenscacheWriteInputTokens

Source: the providers’ documentation, 15 October 2026.

The hit rate is the share of total input that was read from the cache. OpenAI defines it this way and recommends aggregating it by user, workspace or day. Getting total input right matters: on Anthropic and Bedrock the input field does not count the cached tokens, so the total is the sum of three fields; on Gemini, promptTokenCount already includes the tokens read from the cache.

EXAMPLE. Add up all three. usage: "usage": { /   "input_tokens": 50, /   "cache_creation_input_tokens": 0, /   "cache_read_input_tokens": 100000 / }. Input: After the last breakpoint. Write: Written to the cache by this request. Read: The prefix read from the cache.
Values from the documentation’s own example: 100,000 tokens read from the cache, 50 tokens after the breakpoint; total input 100,050 tokens.

Total input is the sum of three fields. input_tokens alone does not show all of the input; it counts only what comes after the last breakpoint. When everything works, the write field on the first call equals the length of the prefix, and on later calls the same number shows up in the read field. If both fields are zero, the prompt was not cached; the most likely reason is a prefix under the minimum length. If the write field stays high on every call, something in the prefix is changing.

We record each call’s three fields separately. If a call comes back without usage data, we count it as unmeasured, not as zero: a gap recorded as zero looks like a free call.

When caching doesn’t pay

Caching is not worth it for every workload. Before you turn it on, check for these four cases:

  • The prefix is read only once. If a write costs 1.25× and no second call reads the prefix, you pay 25% more.
  • The gap between calls is longer than the TTL. Every call finds the cache cold and writes the prefix again.
  • The prefix is under the minimum length. The request works, the cache does not, and no error comes back.
  • The prefix changes on every request. A date written at the start turns every request into a new write.

The gain is not only in price: with long prefixes, the first token also arrives sooner. Providers differ on rate limits: according to Anthropic and Bedrock, on Claude the tokens read from the cache do not count against the rate limit, while on OpenAI they count towards the tokens-per-minute limit.

In diffox, the specialist agents in a review read the same shared context: the change’s diff and the file context. That context sits in a single block at the start of the request, and that block carries the cache breakpoint. The first call goes out on its own, and the rest start in parallel after it. We record the tokens each call writes and reads separately.

Frequently asked questions

What is prompt caching?

It is a feature of language model APIs: the provider processes the unchanging start of a request (the prefix) once and stores it, and later requests with the same prefix read it from the cache. What is stored is the model’s intermediate states (the KV cache), not the response; the model generates a new response every time. Tokens read from the cache are billed at a small fraction of the base input price, and the response starts sooner.

How long does prompt caching last?

It depends on the provider. On Anthropic the default is 5 minutes, and you can choose 1 hour. On OpenAI’s GPT-5.6 and later models it is at least 30 minutes, and up to 24 hours on earlier models. On Gemini’s explicit cache the default is 1 hour and you set the lifetime yourself. On Bedrock it is 5 minutes, and 1 hour on supported models. On Anthropic, OpenAI and Bedrock every hit restarts the timer.

How does prompt caching work on OpenAI?

It is on by default on supported models. On GPT-5.6 and later the prefix must be at least 1,024 tokens and the cache lasts at least 30 minutes; a write costs 1.25 times the uncached input, and a read 0.1 times on most models. On earlier models a write carries no extra charge; a stable prompt_cache_key helps route requests with the same prefix to the same cache. Hits show in the cached_tokens field.

What is context caching in Gemini?

Gemini has two kinds. The implicit cache is on by default on 2.5 and later models and needs no setup, but the saving is not guaranteed. With the explicit cache you upload the content once with cachedContents and refer to it in later requests; the saving is guaranteed, the default lifetime is 1 hour and storage is billed by the hour. Hits show in the cachedContentTokenCount field.

How long does Claude Code keep its prompt cache?

On a Claude subscription, while you stay within your plan’s included usage, the main conversation keeps it for 1 hour and subagents for 5 minutes. Once you move to usage credits, or if you use an API key or a cloud provider, the main conversation drops to 5 minutes as well. You can change it with the promptCacheTtl setting. /usage shows whether the cache is warm or cold, and the clock in the VS Code extension counts the minutes left.

What is the difference between prompt caching, KV cache and semantic caching?

A KV cache holds the input’s intermediate states while the model generates a single response. Prompt caching keeps those states across requests, for a shared prefix; the response is generated afresh every time. Semantic caching, on the other hand, keeps earlier responses in the application’s own layer, matched by the meaning of the question, and returns an old response to a similar question; it can be a false match.

Does diffox use prompt caching?

Yes. diffox’s specialist agents use Anthropic’s models; the full list of providers is on our privacy page. In a review the specialist agents read the same shared context, which sits in a single block at the start of the request and carries the cache breakpoint. The first call goes on its own, and the rest start in parallel after it.