One full 131,072-token (128K) conversation with Llama 3.1 70B needs 42.9 GB of KV cache: the stored key and value vectors of every token the model has already read, kept so that each new token does not recompute them. Because it grows with every token of context and every concurrent request, it often decides which GPU you need: on Nodus the same model might fit a 48 GB NVIDIA L40S listed at $0.793/hr or need a 180 GB NVIDIA B200 listed at $5.500/hr on the Nodus pricing page. When it fills, a serving engine such as vLLM pauses some requests and recomputes them later, so you pay in latency rather than a crash.
Why does an LLM need a KV cache at all?
Generation runs one token at a time, and every new token has to look at all the tokens before it. In each attention layer, the new token's query is compared against the keys of every earlier token, and the result weights their values. Those earlier keys and values never change once computed, because a decoder-only model only attends backwards.
Without a cache, step 1,000 of generation would rerun the whole model over the first 999 tokens just to rebuild keys and values it already had. With a cache, the model computes K and V once per token per layer and stores them. Each decode step then processes only the newest token. That trade of memory for compute is what makes long outputs affordable.
The memory side of the trade is the problem. The PagedAttention paper (Kwon et al., 2023) gives the classic example: OPT-13B needs 800 KB of KV cache per token, so one 2,048-token request can take 1.6 GB. Its authors found that KV memory, not compute, is what limits how many requests a GPU can batch.
How do you calculate KV cache size per token?
Multiply five numbers, four of them from the model's config.json:
KV bytes per token = 2 x num_hidden_layers x num_key_value_heads x head_dim x bytes per value
The 2 is one key and one value. Bytes per value is 2 for BF16 or FP16 and 1 for an FP8 KV cache. When a config has no head_dim field, it is hidden_size / num_attention_heads. The field that matters is num_key_value_heads, not the count of query heads.
We pulled the configs from Hugging Face on November 30, 2026 and did the arithmetic. KiB means 1,024 bytes, and GB below means 10^9 bytes.
| Model | Layers | KV heads | Head dim | KV per token (BF16) | Max context |
|---|---|---|---|---|---|
| Llama 2 7B | 32 | 32 (MHA) | 128 | 524,288 B = 512 KiB | 4,096 |
| Llama 3.1 8B | 32 | 8 (GQA) | 128 | 131,072 B = 128 KiB | 131,072 |
| Qwen3-32B | 64 | 8 (GQA) | 128 | 262,144 B = 256 KiB | 40,960 |
| Llama 3.1 70B | 80 | 8 (GQA) | 128 | 327,680 B = 320 KiB | 131,072 |
The Llama rows link Unsloth's public mirrors because Meta's repositories are gated; the architecture fields are the same. Llama 3.1 70B and Llama 2 7B have no head_dim field, so we used 8,192 / 64 and 4,096 / 32, both 128.
The contrast between the first two rows is the whole story of grouped-query attention (GQA). Llama 2 7B and Llama 3.1 8B both have 32 layers and 128-dimension heads. Llama 2 keeps a separate key and value for each of its 32 heads, which is multi-head attention (MHA). Llama 3.1 shares each KV head across 4 query heads, so it stores 8, and its cache is 4 times smaller per token even though the model is bigger.
DeepSeek-V3 goes further with multi-head latent attention (MLA). The DeepSeek-V2 paper says MLA caches a compressed latent vector plus one decoupled positional key per layer. DeepSeek-V3's config sets those to 512 (kv_lora_rank) and 64 (qk_rope_head_dim) across 61 layers. That is 61 x 576 x 2 bytes = 70,272 bytes, or 68.6 KiB per token.

Attention design, not parameter count, sets the per-token cost: the 7B MHA model stores the most.
How big does the KV cache get next to the model weights?
Multiply the per-token figure by every token resident at once: context length times concurrent requests. A single long context is already large. One full 131,072-token sequence of Llama 3.1 8B takes 131,072 x 131,072 bytes = 17.18 GB of KV cache. That is more than the 16.06 GB of BF16 weights listed in its safetensors index.
For Llama 3.1 70B, one full context is 327,680 x 131,072 bytes = 42.9 GB. A full 40,960-token Qwen3-32B context is 10.74 GB.
Serving is where it adds up. Take 32 concurrent requests at 8,192 tokens each. That is 262,144 tokens resident:
- Llama 3.1 8B: 262,144 x 131,072 bytes = 34.36 GB of KV, against 16.06 GB of weights.
- Qwen3-32B: 262,144 x 262,144 bytes = 68.72 GB of KV, against 65.52 GB of weights.
- Llama 3.1 70B: 262,144 x 327,680 bytes = 85.9 GB of KV, against 141.11 GB of weights.

