the wizard

Prompt caching explained: why an LLM re-reads your whole chat

A model keeps nothing between requests, so every chat message re-sends everything before it. How prompt caching skips re-reading it, measured on a real model.

Part 4 of 6 · How an LLM runs
  1. How an LLM works: from your text to the next token
  2. How attention works in an LLM, with real numbers
  3. What the KV cache is, and why LLMs need it
  4. Prompt caching explained: why an LLM re-reads your whole chat
  5. What an inference engine does: batching, KV memory and quantization
  6. Why LLMs run on GPUs: reading a prompt vs writing a reply

About this series

Part 3 ended with a catch: the KV cache is built for one request. A model keeps nothing between requests. This part is about what that means for a chat, and about prompt caching, the fix providers offer for it.

Measurements come from SmolLM2-135M, a small open model, on one CPU core. Provider details come from Anthropic's and OpenAI's documentation as of October 2026.


The model forgets, so the app re-sends

The weights never change while a model runs (part 1), and the KV cache only lives for one request (part 3). So nothing about your last message is inside the model when your next one arrives.

A chat still works because the app sends the whole conversation every time. Anthropic's API docs say it directly: "The Messages API is stateless, which means that you always send the full conversational history to the API."

So a chat gets more expensive with every turn. If each turn adds 500 tokens (your message plus the reply), turn 20 sends all 10,000 tokens of the chat so far. Over 20 turns that's 105,000 input tokens for 10,000 tokens of actual conversation, and the model reads every one of them again.

A model also has a limit on how much it can read at once (Llama 3's is 128K tokens). A chat that outgrows it can't be sent whole, and the app has to decide what to leave out.

Prompt caching: keep the KV cache of the start

Most of what a request sends was in the previous request too: the instructions, any documents, the earlier turns. Reading them again produces exactly the same keys and values every time.

Prompt caching keeps them. The server saves the KV cache for the start of a prompt, and a later request that begins with exactly the same tokens skips straight to the new part. Both providers say this is what's stored: Anthropic's docs mention "KV (key-value) cache representations", OpenAI's "key/value tensors".

I measured the same thing on SmolLM2. A 256-token set of instructions for a library help desk, then three different questions:

Read everything Reuse the saved start
Question 1 (12 tokens) 1.17 s 0.10 s
Question 2 (15 tokens) 1.16 s 0.13 s
Question 3 (11 tokens) 1.14 s 0.09 s

The next-token scores were identical either way. The saved start cost 1.51 s to make once.

Why it has to match exactly

Anthropic's docs: "Cache hits require 100% identical prompt segments." The reason is attention. I changed one early word, "helpful" to "friendly", in "You are a helpful assistant for a city library. Answer briefly." and compared the keys of the two versions token by token:

Tokens before the change The changed token Every token after it
First layer same different same
Last layer same different all different

At the first layer, a token's key only depends on the token itself and its position. By the last layer, every later token has taken in the changed word through attention, so its keys are different too. A cache is only good up to the first difference.

That gives the one rule of prompt caching: put what stays the same first. Instructions, documents and tool definitions go at the start; the new question goes at the end. A timestamp at the very top changes on every request and breaks the cache for everything after it.

What it costs

As of October 2026, both providers price it in the same shape:

Caching changes what you pay to read the prompt. It doesn't change the reply: Anthropic's docs note it "has no effect on output token generation". Caches aren't shared between organizations at either provider.

Here's the chat from above with those multipliers, assuming every turn arrives while the cache is still alive:

Input tokens a chat sends, billed in normal input tokens: cache writes at 1.25× and reads at 0.1×, the multipliers in both providers' docs.
A chat adding 500 tokens per turn (cache writes 1.25×, reads 0.1× the normal input price)
TurnsInput tokens sentBilled as, no cachingBilled as, with cachingSaved
1027,50027,5008,50069%
20105,000105,00022,00079%
50637,500637,50092,50085%

At 20 turns, 105,000 tokens sent are billed like 22,000. The longer the chat, the bigger the share that is cached reads.

Next, part 5: inference engines, the programs that run a model for many people at once, and what they do with all these caches.


How I measured

← All memories