What is a KV cache, and how does it relate to prompt caching?
Work a model saves from the text it has already read, so it is not redone for every new token. Prompt caching keeps that work between requests and charges less for it.
advanced · Also called Key-Value Cache
There are 2 caches with similar names here. One lives inside a model while it writes a single answer. The other lives at the provider, between your requests, and shows up on your bill. The second is built on the first.
Inside one answer: the KV cache
A model writes one TokenTokenThe small piece of text a language model reads and writes, often part of a word. Prices, limits and the context window are all counted in tokens. at a time, and each new token is worked out from every token before it. To write the 1,000th token it needs information from the 999 before; for the 1,001st, the same 999 plus one more. Without help, it would redo almost all of that work for every single token.
The KV cache is the help. As the model processes each token, its attentionSelf-AttentionThe step in a transformer where every token looks at the other tokens in the same text and takes in what is relevant to it, so meaning depends on context. layers produce 2 small bundles of numbers for it, called a key and a value. The cache keeps them. For the next token the model reuses what is stored and only computes the new token's share. In Hugging Face's words, the cost of each step stops growing with the square of the length and grows only in step with it.
The price is memory. The cache grows with every token in the conversation, in every layer of the model. That is one reason long contexts are expensive to serve, and why people who run models on their own hardware watch the cache as closely as the model's size.
Where did the cat sit?
Without the cache
6 worked out
With the cache
6 worked out
- Worked out at that step
- Read from the cache
Reading the prompt works out a key and a value for each of its 6 tokens. Now write the answer and watch the two grids part.
The prompt and answer are made up, and a real model does this in every one of its layers. One row per step, one square per token in the context: yellow means worked out at that step, green means read back from the cache. The last row of green is what the cache holds in memory, which is its price.
Between requests: prompt caching
A chat or an agent sends the whole conversation again on every turn. Anthropic's example is a task that grows from 20,000 to 120,000 tokens over 40 turns and processes about 2.8 million input tokens in total, because the start is re-sent every time.
Prompt cachingPrompt CachingLetting the API keep the processed start of a prompt for a few minutes, so later requests that begin the same way are cheaper and quicker. lets the provider keep the processed start of your prompt for a while and reuse it. With Claude, reading from the cache costs a tenth of the normal input price, or a twentieth on Opus 5.5 and Sonnet 5.5. Writing it the first time costs a little more than normal. The cache lasts 5 minutes by default, and each use resets the clock. Long documents also start answering sooner.
What breaks it
A cache only matches an identical beginning. The prompt is read in a fixed order (tools, then the system promptSystem PromptInstructions the builder of an AI app gives the model before you type anything. It sets the role, the tone and the rules, and it outranks what users say., then messages), and a change anywhere invalidates everything after it. Put a timestamp at the top of a system prompt and nothing after it can ever be reused.
What to do with this
If you build with a model: keep the fixed parts first and unchanged, and put whatever varies at the end. If you only use AI apps: this is part of why starting a fresh chat for a new subject is cheaper and faster than dragging a long one along.
Sources