SysArt
Book a briefing
SysArt

On-premises AI

GPU Memory Management and KV Cache Optimization for On-Premises LLM Serving

How GPU memory is split between model weights and the KV cache when you serve LLMs on-premises, and how paging, smaller caches and limits raise concurrency.

Last reviewed by SysArt AI Architecture Team8 min readHow we review

In short

On-premises LLM serving is usually limited by GPU memory rather than compute. Each GPU must hold the model weights plus a key-value (KV) cache that grows with every token of every request in flight. Paged cache allocation, models with grouped-query attention, FP8 cache storage, prefix caching and explicit concurrency limits let the same GPUs serve more requests.

Key takeaways

  • Size GPU memory for the weights plus the KV cache. Per token, the cache needs 2 × layers × key-value heads × head dimension × bytes per value.
  • Paged allocation, introduced with vLLM's PagedAttention, keeps cache waste near zero; in the systems the paper profiled, only 20.4% to 38.2% of cache memory held real token states.
  • Grouped-query attention and FP8 cache storage shrink the cache per token. Validate any precision change on your own evaluation set.
  • When the cache runs out, vLLM preempts requests and recomputes them later. Rising preemptions and waiting queues mean the cache is too small for the load.
  • Plan capacity from measured context lengths and concurrency, then check the result against candidate GPUs.
Close-up of computer RAM modules

Why is GPU memory the bottleneck in on-premises LLM serving?

GPU memory, more often than compute, decides how many requests an on-premises LLM server can handle at once. Each GPU has a fixed amount of memory that must hold the model weights and the key-value (KV) cache, the working memory of every request in flight. On-premises, you cannot rent more when demand peaks.

The weights are the predictable part. At 16-bit precision a model needs about 2 bytes per parameter, so a 13-billion-parameter model needs roughly 26 GB before it serves a single request. The KV cache is the variable part. During generation, the model keeps the attention keys and values of all previous tokens so that it does not recompute them for each new token. The cache grows with every generated token, with every concurrent request and with every layer of the model.

The PagedAttention paper, which introduced the memory manager behind vLLM, explains why this matters. The prompt phase processes all prompt tokens in parallel and uses the GPU efficiently. The generation phase produces one token at a time, underuses the GPU’s compute and is memory-bound. Throughput therefore depends on how many requests fit in memory together. Poor memory management forces a choice between low concurrency and out-of-memory failures; good management lets the same GPUs serve more users.

How much memory does the KV cache need?

Per token, the cache needs 2 (keys and values) × number of layers × number of key-value heads × head dimension × bytes per value. Multiply that by the tokens each request holds and by the number of concurrent requests.

ModelArchitecture inputsCache per token at 16-bitOne request
OPT-13B40 layers, hidden size 5,120, one key-value head per attention head800 KBUp to 1.6 GB at 2,048 tokens
Llama 3 8B32 layers, 8 key-value heads of dimension 128128 KB1 GB at 8,192 tokens
Llama 3 70B80 layers, 8 key-value heads of dimension 128320 KB2.5 GB at 8,192 tokens

KB and GB are binary units here (1 KB = 1,024 bytes), as in the PagedAttention paper, which gives the OPT-13B figures. The Llama 3 figures follow from the architecture table in Meta’s Llama 3 paper; the head dimension is the model dimension divided by the number of attention heads.

Two consequences follow. First, at realistic concurrency the cache can need as much memory as the weights, or more: 32 OPT-13B requests at full length need about 51 GB of cache, against roughly 26 GB of weights. Second, the cache per token differs widely between models, so the architecture matters as much as the parameter count. Our LLM GPU sizing calculator applies the same formula to your model, context length and number of users.

How does paged attention reduce wasted memory?

Paged attention stores the cache in small fixed-size blocks instead of one contiguous region per request, which removes most of the waste that limited earlier serving systems.

Earlier systems reserved a contiguous chunk of memory for each request’s maximum possible length. Short requests left most of their reservation unused, and reservations of different sizes fragmented the remaining memory. The PagedAttention authors found that only 20.4% to 38.2% of KV cache memory held actual token states in the systems they profiled.

PagedAttention borrows the idea of pages from operating-system virtual memory. Each request’s cache is split into blocks holding a fixed number of tokens, 16 by default in the paper, and a block table maps them to physical blocks anywhere in GPU memory. Blocks are allocated only as tokens are generated, so waste is limited to the last, partly filled block. The paper reports near-zero waste in KV cache memory and 2 to 4 times the throughput of FasterTransformer and Orca at the same level of latency.

In vLLM, two settings shape this budget:

  • --gpu-memory-utilization is the fraction of each GPU’s memory that the vLLM instance may use, and vLLM pre-allocates its cache from it. It is a per-instance limit, so two instances sharing one GPU can each be set to 0.5.
  • --block-size sets the number of tokens per cache block. The paper found 16 a good balance between GPU efficiency and fragmentation for its workloads; change it only with measurements.

Engines differ in how they schedule and cache requests; our comparison of vLLM, Ollama, llama.cpp and SGLang covers the choice.

How can you make each token’s cache smaller?

Choose models that share key-value heads, store the cache at lower precision and cap context length at what your use cases need.

  • Grouped-query attention (GQA). Instead of one key-value head per query head, groups of query heads share one. Llama 3 uses 8 key-value heads “to improve inference speed and to reduce the size of key-value caches during decoding”. Llama 3 70B has 64 query heads, so its cache is one-eighth of what it would be with a key-value head per query head; for the 8B model, with 32 query heads, it is one-quarter. Compare the number of key-value heads in each candidate model’s configuration.
  • FP8 cache storage. vLLM can store the cache in 8-bit floating point (--kv-cache-dtype fp8, or the explicit fp8_e4m3 and fp8_e5m2 formats), one byte per value instead of two. Its documentation says this can significantly reduce the cache’s memory footprint, so you can store more tokens and improve throughput. It recommends scales estimated on a calibration dataset for maximum accuracy and does not promise unchanged quality, so run your evaluation set before and after.
  • Context limits. --max-model-len caps prompt plus output length. If you leave it unset, vLLM derives it from the model configuration, which may be far longer than your use cases need. Retrieval that passes fewer, better passages also shortens prompts.
  • Smaller weights. Weight quantisation does not shrink the cache, but it frees memory the cache can use. Our post on quantisation and pruning covers the quality trade-offs.

A smaller model with GQA and an FP8 cache may serve more users on the same hardware than a larger one. Choose on evaluation results for your tasks, not on memory alone.

How do prefix caching and preemption affect memory?

Prefix caching saves memory and compute when requests share the same opening text. Preemption is what the engine does when the cache runs out.

Prefix caching. Many requests start the same way: a long system prompt, the same few-shot examples or the same document. The PagedAttention paper describes storing the cache of such a shared prefix once and mapping new requests onto the cached blocks, so the prefix is not recomputed. vLLM exposes this as prefix caching (--enable-prefix-caching). It helps most when shared prefixes are long and identical, so keep system prompts stable and place variable content after them.

Preemption. When running requests need more cache blocks than are free, the engine must pause some of them. The PagedAttention paper describes two recovery methods: swapping blocks to CPU memory, or discarding and recomputing them. In vLLM V1 the default is recomputation, and preempted requests are recomputed when enough cache space is available again. Users experience preemption as slower responses, so frequent preemption is a capacity signal, not a tuning detail. vLLM’s tuning guide suggests three responses:

  • increase gpu_memory_utilization, so more memory goes to the cache;
  • decrease max_num_seqs or max_num_batched_tokens, so fewer requests share a batch;
  • increase tensor parallelism, so each GPU holds a smaller share of the weights and more cache, at the cost of extra synchronisation between GPUs.

How should you share GPU memory between models and teams?

Give each model deployment an explicit memory budget, and enforce team limits in front of the engines rather than letting workloads compete inside one of them.

  • Run each model in its own engine instance with its own memory fraction and maximum number of concurrent sequences. Where instances share a GPU, keep the sum of their fractions within the GPU’s memory, with room for other processes.
  • Separate interactive and batch workloads, so that a long document job does not fill the cache that chat users need.
  • Enforce per-team quotas and token rate limits at the AI gateway, where each request carries an identity.
  • For partitioning hardware between teams, see our posts on GPU resource scheduling and GPU virtualisation.

How do you monitor and plan KV cache capacity?

Watch cache usage, preemptions and queues together, and size the hardware from measured traffic rather than averages.

vLLM’s metrics endpoint exposes the signals you need: vllm:kv_cache_usage_perc for cache occupancy, vllm:num_preemptions for preempted requests, vllm:num_requests_waiting and vllm:num_requests_running for queues, and histograms such as vllm:time_to_first_token_seconds. Cache usage close to its ceiling, a growing waiting queue and rising preemptions together mean memory is the limit. Alert on sustained patterns, not single spikes.

For capacity planning:

  1. Measure the real distribution of prompt and output lengths for each use case, including peaks, over a period that covers your business cycle.
  2. Work out the cache per token for each candidate model, and the cache needed at peak concurrency and at the 95th or 99th percentile context length.
  3. Add the weights, runtime overhead and headroom for growth, then compare the total with candidate GPUs.
  4. Repeat the calculation after every model change, because a new model can change the cache per token several times over.

The GPU capacity section of our on-premises AI reference architecture shows how this fits into platform design, and our guide to capacity planning for on-premises LLM hardware covers forecasting. When a model does not fit on one GPU even after these steps, the next question is how to split it across several GPUs. For how serving engines batch and schedule requests, see our explainer on LLM inference serving.

Featured image by alireza edalati on Unsplash.

Frequently asked questions

Why does the KV cache use so much GPU memory?

It stores the attention keys and values of every token, in every layer, for every request in flight. In the PagedAttention paper, one token of the 13-billion-parameter OPT model needs about 800 KB, so one 2,048-token request can need up to 1.6 GB. With a few dozen concurrent requests, the cache can need as much memory as the model weights, or more.

What does the gpu_memory_utilization setting in vLLM control?

It sets the fraction of each GPU's memory that one vLLM instance may use, and vLLM pre-allocates its KV cache from that budget. A higher value gives the cache more room and can reduce preemptions, but leaves less memory for other processes on the GPU. It is a per-instance limit: two instances sharing a GPU can each be set to 0.5.

Is an FP8 KV cache safe for production?

It can be, but test it first. vLLM documents FP8 cache storage as a way to reduce the cache's memory footprint, store more tokens and improve throughput. The documentation recommends scales estimated on a calibration dataset for maximum accuracy and does not promise unchanged output quality. Run your evaluation set with and without FP8 before switching production traffic, and record the decision.

What happens when a GPU runs out of KV cache memory?

The engine preempts some running requests to free blocks. In vLLM V1 the default is recomputation: preempted requests are recomputed when enough cache space becomes available again. Users experience this as slower responses. vLLM's tuning guide suggests raising gpu_memory_utilization, lowering the number of concurrent sequences or batched tokens, or adding tensor parallelism.

Which metrics show that GPU memory is the bottleneck?

In vLLM, watch vllm:kv_cache_usage_perc, vllm:num_preemptions, vllm:num_requests_waiting and the time-to-first-token histogram. Cache usage close to its ceiling, together with a growing waiting queue and rising preemptions, points to memory rather than compute as the limit. Alert on sustained patterns rather than single spikes, and review them after every model change.

Related insights

Plan your private AI with people who build it

Book a briefing. We look at your use case, data and constraints, and you leave with the deployment options and governance steps that fit.