Close-up of a GPU accelerator board on a test bench

KV Cache in LLM Inference: How to Size GPU Memory for Concurrent Users

Teams size a GPU for the model weights, deploy it, and then hit out-of-memory errors the moment ten people use it at once. The missing number is usually the KV cache. This guide explains what it is, how to estimate it and which settings reduce it.

Engineer watching GPU memory graphs
KV Cache in LLM Inference: How to Size GPU Memory for Concurrent Users

What the KV cache is

A transformer produces text one token at a time. For every token it compares the new position with all earlier positions, using key and value vectors from each attention layer. Recomputing those vectors at each step would be wasteful, so the server keeps them in GPU memory. That store is the key-value cache, and it grows with every token in the prompt and the answer.

The sizing formula

For one sequence, a good estimate is:

KV bytes = 2 × layers × KV heads × head dimension × tokens × bytes per value

The 2 covers keys and values. Multiply by the number of concurrent sequences to get the total. Take an illustrative model with 80 layers, 8 KV heads, a head dimension of 128 and FP16 values: 2 × 80 × 8 × 128 × 2 bytes is about 328 KB per token. At 8,192 tokens that is roughly 2.7 GB for one conversation, and around 43 GB for sixteen. Use the values from your own model’s configuration file.

Why KV heads matter so much

Attention type KV heads Effect on cache
Multi-head Same as query heads Largest
Grouped-query Shared by groups of query heads Several times smaller
Multi-query One Smallest

Grouped-query attention was proposed to keep quality close to multi-head attention while cutting this memory, as described in the GQA paper. Two models with the same parameter count can therefore need very different cache sizes.

Where the memory goes on a serving GPU

  1. Model weights, which are fixed once loaded
  2. KV cache, which varies with users and context
  3. Activations and framework overhead
  4. Fragmentation, if the cache is not managed in blocks

Weights are covered in our LLM GPU memory requirements guide. This article covers the second line, which decides how many users you can serve.

Open GPU server in a lab
KV Cache in LLM Inference: How to Size GPU Memory for Concurrent Users

Ways to reduce it

  • Paged allocation: splitting the cache into blocks avoids reserving memory for tokens that never arrive. The PagedAttention paper behind vLLM explains the idea.
  • Prefix caching: requests that share a system prompt reuse the same blocks.
  • Cache quantization: storing values in 8 bits instead of 16 halves the cache. Test quality on your own prompts.
  • Shorter context: retrieve fewer, better passages. Long context you do not need is paid for on every request.
  • Limits: set a maximum context and a maximum concurrency so one user cannot evict everyone else.

Memory bandwidth also matters

During generation, the server reads weights and cache for every token. Capacity decides whether the workload fits, and bandwidth strongly influences how quickly tokens appear. That is one reason to compare cards on both, as in our H200 vs H100 comparison.

A practical sizing routine

  1. Read layers, KV heads and head dimension from the model configuration.
  2. Measure real prompt and answer lengths instead of assuming the maximum.
  3. Calculate the cache at the 95th percentile length and your peak concurrency.
  4. Add weights and leave headroom for overhead.
  5. Load test with realistic prompts, then watch cache usage and preemption counters.
  6. If it does not fit, try prefix caching and limits before buying another GPU.

Server software behaves differently here, so see vLLM vs Ollama vs llama.cpp before choosing one.

Worked example: from formula to users

Take a model with 32 layers, 8 key-value heads and a head size of 128, storing the cache in 16-bit precision. Each token needs 2 × 32 × 8 × 128 × 2 bytes, which is 131,072 bytes, or 128 KiB. A conversation of 8,192 tokens therefore occupies exactly 1 GiB.

Suppose a 24 GB card holds weights of about 16 GB and you reserve 2 GB for runtime and fragmentation. Roughly 6 GB remain, which is about five or six such conversations at full length. If the average conversation is 2,000 tokens, the same memory holds around 24. Plan with the distribution of lengths, not only with the maximum.

Common mistakes

  • Using total attention heads when the model has fewer KV heads
  • Planning for the average and ignoring the longest requests
  • Forgetting that generated tokens also enter the cache
  • Assuming a longer context window means it is free to use
  • Reading a full GPU as an error, when the server reserved the memory on purpose

Frequently asked questions

Does a bigger model always mean a bigger KV cache?

No. Layers, KV heads and head size decide it. A model with grouped-query attention may use less cache than a smaller one without it.

Can the KV cache be offloaded to system memory?

Some servers support it. It helps with long idle conversations, but moving data across PCIe adds latency when a conversation resumes.

Why does my server slow down before memory is full?

When the cache nears its limit, requests may be queued or preempted and recomputed. Lower concurrency or shorten context and measure again.

Does quantizing the weights shrink the KV cache?

Not by itself. Weight precision and cache precision are separate settings.

Key takeaway

Size for weights plus cache at peak concurrency. A short calculation up front costs far less than discovering the limit in production. To compare hardware on running cost, continue with cost per token.


Last update at December 2025

Leave a Reply

Your email address will not be published. Required fields are marked *