What is the KV cache? The memory that decides how many users your LLM can serve

The KV cache stores the keys and values of every token an LLM has read, so it does not recompute them. How it works, how big it gets and how to shrink it.

NVIDIA GP100 GPU package from a Tesla P100 board, the GPU die flanked by four stacked HBM2 memory chips where data such as the KV cache lives
Photo: NVIDIA GP100 (Tesla P100) GPU package by FritzchensFritz, Wikimedia Commons, CC0

The KV cache is the memory where a large language model keeps the keys and values it has already computed for every token in a conversation. Without it, the model would have to reread the whole prompt each time it writes a new word. With it, each new token costs one step instead of a full pass over the text. The price is GPU memory: the cache grows with every token and every user, and on a busy server it often decides how many people you can serve at once, not the model weights.

Which fix for whom

  • Many short chats, one shared system prompt → prefix caching. The engine keeps the cache of the common start and reuses it, so it is computed once. It does not change the model's answers.
  • You run out of cache before you run out of users → an FP8 cache. It halves the cache against 16-bit. Test the quality on your own prompts first.
  • Long documents (100,000+ tokens) → a model with an efficient attention design such as grouped-query attention (GQA) or multi-head latent attention (MLA), and enough GPU memory for it.
  • Memory is wasted even though the GPU looks full → a paged engine such as vLLM or SGLang instead of a simple one-request-at-a-time server.
  • A team of 20 to 100 people on a 70B model with long context → more GPU memory. Our server in Sofia has eight RTX PRO 6000 Blackwell Server Edition cards with 768 GB of GPU memory in total. It rents from $1.80 per GPU-hour, or about $10,510 a month for the whole machine. It is rented into early 2027 and we take reservations for the period after. See the server.

How the KV cache works

A transformer model reads text as tokens. In every layer, each token is turned into three vectors: a query, a key and a value. To decide what to write next, the newest token's query is compared with the keys of all earlier tokens, and the result weights their values.

The keys and values of earlier tokens do not change when a new token arrives. So the engine stores them once, layer by layer, and only computes the new token's own key and value at each step. That store is the KV cache.

This splits inference into two phases:

  • Prefill. The model reads the whole prompt at once and fills the cache. This is heavy on compute and fast per token, because all tokens are processed in parallel.
  • Decode. The model writes the answer one token at a time. Each step reads the full cache and the weights from memory, so speed depends mostly on memory bandwidth, not on raw compute.

That is why a long prompt mainly costs time before the first word appears, and a long answer mainly costs memory reads.

How big the KV cache gets

The size per token is simple to work out from the model's config.json:

KV cache per token = 2 × layers × KV heads × head size × bytes per value

The 2 is for keys and values. For Llama 3.3 70B (80 layers, 8 KV heads, head size 128) in 16-bit, that is 2 × 80 × 8 × 128 × 2 bytes = 327,680 bytes, or 320 KiB per token. For Llama 3.1 8B (32 layers, 8 KV heads) it is 128 KiB.

Multiply by every token being served at the same moment, prompt plus answer, for every user:

Llama 3.3 70B, 16-bit cache Tokens in cache KV cache
One user, short chat 4,096 1.25 GiB
One user, long document 32,768 10 GiB
One user, full context 131,072 40 GiB
20 users, 8,192 tokens each 163,840 50 GiB

On a single 96 GB card holding the 70B model in FP8 (about 70 GB of weights), only around 16 GB is left for cache. That is enough for one long conversation or a few short ones. We worked through more models in how much VRAM an LLM needs.

Why models got better at it

Older models had one key and value head for every attention head. Llama 3.3 70B has 64 attention heads but only 8 KV heads, a design called grouped-query attention (GQA), published by Google researchers in 2023. With one KV head per attention head, the same model would need 8 times more cache: 2.5 MiB per token instead of 320 KiB.

DeepSeek went further with multi-head latent attention (MLA), which compresses keys and values into a smaller latent vector. The DeepSeek-V2 paper reports a KV cache 93.3% smaller than its earlier DeepSeek 67B model, and up to 5.76 times higher maximum generation throughput.

When you choose an open model for a server, the attention design matters as much as the parameter count. Two models of the same size can differ several times in how many users fit.

How inference engines manage it

Paged attention. Early serving systems reserved one continuous block of memory per request, sized for the longest possible answer. Much of it stayed empty. The vLLM team's PagedAttention paper (SOSP 2023) borrowed the idea of memory pages from operating systems: the cache is split into small blocks that are handed out as the conversation grows. The authors report near-zero waste and 2 to 4 times higher throughput than earlier systems at the same latency.

Prefix caching. If many requests start with the same text (a long system prompt, a shared document, the same instructions for an AI agent), the engine can keep the cache of that prefix and reuse it. vLLM calls this automatic prefix caching, and its documentation notes that it does not change model outputs. The hosted APIs use the same idea when they offer cheaper "cached" input tokens.

Quantized cache. vLLM, SGLang and TensorRT-LLM can store the cache in FP8 instead of 16-bit. vLLM exposes it as kv_cache_dtype="fp8". That halves the memory per token. It is a trade: check answers on your real prompts, especially with long context.

Offloading. Some engines can move parts of the cache to CPU memory or fast disk and bring them back when needed. It helps with rarely used long contexts but adds latency, because the cache has to travel back to the GPU.

For a comparison of the engines themselves, see vLLM vs Ollama vs SGLang.

What it means for buying or renting a GPU server

Plan GPU memory as weights plus cache, not weights alone. A practical way:

  1. Pick the model and precision, and note the weight size.
  2. Work out the cache per token from the formula above.
  3. Estimate concurrent users × average tokens per conversation.
  4. Keep about 10% of memory for the engine's own buffers.

If the cache does not fit, the engine queues requests or refuses longer prompts. Users see slow first answers, not an error message, so the problem is easy to miss.

Memory bandwidth matters too, because every decode step reads the cache. The RTX PRO 6000 Blackwell Server Edition has 96 GB of GDDR7 at 1,597 GB/s per card. Across eight cards with tensor parallelism, both the weights and the cache are split between GPUs, so each card reads only its share. If you want to run open models on hardware you control in the EU, see private LLM hosting.

Questions

Does the KV cache change the model's answers? No. It stores values the model would otherwise recompute. Only lossy options, such as an FP8 cache or methods that drop old tokens, can change the output slightly.

Is the KV cache cleared between conversations? Each request has its own cache, freed when the request ends. With prefix caching, blocks of a shared prefix can stay in memory and be reused by later requests until the engine needs the space.

Why is the first token slow with a long prompt? Because the model has to fill the cache for the whole prompt before it writes anything. This is the prefill phase. Prefix caching helps when the long part repeats.

Is the KV cache the same as the context window? No. The context window is the maximum number of tokens a model accepts. The KV cache is the memory used for the tokens actually in play right now, across all users.

Sources

SHARE