For the 8B model the cache is twice the weights; for 70B it is more than a whole 80 GB GPU.
The MHA model is worse still. The same 32 requests at Llama 2 7B's full 4,096 tokens would need 68.72 GB of KV for 13.48 GB of weights. Weights are a fixed cost you pay once per GPU. The KV cache is the variable cost of every user and every token, which is why serving needs more than the weights-only estimate in our LLM VRAM guide.
What happens when the KV cache is full?
In vLLM, requests start getting preempted. The vLLM optimization docs say that when KV cache space cannot hold every batched request, vLLM preempts some to free space and recomputes them once space returns. In vLLM V1 the default mode is recompute rather than swapping to CPU, because recomputation costs less in that architecture.
Nothing crashes and no output is wrong. The preempted request loses its cached keys and values and must run prefill again over its prompt and the tokens it had generated. You see it as latency spikes and lower throughput. The log shows a warning that a request "is preempted by PreemptionMode.RECOMPUTE mode because there is not enough KV cache space." New requests wait in the queue until blocks free up.
vLLM also tells you the size of the pool before any request arrives. At startup it logs a line of the form "GPU KV cache size: N tokens, Maximum concurrency for M tokens per request: X" (source). If N divided by your typical request length is below your expected concurrency, expect preemption under load.
The docs' remedies are to keep gpu_memory_utilization high so the cache gets more memory, or to lower max_num_seqs or max_num_batched_tokens. Raising tensor_parallel_size or pipeline_parallel_size also helps, because weights then take less memory on each GPU. vLLM exports preemption counts as Prometheus metrics, so you can alert on them.
How do you make the KV cache smaller?
Each lever either stores fewer bytes per token or holds fewer tokens at once. In rough order of how much they change the math:
- Pick a GQA or MLA model. This is the largest lever, and it is fixed at training time. Llama 3.1 70B has 64 query heads; if it kept all 64 as KV heads, it would need 2 x 80 x 64 x 128 x 2 = 2,621,440 bytes, or 2,560 KiB per token, instead of 320 KiB. The GQA paper reports quality close to multi-head attention at speed comparable to multi-query attention.
- Store the cache in FP8. vLLM's
--kv-cache-dtype fp8halves bytes per value, so 320 KiB becomes 160 KiB per token. The quantized KV cache docs recommend calibrating scales with llm-compressor for best accuracy; the uncalibrated default sets every scale to 1.0. Check quality on your own evals before shipping it. - Cap context and concurrency.
max_model_lenlimits how long one request can grow, andmax_num_seqslimits how many run at once, per vLLM's memory guide. If your prompts never exceed 16,000 tokens, do not serve a 131,072-token limit. - Page the cache. PagedAttention allocates KV in small blocks as tokens arrive, so memory tracks real lengths instead of worst-case reservations. vLLM does this by default; our vLLM explainer has the measured waste figures.
- Offload to CPU memory. vLLM's OffloadingConnector copies completed KV blocks to host memory and loads them back on a prefix hit. It extends the prefix cache for repeated prompts; it does not raise how many tokens can decode on the GPU at once.
- Split the model. Tensor parallelism shards both weights and KV heads across GPUs, which our multi-GPU inference guide covers.

GQA already removed seven eighths of the 70B model's cache; FP8 halves what is left.
Quantizing weights to 4 bits is missing from that list on purpose: it does not shrink the KV cache. Formats like AWQ and GPTQ compress the weights, while keys and values stay in the activation dtype unless you set the KV dtype separately. Our quantization guide covers the weight side.
How much GPU memory should you pick for the KV cache on Nodus?
Size the KV cache from your traffic, add the weights, then pick the smallest GPU that fits with headroom. The rule we use:
- KV needed = per-token bytes x average resident tokens per request x peak concurrent requests. Use real average lengths, since paging means you pay for actual tokens, not the maximum.
- Total = weights + KV needed + headroom for activations and CUDA graphs. vLLM measures that overhead at startup, so read its KV size log line rather than guessing.
- If it does not fit, try an FP8 KV cache first, then a lower
max_num_seqs, then a bigger GPU or tensor parallelism. - If the KV cache for your target load is larger than the weights, extra memory buys throughput directly. That is when a 141 GB card beats an 80 GB one.
A worked example, from the numbers above. Llama 3.1 8B serving 32 requests of 8,192 tokens needs 16.06 + 34.36 = 50.42 GB before overhead. That does not fit a 48 GB L40S, so the obvious pick is an 80 GB NVIDIA H100 SXM, listed at $2.600/hr on the Nodus pricing page. With an FP8 KV cache, the same load needs 16.06 + 17.18 = 33.24 GB, which leaves about 14.8 GB of headroom on the L40S at $0.793/hr.
| Option | Weights + KV | GPU | Listed price |
|---|---|---|---|
| BF16 KV cache | 50.42 GB | NVIDIA H100 SXM, 80 GB | $2.600/hr |
| FP8 KV cache | 33.24 GB | NVIDIA L40S, 48 GB | $0.793/hr |
Prices checked November 30, 2026, from the Nodus pricing page.
The L40S has less memory bandwidth than an H100, so check that its tokens per second meet your latency target; our tokens per second guide shows how to estimate that. For 70B models the same rule gives 141.11 + 85.9 = 227.01 GB in BF16 at this load, which is a multi-GPU decision. On Nodus, nodus get gpus lists which cards are available right now and their rates, as described in the GPU selection docs.
FAQ
Does training use a KV cache?
No. Training processes every position of a sequence in one parallel forward pass, so there are no decode steps to reuse keys and values across. Training memory is dominated by activations, gradients and optimizer state instead.
Does prefix caching use extra memory?
Not memory that active requests need. In vLLM's prefix caching design, blocks from finished requests stay in a free queue with their contents and are evicted only when a new allocation needs them. A later request with the same prefix can reuse them and skip that part of prefill.
Is the KV cache the same as the context window?
No. The context window is the model's maximum sequence length, a property of training. The KV cache is the memory you actually allocate for tokens in flight. vLLM's max_model_len lets you serve a shorter limit than the model supports.
Why does my vLLM server use almost all GPU memory with no traffic?
vLLM reserves the KV pool at startup, sized to the memory left after weights and warmup, so the GPU looks full while idle. That is the cache waiting for requests, not a leak; the startup log line gives its size in tokens.
Can I set the KV cache dtype when serving vLLM on Nodus?
Yes. Nodus runs any public registry image as a Job, as the Images guide describes, so --kv-cache-dtype fp8 goes into your vllm serve command as it would anywhere else. Our vLLM in Docker guide has the full nodus run command.
Originally published on the Nodus blog.
Top comments (0)