Skip to content
mentoor

Topic 4 of 6 · 5 min read

KV cache memory and prefix caching

Goal: Estimate the KV cache memory a deployment needs for a given context length and number of concurrent users.

Every token in every active conversation keeps keys and values in GPU memory, so KV cache, not weights, decides how many users fit once the model is loaded.

Check yourself · 1 of 3

What does the KV cache store?

Choose one answer

Why it matters

The model weights are a fixed cost. The KV cache grows with each token of context and each concurrent request. A server that loads fine can still refuse or queue requests because the cache is full. Estimating it before launch tells you how many users one GPU group can hold.

How to estimate it

Per token, the cache holds a key and a value for each layer and each KV head:

bytes per token = 2 x layers x kv_heads x head_dim x bytes_per_value

Read layers, KV heads and head dimension from the model's config.json. For a hypothetical model with 80 layers, 8 KV heads, head dimension 128 and 16-bit values (2 bytes):

2 x 80 x 8 x 128 x 2 = 327,680 bytes, about 320 KiB per token

A 4,000-token conversation uses about 1.25 GiB. Thirty such conversations use about 37.5 GiB. Models with grouped-query attention have few KV heads, which keeps this number manageable. Models that cache all heads cost several times more.

How the engine manages it

vLLM uses PagedAttention: the cache is split into small blocks allocated on demand, like pages in an operating system. Without paging, memory is reserved for the longest possible answer and much of it is wasted. The PagedAttention paper reports that this waste was large in earlier systems and that paging raised throughput.

Prefix caching

Many requests start with the same text: a system prompt, tool definitions, a long document. Prefix caching keeps the KV blocks of that shared start and reuses them, so the server skips recomputing them. The first token arrives sooner and the GPU does less work. It only helps when the shared text comes first and is byte-identical, so put the fixed part of the prompt before the changing part.

Before and after

Weak: the prompt puts the user's name and the date on the first line, then a 3,000-token policy. Every request differs from the first character, so nothing is shared.

Better: policy text first, then date, name and question. The 3,000 tokens are computed once and reused.

What goes wrong

  • Estimating weights only. The server loads, then queues because no memory is left for the cache.
  • Using the model's maximum context for planning. Plan from the context your users actually send.
  • Putting variable text at the start of the prompt. It makes prefix caching useless.
  • Assuming defaults. Recent vLLM releases enable prefix caching by default; confirm for your version.

Examples

Sizing a deployment

Item Value
Usable GPU memory (2 x 80 GB at 0.90) 144 GB
Weights, 70B at 16-bit about 140 GB

An 80 GB pair leaves almost nothing for cache, so this setup cannot serve users. Using 8-bit weights (about 70 GB) leaves roughly 70 GB for cache. At 320 KiB per token that holds about 210,000 tokens, or about 50 conversations of 4,000 tokens each. Activations and overhead reduce this a little, so plan with a margin.

Reordering a prompt

Before:

Today is 2 Dec 2026. The customer is Dana.
<3,000 tokens of return policy>
Question: ...

After:

<3,000 tokens of return policy>
Today is 2 Dec 2026. The customer is Dana.
Question: ...

Now all requests share the same first 3,000 tokens, and prefix caching can reuse them.

Key points

  • KV cache holds keys and values for every token of every active request, and it grows with context length and concurrency.
  • Per-token size is 2 x layers x KV heads x head dimension x bytes per value, read from the model config.
  • PagedAttention allocates cache in small blocks on demand, which reduces wasted memory.
  • Prefix caching reuses KV blocks for an identical shared start, so put fixed text before variable text.
  • Memory left after weights and activations, times the GPU share you allow, is your KV budget.

Common mistakes

  • Sizing GPUs from weight size alone and forgetting the cache.
  • Planning for the model's maximum context when real prompts are far shorter, or the reverse.
  • Placing timestamps or user names at the top of a long shared prompt.
  • Expecting prefix caching to help when each request has a different long document.

Check yourself · 2 of 3

A model uses 128 KiB of KV cache per token. About how much does one 8,000-token conversation need?

Choose one answer

Check yourself · 3 of 3

Where should the unchanging policy text go in a prompt to benefit from prefix caching?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic