The field guide
The KV cache, in plain words
exhibit one hundred and thirty-two The field guide
Published 2026-12-02 (UTC)
A small (human) team and a fleet of AI agents.
What the keys and the values inside a language model are, why an engine keeps them, what they cost in memory, and how to work that cost out for a model before you load it.
ask about this page → assistant.strata2signal.com · in beta, still being tested
hardware on this page: RTX 3090 (24 GB) · RTX 5090 Laptop GPU (24 GB) · RTX 3080 (10 GB) · RTX 5080 (16 GB) · Intel Core i5-12600H · the mini PC → research.strata2signal.com/hardware/ · the roster is in beta, still being tested
the short version
At every layer of a language model, each token is turned into three short lists of numbers. In our words: its query is what it is looking for, its key is what it offers to be found by, and its value is what it hands over when it is found. To write the next token, the model matches the newest token's query against every key so far and blends the values by how well each key matched. A token's key and value never change once they are made, so an engine keeps them: that store is the KV cache. It is saved working, and an engine can rebuild it from the text. For a model whose every layer is ordinary attention, it costs a fixed number of bytes for every token of window (the context length you set); for seven such models on our roster that is 56 to 320 KiB a token at 16 bits, by our arithmetic. For several newer models the simple sum is from 2 to more than 11 times too high, and this page says why.
3,740 words, about 17 minutes to read.
The summary is this page’s own; what was dropped, and why, is in this page’s receipt file.
If you're new here: strata→signal is a small workshop that runs its own machines and writes up what it measures. Several of our guides call the KV cache "the notes"; this page opens the notebook. It measures nothing new. What it says about models and engines is from papers, manuals and source code named under Sources, read on 2026-11-30 (UTC); every figure of our own is from a page of ours that measured it, linked where it stands; and every cache size we worked out ourselves is marked as our arithmetic.
What a layer does with the words so far
A model reads text as tokens, pieces of words. Inside the model each token is a long list of numbers, and the model is a stack of layers that each rework those lists. Most of what a layer does, it does to one token at a time. One part is different. As a current textbook puts it, "the only component that takes as input information from other tokens ... is multi-head attention" (Jurafsky and Martin, chapter 7; its address is under Sources). Attention is where a token reads the text before it.
The paper that introduced this design in 2017 describes attention in one sentence: "An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors" (Vaswani and seven others, section 3.2). In our words, not the paper's:
- the query is what the current token is looking for;
- a key is what a token offers to be matched on;
- a value is what that token hands over when it is matched.
Each of the three is made from the token's own list of numbers by multiplying it with a table of learned numbers: one table for queries, one for keys, one for values. The match between a query and a key is a single number: multiply the two lists entry by entry and add up. A high number means this is what I was looking for. The scores are turned into weights that add up to one, and the token takes that weighted blend of the values.
Three details matter for what follows.
A token looks at the tokens before it and at itself, never at a later one. In the paper's words, each position may attend to all positions "up to and including that position".
Every layer makes its own keys and values. They are not the words. After the first layer, what a layer receives is the layer below's reworking of the token, and that is what it makes a key and a value from.
A layer does this several times side by side, in what the paper calls heads. Many current models give the keys and values fewer heads than the queries; the two papers behind that idea aimed at faster writing, and a smaller cache is the means. That number, the key-value heads, comes back in the arithmetic.
One step, with made-up numbers
Here is one head of one layer at the moment a fourth token arrives. Every number is made up to show the arithmetic. A head in the models on this page is 64 to 512 numbers wide, not two.
| Token | Its key | Its value | Token 4's query (2, 1) against the key | Weight |
|---|---|---|---|---|
| 1 | (1, 0) | (1, 2) | 2 | 0.2418 |
| 2 | (0, 1) | (3, 0) | 1 | 0.0889 |
| 3 | (1, 1) | (0, 4) | 3 | 0.6572 |
| 4, the new one | (-1, 1) | (2, 2) | -1 | 0.0120 |
The weights are the scores put through the same softmax that our guide to the sampler works by hand. The blend of the four values by those weights is (0.5327, 3.1366), worked with the unrounded weights, and that is what this head hands on for token 4. (The 2017 paper also scales each score first; we left that out.)
Now look at what was needed. Of token 4: its query, its key and its value. Of tokens 1 to 3: their keys and their values, and nothing else. Their queries were used when they arrived and are of no further use. The scores and the weights are thrown away as soon as the blend is made.
Why the keys and values can be kept
Because no token ever looks at a later one, nothing that arrives later can change what an earlier token's layers worked out. The documentation of the Hugging Face transformers library says it in one line: "Once a token is processed, its representation never changes with respect to future tokens", so the past keys and values "can be cached and reused".
So an engine keeps, for every layer, the key and the value of every token it has read or written. That store is the KV cache, which several of our guides call the notes. When a new token arrives, each layer makes its query, key and value, adds the key and the value to the cache, and then matches the query against everything in the cache, the token's own key included.
What is not in the cache: the queries, the scores, the weights, the model's own tables of learned numbers, and the model's final ratings of each possible next token.
It is tempting to call the cache the model's memory of the conversation, and our own fit guide comes close. It is narrower than that. It holds nothing the text does not. Throw it away and the engine can rebuild it from the text, at the cost of time. Change one word early in the text and everything after it has to be worked out again.
What it saves, and what it does not
Without the cache, writing each new token would mean running every earlier token through every layer again. The transformers documentation puts the cost of attention for one step as "quadratic with sequence length" without the cache and "linear with sequence length" with it. That is why an engine reads a prompt once, filling the cache, and then writes one token at a time; our page Reading is fast, writing is slow is about why the first is fast and the second slow.
The cache does not make the past free. Each new token still has to match its query against every key in the cache, so writing slows as the cache fills. On an EVGA GeForce RTX 3090 XC3 Ultra Gaming 24 GB, gemma4:26b with about 98,000 tokens really in a 131,072-token window wrote 73.327 tokens a second; with the same window opened and a short prompt it wrote 136.618 (the bench). Opening a big window without filling it cost almost nothing in speed: on a laptop's NVIDIA GeForce RTX 5090 Laptop GPU (24 GB), under its boost budget of 140 to 150 W, the same model wrote 155.2 to 156.0 tokens a second at every window from 4,096 to 131,072 (the bench). What a big window costs up front is memory.
The cache should not change the answer, and in exact arithmetic it does not. Real engines can differ slightly: the transformers documentation says the cache "leads to identical results" and, on the same page, that output "may be slightly different" with it. On our temperature-zero bench, at home on one llama.cpp build, answers changed when a prompt cache was reused, and also when other requests shared the server; read fresh and alone, they did not.
The arithmetic
First, which window. Three numbers go by that name: the longest text the model was trained to take, the room an engine reserves (llama.cpp's -c, Ollama's num_ctx), and the tokens actually in use. The arithmetic is about the second. llama.cpp reserves all of it when it starts, rounded up to a multiple of 256; left unset, it asks for the model's full trained window and may then cut it to fit the card. Ollama, at version 0.40.19, picks its default from the total memory of its cards (4k, 32k or 256k), though its FAQ and its Modelfile page still give older, smaller figures.
For a model whose every layer is ordinary attention, each token adds to the cache:
numbers a token = 2 x layers x key-value heads x head width
bytes a token = that, times the bytes one number takes
the cache = bytes a token x the tokens of window reserved
The 2 is for the key and the value. The other three numbers are in the config.json the model's maker ships beside the weights, as num_hidden_layers, num_key_value_heads and head_dim. Two mistakes are easy to make there:
- Use the key-value heads, not the attention heads. Llama 3.1 8B has 32 attention heads and 8 key-value heads. Using 32 overstates its cache four times.
- Take the head width from its own field when the file has one, not from the hidden size divided by the heads. For Qwen3 4B that division gives 80 and the file says 128.
The bytes a number takes are 2 at 16 bits, llama.cpp's and Ollama's default for the cache.
Worked once, by our arithmetic, for Llama 3.1 8B, from Meta's own published figures: 2 x 32 layers x 8 key-value heads x 128 = 65,536 numbers a token, which at 2 bytes each is 131,072 bytes, 128 KiB. A window of 8,448 tokens is then 1,056 MiB.
That sum can be held to an engine. When llama.cpp sets up its cache it prints the size it reserved, and a bug report in its own tracker, from build 8140, shows the line for this model at this window: "size = 1056.00 MiB ( 8448 cells, 32 layers, 1/1 seqs), K (f16): 528.00 MiB, V (f16): 528.00 MiB" (issue 19844). The same to the last digit.
Seven models on our roster whose every layer is ordinary attention, from each maker's own configuration, by our arithmetic at 16 bits (a KiB is 1,024 bytes):
| Model | Layers | Key-value heads | Head width | A token | At 8,192 tokens | At 32,768 | At the maker's stated maximum |
|---|---|---|---|---|---|---|---|
| Qwen2.5-7B-Instruct | 28 | 4 | 128 | 56 KiB | 0.44 GiB | 1.75 GiB | 7 GiB at 131,072 |
| Llama 3.1 8B | 32 | 8 | 128 | 128 KiB | 1.00 GiB | 4.00 GiB | 16 GiB at 131,072 |
| Qwen3 4B (2507) | 36 | 8 | 128 | 144 KiB | 1.13 GiB | 4.50 GiB | 36 GiB at 262,144 |
| Mistral NeMo 12B | 40 | 8 | 128 | 160 KiB | 1.25 GiB | 5.00 GiB | 20 GiB at 131,072 |
| Mistral Small 3.2 24B | 40 | 8 | 128 | 160 KiB | 1.25 GiB | 5.00 GiB | 20 GiB at 131,072 |
| Granite 4.1 30B | 64 | 8 | 128 | 256 KiB | 2.00 GiB | 8.00 GiB | 32 GiB at 131,072 |
| Llama 3.3 70B | 80 | 8 | 128 | 320 KiB | 2.50 GiB | 10.00 GiB | 40 GiB at 131,072 |
Qwen2.5-7B-Instruct's 131,072 is its card's figure; its config.json says 32,768, which is what an engine reads by default.
Two things stand out. The cache follows the layers and the heads, not the size of the model: Mistral NeMo 12B and Mistral Small 3.2 24B cost the same 160 KiB a token. And a small model with a long window can need more memory for its cache than for itself: Qwen3 4B at its stated 262,144 tokens would need 36 GiB of 16-bit cache, beside a 4-bit file that is 2,497,280,480 bytes in Ollama's registry, 2.33 GiB. That is about fifteen and a half times the file, by our arithmetic.
Where the simple sum is wrong
Layers that are not ordinary attention. Several current models do not keep a full cache in every layer. Some layers look back only a fixed distance, and their cache stops growing at that distance; makers call them sliding-window layers. Some layers are not attention at all and keep a fixed state of their own for each conversation. On our roster that is true of Gemma 4, gpt-oss, the 27B Qwen models from 3.5 on, and Olmo 3.1. For such models the sum has to be made a layer at a time. For gpt-oss-20b at 32,768 tokens and 16 bits, by our arithmetic: its 12 full layers cost 2 x 12 x 8 x 64 numbers a token, 24 KiB, 768 MiB in all; its 12 sliding-window layers keep only 128 tokens each, 3 MiB; about 771 MiB where the simple sum says 1,536. For Gemma 4 31B at its stated 262,144 tokens, the simple sum gives 240 GiB and the layer-by-layer one about 21 GiB for one conversation, by our arithmetic. Over the four mixed models we worked, the simple sum is from 2 to more than 11 times too high.
Fewer bits. llama.cpp and Ollama can store the cache more coarsely; in both, a coarser store for the values needs flash attention switched on. The 8-bit form takes 34 bytes for every 32 numbers, about 53 per cent of the 16-bit form, and the 4-bit form takes 18, about 28 per cent, by our arithmetic from llama.cpp's source. Ollama's manual rounds these to "approximately 1/2" and "approximately 1/4". Smaller was not faster at writing on the one model we timed both ways: on the same EVGA GeForce RTX 3090 XC3 Ultra Gaming 24 GB, at a 131,072-token window and 350 W, gemma4:26b wrote 146.308 tokens a second with the 16-bit cache and 136.618 with the 8-bit one (the bench).
Slots. An engine that serves several conversations at once has to hold a cache for each. Ollama multiplies, and outside its MLX engine runs one slot unless its setting for parallel requests says more: its manual says "a 2K context with 4 parallel requests will result in an 8K context and additional memory allocation". On a Gigabyte GeForce RTX 3080 10 GB and an EVGA GeForce RTX 3080 FTW3 Ultra 10 GB, four Ollama slots reserved four windows' worth of cache and pushed part of the model off the cards. llama.cpp does it differently: the window you set is shared or split among its slots, its server now starts four slots by default, and the sliding-window layers get room for each slot.
An engine that fills the card. vLLM v0.34.0 does not reserve a window: left at its defaults on CUDA and ROCm cards, it sizes the cache from the memory actually free after start-up and reports how many tokens that holds. That figure is a capacity, not a need.
Checking the sum against a card
A card's memory reading holds more than the cache, so a hand sum cannot be checked against one reading. It can be checked against the difference between two readings meant to differ only in the cache.
Our bench of an RTX 5080 has such a pair. On a ZOTAC GeForce RTX 5080 Solid CORE OC 16 GB, gpt-oss:20b was loaded with the cache at 8 bits and again at 16 bits, and the card's memory was read each time. At 32,768 tokens the card held 12,900 MiB with the 8-bit cache and 13,262 MiB with the 16-bit one: 362 MiB apart. The layer-by-layer sum above gives 361.4 MiB for the same difference, by our arithmetic, if Ollama kept one slot and 128 places in each of the model's 12 sliding-window layers; we did not read Ollama's code to check that. At 4,096 tokens the card read 52 MiB apart and the sum gives 46.4.
The same trick between requests
If two requests begin with the same tokens, the keys and values for that beginning are the same, and an engine that still has them need not read the beginning again. llama.cpp's server does this by default: "the common prefix does not have to be re-processed, only the suffix that differs". On a Minisforum MS-01 mini PC (Intel Core i5-12600H, 32 GB) with no graphics card in use, qwen3.5:9b under Ollama 0.32.15 gave a re-sent prompt's first token in 211 ms against 16.0 s for a fresh one (our battery bench).
By their own guides, the hosted services' "prompt caching" is the same thing, kept for a set time and billed. OpenAI's guide says its prompt cache "stores key-value (KV) tensors, not the tokens themselves". Anthropic's speaks of "KV (key-value) cache representations".
It is also a trap for anyone timing a model. Send the same prompt twice and the second prompt-reading time is of the cache, not of the model. One of our own controlled runs withheld a whole column for that reason.
Where the name comes from
The 2017 paper has queries, keys and values and never mentions a cache. The library that accompanied it gained the cache three months later, on 2017-09-21, with a note that still reads as the plainest statement of the idea: "The caching works by saving all the previous key and value values so that you are able to send just the last query location to this attention function." A Google paper of November 2022 defines the term in so many words: "The attention key and value tensors of each layer, which we refer to as the KV cache". Earlier uses of the two letters exist: NVIDIA's FasterTransformer manual wrote "k/v cache" by April 2021. Who said it first, we do not know.
What to take with you
- A key is what a token offers to be found by, and a value is what it hands over. Every layer makes its own for every token.
- They never change once made, so engines keep them. The cache saves work. It is not where the model's knowledge lives, and it can be rebuilt from the text.
- For ordinary attention, its size is 2 x layers x key-value heads x head width, times the bytes a number takes, for every token of window the engine reserves.
- For models with sliding-window or other layers that sum is too high, by up to eleven and a half times, and has to be made a layer at a time.
- A hand sum prices the cache alone. The card also holds the model and the engine's buffers: our fit guide's rule is file, plus the cache for the window, plus about a gigabyte.
What this cannot say
- What any engine other than the ones we read does. The statements about engines are from llama.cpp, Ollama, vLLM and the transformers library at the versions named in the sources, read on one day. Their defaults change.
- A cache size for the mixed-layer models that an engine has confirmed. The layer-by-layer sums for Gemma 4, gpt-oss, Qwen3.6 27B and Olmo 3.1 are our arithmetic from the makers' files and from llama.cpp's source; for Qwen3.6 they leave out the fixed state of its 48 other layers, which we did not size. One pair of card readings for gpt-oss agrees with its sum at 32,768 tokens, under an assumption about Ollama we did not check.
- An engine's own printed cache size from our own machines. No article of ours prints one for llama.cpp or Ollama. The line quoted above is from a public bug report.
- How much quality a coarser cache costs. We have measured its speed on one model and its memory on two, not their answers.
- Why writing slows by the amount it does as the cache fills, and who first said "KV cache".
Who ran this, and thanks
Thanks to the authors of the papers and manuals quoted here, and to the people who post their load logs in public bug reports, which let anyone check a sum. A small human team asked for this page and signed it; AI agents read the sources, did the arithmetic twice by two separate scripts, and wrote the words.
Sources
Read on 2026-11-30 (UTC).
- A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, Ł. Kaiser and I. Polosukhin, "Attention Is All You Need", arXiv:1706.03762, version 7; first posted 12 June 2017: https://arxiv.org/abs/1706.03762
- D. Jurafsky and J. H. Martin, Speech and Language Processing, third edition draft of 19 August 2026, chapter 7, "Transformers and Pretraining" (the KV cache is in section 7.8): https://web.stanford.edu/~jurafsky/slp3/7.pdf
- Hugging Face transformers, release v5.24.1, "How caching works" and "Optimizing LLMs for Speed and Memory": https://github.com/huggingface/transformers/blob/v5.24.1/docs/source/en/cache_explanation.md and https://github.com/huggingface/transformers/blob/v5.24.1/docs/source/en/llm_tutorial_optimization.md
- llama.cpp, build b12855 (commit 9a278b02): the cache's source, its sliding-window form, the context's set-up, the server and its manual, and the byte sizes of the coarser forms, all under https://github.com/ggml-org/llama.cpp/tree/9a278b02b83d476d7c9758c8c91e32392f896e66 (src/llama-kv-cache.cpp, src/llama-kv-cache-iswa.cpp, src/llama-context.cpp, tools/server/server.cpp, tools/server/README.md, ggml/src/ggml-common.h); the load log quoted is in issue 19844: https://github.com/ggml-org/llama.cpp/issues/19844
- Ollama, release v0.40.19: the FAQ, the page on context length and its server's source, under https://github.com/ollama/ollama/tree/v0.40.19 (docs/faq.mdx, docs/context-length.mdx, server/sched.go, server/routes.go); its Modelfile page as it stood on its main branch on 2026-11-29 (commit d0e8482); and its registry's record of the Qwen3 4B file: https://registry.ollama.ai/v2/library/qwen3/manifests/4b-instruct-2507-q4_K_M
- vLLM, release v0.34.0: its notes on optimization, its cache configuration and the start-up line that reports the cache's size in tokens, under https://github.com/vllm-project/vllm/tree/v0.34.0 (docs/configuration/optimization.md, vllm/config/cache.py, vllm/v1/core/kv_cache_utils.py)
- The makers' configurations behind the two sets of sums, each at a fixed commit: Meta's llama-models (https://github.com/meta-llama/llama-models/blob/0e0b8c519242d5833d8c11bffc1232b77ad7f301/models/sku_list.py, and the two model cards under models/llama3_1 and models/llama3_3 at the same commit); Qwen2.5 7B (https://huggingface.co/Qwen/Qwen2.5-7B-Instruct/blob/a09a35458c702b33eeacc393d103063234e8bc28/config.json, and its card, README.md, at the same commit); Qwen3 4B (https://huggingface.co/Qwen/Qwen3-4B-Instruct-2507/blob/cdbee75f17c01a7cc42f958dc650907174af0554/config.json); Mistral NeMo 12B (https://huggingface.co/mistralai/Mistral-Nemo-Instruct-2407/blob/04d8a90549d23fc6bd7f642064003592df51e9b3/config.json); Mistral Small 3.2 24B (https://huggingface.co/mistralai/Mistral-Small-3.2-24B-Instruct-2506/blob/95a6d26c4bfb886c58daf9d3f7332c857cb27b43/config.json); Granite 4.1 30B (https://huggingface.co/ibm-granite/granite-4.1-30b/blob/4fae6278f7132abf5e971f9de49ebbad09c54cce/config.json); Gemma 4 31B (https://huggingface.co/google/gemma-4-31B-it/blob/842da3794eaa0b77d5f08bae87a17459d91ff475/config.json); gpt-oss-20b (https://huggingface.co/openai/gpt-oss-20b/blob/6cee5e81ee83917806bbde320786a8fb61efebee/config.json); Qwen3.6 27B (https://huggingface.co/Qwen/Qwen3.6-27B/blob/1cda14dd71a3f47115850eb790f416f165da1cb5/config.json); and Olmo 3.1 32B (https://huggingface.co/allenai/Olmo-3.1-32B-Think/blob/97ade57a9cc55ed48bcd76be4b7a3b6fee61b80d/config.json).
- N. Shazeer, "Fast Transformer Decoding: One Write-Head is All You Need", arXiv:1911.02150, 2019, and J. Ainslie and five others, "GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints", arXiv:2305.13245, 2023, on sharing keys and values among heads: https://arxiv.org/abs/1911.02150 and https://arxiv.org/abs/2305.13245
- OpenAI, "Prompt caching", and Anthropic, "Prompt caching": https://developers.openai.com/api/docs/guides/prompt-caching and https://platform.claude.com/docs/en/build-with-claude/prompt-caching
- For the name: the tensor2tensor library's commit of 21 September 2017 (https://github.com/tensorflow/tensor2tensor/commit/1c7d365dd37a5873017b9529e9fa6fba9c1a6e50), NVIDIA FasterTransformer's GPT guide at tag v4.0 (https://github.com/NVIDIA/FasterTransformer/blob/v4.0/docs/gpt_guide.md), and R. Pope and nine others, "Efficiently Scaling Transformer Inference", arXiv:2211.05102, 2022 (https://arxiv.org/abs/2211.05102).
- This workshop's own pages, each linked where its figure stands: the fit guide, the bench of one 3090 against one 3090 Ti, the RTX 5080 bench, the laptop bench, the two-card bench, the battery bench, the temperature-zero bench, Reading is fast, the sampler guide, and Three at the table.
A reading, not a bench. Ten of our own pages and the outside sources listed above; one step done by hand on made-up numbers; nothing measured for this page, and no data kit.
Corrections and later readings will be added below, each dated (UTC), saying in plain words what they show.