Skip to the page
Tecyfy

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.

Try itWrite an answer with and without the KV cache

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

Where each fact comes from

  1. 1How caching works
  2. 2Prompt caching
  3. 3What a task costs on Opus 5.5

Put this demo on your own site

Copy the code into any web page: a lesson, a blog post, a course or your team's wiki. The demo runs in a frame and works just as it does here, so your readers can try it where they read. It is free and needs no account, and nothing a reader does in it is sent anywhere.

The short script at the end lets the frame grow and shrink with the demo. Where a page cannot take scripts, choose a fixed height and the frame scrolls.

<iframe src="https://tecyfy.com/embed/demo/kv-cache" title="Write an answer with and without the KV cache" width="100%" height="850" style="border:0;display:block" loading="lazy" allow="clipboard-write"></iframe>
<p><a href="https://tecyfy.com/concepts/kv-cache">Write an answer with and without the KV cache</a> by Tecyfy</p>
<script>addEventListener("message",function(e){if(e.origin!=="https://tecyfy.com"||!e.data||e.data.type!=="tecyfy:height")return;var h=Number(e.data.height);if(!(h>0))return;document.querySelectorAll("iframe").forEach(function(f){if(f.contentWindow===e.source)f.style.height=Math.min(h,20000)+"px"})})</script>