Zenaique

Prompt caching shows up on Anthropic and OpenAI pricing pages, what does it actually do?

Flashcard·Easy·4.0 · 0·~30s·Asked atPalantirRobinhoodSwiggy·Relevant atOpenAI
Attempt it
TL;DR

Prompt caching stores the KV state of a prompt prefix server-side so future requests sharing that prefix skip the prefill compute, dropping both cost (cached input billed at a steep discount) and time to first token.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine a coffee shop where every customer asks for the same long list of background details before placing their actual order: 'Hi, I am a tall person, I drink mostly black coffee, I am allergic to dairy, I prefer ceramic cups, I'm in a hurry today, AND I would like a flat white.' The barista has to listen to the whole script every single time. Now imagine the shop remembers your background details from yesterday. You walk in and just say 'flat white' and the barista already knows the rest. The shop charges you a small fee to remember your details, but each subsequent order is faster and cheaper because they skip re-listening to the script. That is prompt caching. The repeated prefix is the script. Skipping the re-read is the savings.

Key concepts

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Prompt caching is the productized form of an inference-engineering observation: prefill is expensive, and most production prompts share long prefixes. A chat app sends the same system prompt every turn. An agent framework includes the same tool definitions on every call. A RAG pipeline re-sends the same retrieved chunks until the user asks a different question. If the inference provider can recognize these repeated prefixes and reuse the work, both cost and latency drop dramatically.

This deep dive walks through what prompt caching actually does inside the server, how the pricing structures of Anthropic and OpenAI map to the underlying mechanism, the prompt-structure discipline required to hit the cache reliably, and the self-hosted equivalents available in vLLM and SGLang. By the end, you should be able to look at any LLM application's prompt template and predict its cache hit rate.

What prompt caching actually does

Recall the structure of LLM inference: prefill processes the prompt and populates the KV cache, then decode generates output tokens by repeatedly consulting that cache. Prefill is the expensive part: it scales with prompt length and dominates time to first token. For a 10,000-token system prompt, prefill might take a full second of compute on H100-class hardware.

Prompt caching observes that if the same prefix appears at the start of many requests, the KV cache state for that prefix is identical every time. So the provider can compute it once, store it in HBM (or fall back to SSD), and reuse it on future requests. When a new request comes in:

  1. The provider tokenizes the prompt.
  2. It hashes the prefix incrementally and checks against the cache index.
  3. If a match is found, it loads the cached KV state directly into the inference engine.
  4. Fresh prefill only runs on the tokens that appear after the cached portion.
  5. Decode proceeds normally from the (cached prefix + fresh tokens) combined KV state.

The match must be an exact tokenized prefix. A single character change near the start of the prompt produces a different tokenization (or at minimum a different token at that position), which means the hash changes, and subsequent tokens' KV values depend on the changed token through self-attention. The cache is invalidated from that point forward.

This is why prompt caching is sometimes called prefix caching: the unit of reuse is always a contiguous run of tokens from the beginning of the prompt. You cannot cache a chunk from the middle; the KV state of position i depends on every position < i through the attention mechanism.

The pricing structure decoded
Prompt structure: stable first, variable last
Self-hosted equivalents: prefix caching and RadixAttention
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
AspectAnthropicOpenAISelf-hosted (vLLM, SGLang)
Discount on cached reads~90% off~50% offFree (you own the GPU)
Write surcharge~25% on first callNoneNone
ActivationExplicit cache_control markersAutomatic for prefixes >=1024 tokensConfigurable feature flag
Typical TTL5 minutes default (extended tiers available)5-10 minutesConfigurable; bounded by GPU/SSD
Cache keyTokenized prefix exact matchTokenized prefix exact matchTokenized prefix; tree-structured in SGLang

Real products, models, and research that use this idea.

  • Claude Opus 4.7 with a 10000-token tool-definitions system prompt sees TTFT drop from ~1500ms to ~200ms when prompt caching is enabled on Anthropic's API.
  • OpenAI's GPT-5.5 automatically caches prefixes of at least 1024 tokens, discounting them at 50% on subsequent matching calls within the TTL.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy does Anthropic charge a write surcharge while OpenAI does not?
A

Anthropic exposes explicit cache_control markers, so users opt in and the surcharge incentivizes thoughtful placement. OpenAI's automatic detection avoids charging for caches the user did not request. Both pricing models can be self-consistent given their UX choice.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Thinking prompt caching saves on output tokens. It only discounts input (prompt) tokens that match a cached prefix; output token billing is unchanged.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Definition of prompt caching as skipping prefill for repeated prefixes

  • Which phase of inference it accelerates and which it does not affect

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
What is the KV cache in transformer inference?
Flashcard·Easy