Skip to content
Ali Hassan, home

Blog8 min read

Prompt caching across Claude, GPT and Gemini

Prompt caching cuts the cost of resending the same prompt prefix on Anthropic, OpenAI and Gemini. See a worked saving and how to make the cache hit.

By Ali Hassan, Senior engineer for SaaS and AI products

Prompt caching lets you reuse the work a model has already done on the start of a prompt, so you stop paying full price to resend the same tokens on every request. Anthropic, OpenAI and Google all offer it, but the mechanics differ: who switches it on, what a cache hit costs, how long an entry lives, and how short a prompt can be and still cache. This post covers what the feature is, how each vendor does it, how to lay a prompt out so the cache actually hits, a worked saving, and the traps.

What is prompt caching?

Prompt caching stores the model's processed state for a stable prefix of your prompt, the part that does not change between requests. The first call writes that prefix to a cache. Later calls that share it read from the cache instead of reprocessing the tokens. A cache read is billed well below the normal input rate, as little as 5% of it, and that gap is the whole point.

The repeated part is usually a system prompt, a long set of instructions, a tool schema, or a document many questions run against. Reprocessing the same ten thousand tokens on every call is pure waste. OpenAI puts the ceiling at up to 95% off the input rate for a reused prefix; the exact discount, and whether you pay to write the cache at all, is where the vendors differ.

Caching speeds requests up as well as making them cheaper. The model skips reprocessing the shared prefix before it starts writing, which can shave real latency off the first token on a long prompt. Both savings grow with two things: how much of the prompt stays identical across calls, and how often you make them. A prefix that is large and heavily reused is the best case; a short prefix used once is not worth caching at all.

How does Anthropic prompt caching work?

Anthropic prompt caching is explicit. You mark the end of the stable prefix with a cache breakpoint, and Claude writes one cache entry for everything up to it. A 5-minute write costs 1.25x the normal input price, a 1-hour write costs 2x, and every later read is billed at 0.1x the input price on most models, or 0.05x on Claude Sonnet 5.5 and Opus 5.5. The entry refreshes for free each time it is used.

A few rules decide whether it works. The prefix has to clear a minimum length to cache at all: 512 tokens on Sonnet 5.5, Opus 5.5, Haiku 5.5 and the Fable models, more on older ones. The default entry lives 5 minutes, timed from the start of the request, and you can place up to four breakpoints in one prompt. Anthropic's prompt caching docs carry the full table, and Claude API pricing has the per-model token prices.

How does OpenAI prompt caching work?

OpenAI prompt caching is automatic. It is on by default for supported models, with no code change, so you simply see a lower rate on the reused prefix. The prefix is matched from the very first token, which means anything that changes early, a tool definition or a stray timestamp, breaks the match. On GPT-5.6 and later the minimum cacheable prefix is 1,024 tokens, and the cached count is reported rounded down to the nearest 128.

For those models a write costs 1.25x the input rate at the default 30-minute lifetime, and reads cost 0.1x, or 0.05x on GPT-6.1 Sol and a handful of others. Each request can create up to four cache writes, and an idle entry is typically kept for 5 to 10 minutes, up to an hour. OpenAI's prompt caching guide has the per-model detail; OpenAI API pricing and what a ChatGPT feature costs to run cover the rates.

How does Gemini context caching work?

Gemini splits caching in two. Implicit caching is automatic on Gemini 2.5 and newer models: Google spots a shared prefix and passes the saving on with no setup. Explicit context caching is the deliberate kind, where you create a cache object that holds the content and give it a lifetime, which suits a large document reused over a while. The implicit minimum is 4,096 tokens on Gemini 3.8 Flash and Gemini 3.1 Pro.

The two kinds cost differently. An implicit hit just lowers the input price, as the Gemini API pricing guide lays out per model. An explicit cache adds a storage charge, billed by the hour for as long as you keep it alive, so a short system prompt costs pennies while a big document held all month does not. Google's context caching docs have the current rules; explicit caching is not offered on Gemini 4, and the Gemini 3.8 Flash rates step up on 1 January 2027.

How do you structure a prompt so the cache hits?

The rule is the same for all three vendors: put the stable content first and the volatile content last. A cache matches a prefix from the front, so one changing token near the top, a name, a date, a user id, shifts the boundary and you pay full price again. Lay the prompt out so everything shared sits ahead of anything per-request.

  • Lead with the fixed parts: the system prompt, instructions, tool definitions and any document everyone shares.
  • Put the user's question, the retrieved chunks and any per-request context at the end, behind the stable prefix.
  • Keep a changing value such as a timestamp out of the cached section altogether.
  • On Anthropic, set the breakpoint on the last block that stays identical; it looks back at most 20 blocks for a prior write.
  • Check the prefix clears the minimum length, or nothing caches and no error tells you so.

Get it right and the reused prefix drops to as little as 5% of the input price. Get it wrong, with the breakpoint on a line that moves, and you pay the full 100% on every call. You can try the effect on your own numbers in the API pricing calculator.

What does prompt caching save?

Here is the saving on a realistic shape. A support feature sends 2,000 input tokens per request, a 1,500-token system prompt plus 500 tokens of question and context, and gets 400 tokens back, across 100,000 requests a month. That is 200 million input tokens and 40 million output tokens. On Claude Sonnet 5.5 at $2 input and $10 output, the list bill is $800 a month.

Cache the 1,500-token system prompt and, with steady traffic, it reads from the cache on nearly every call:

  • Cached input: 150 million tokens at $0.10 per million, $15
  • Fresh input: 50 million tokens at $2 per million, $100
  • Output: 40 million tokens at $10 per million, $400
  • Total: about $515 a month, plus a few dollars of cache writes

That is roughly a third off the input half of the bill, from one change. Output is now the bigger line, so the next saving comes from shorter answers, not a shorter prompt.

Where does prompt caching go wrong?

Caching can cost more than it saves when it is wired up carelessly. The common mistake is a volatile value near the top of the prompt: it forces a fresh write on every call and never a read, and the write is dearer than plain input, so you pay a premium for a cache you never hit. The rest are variations on the same theme.

  • A changing early token makes you pay the 1.25x write every request and read nothing back. Pin the breakpoint behind the last stable line.
  • Below the minimum length nothing caches and no error is raised, so a short prompt quietly pays full price.
  • An entry that lives 5 minutes expires between bursts, so sparse, spiky traffic may never meet a live cache. A longer lifetime helps, at a higher write or storage cost.
  • With Gemini's explicit caching you pay storage by the hour whether the cache is read or not, so an idle large cache leaks money.
  • Caching the stable half does nothing for output, which is often 80% of the bill once the input side is cheap.

Measure before you cache

Every lever here is a guess until you know what one request costs today and which part is expensive. Caching only pays when a stable prefix truly repeats; a prompt that changes every time has nothing to reuse. Measure the shape of your traffic first, then cache the part that is genuinely shared.

On Evoriqa, my multi-tenant support platform, every AI action draws from one credit balance, so a workspace sees what its AI use costs. On an AI audit reviewer I built for a CPA firm, logging tokens on every call put a review at a measured $0.24-0.28. Caching took the support feature above from $800 to about $515 a month; your own figures will differ. If your model bill is climbing faster than your usage, an LLM cost and RAG quality audit measures cost per request on your real traffic and ranks the changes by what they save.

From the work

  • AI reply grounded in cited help-centre content, shadow-mode draft metrics per channel, and metered AI credits in billing.

    Evoriqa

    My own SaaS: a multi-tenant AI customer-support platform, live in production, built by the person who also pays its inference bill.

    Live in production

    • Next.js
    • TypeScript
    • PostgreSQL
  • Exception report, upload screen and style-guide admin of an AI tool that reviews financial statements for $0.24 each.

    AI audit reviewer

    A draft financial-statement package goes in; a categorised exception report comes back in about 90 seconds, at a measured $0.24-0.28 per review.

    • Python
    • FastAPI
    • Anthropic Claude

The offer this article leads to: AI accuracy and cost audit, typically $1.5k-$3k.

Tell me what you’re building and where it’s stuck.

I’ll tell you the cleanest path forward, including if it’s “don’t build that.”

Email only, on purpose: it reaches me, not a sales team.

Or write tocontact@alihassan.dev

Ali Hassan in a dark winter jacket, looking off to one side, standing in a stone courtyard with a minaret and cloudy sky behind him.