%3C/text%3E%3C/svg%3E">
💻
💻 Tech & IT

LLM Inference Memory Calculator (weights + KV cache)

Results update as you type.

Your numbers

Understanding the LLM inference memory calculator — concept and principle

Before running a large language model on a GPU, you need to know whether it fits. The two main consumers of memory are the model weights, which are fixed, and the key-value (KV) cache, which grows with every token of context and every request in the batch. This calculator estimates both from the model's size and architecture to help you choose a GPU, quantisation level or context length.

Inference memory of an 8B modelEight billion FP16 weights take 16 GB. The KV cache stores 2 × 32 × 8 × 128 × 2 bytes = 128 KiB per token, so 8,192 tokens add 1.07 GB, for 17.07 GB in total.GPU memory = weights + KV cacheweights16 GBKV 1.07 GBtotal17.07 GB8 B params × 2 bytes= 16 GB (FP16)per token:2 × 32 × 8 × 128 × 2 B= 128 KiB× 8,192 tokens = 1 GiBK and V · layers · heads· head dim · FP16 bytesLonger context or bigger batch → KV cache grows
16 GB of weights plus a 1.07 GB KV cache for 8,192 tokens = 17.07 GB.

How it is calculated

The first factor 2 counts one key and one value vector; the last factor 2 is the bytes per FP16 value. GB means 10⁹ bytes.

The principle

During generation a transformer attends to every earlier token. Instead of recomputing the keys and values of past tokens at each step, it stores them, in every layer and for every KV head. That store grows linearly with context length and batch size. Many recent models use grouped-query attention, in which several query heads share one KV head; this shrinks the cache in proportion to the number of KV heads, which is why that input matters more than the total number of attention heads.

Worked example

  1. Inputs: 8 billion parameters at FP16 (2 bytes), 32 layers, 8 KV heads, head dimension 128, 8,192 tokens, batch 1.
  2. Weights = 8 × 2 = 16 GB.
  3. KV cache per token = 2 × 32 × 8 × 128 × 2 = 131,072 bytes (128 KiB).
  4. × 8,192 tokens = 1,073,741,824 bytes = 1.0737 GB (exactly 1 GiB).
  5. Total = 16 + 1.0737 = 17.07 GB.
How Total (GB) changes with Parameters (billions)How Total changes with Parameters0102030402468101216Example: 17.07374182Parameters (billions)
Each point is the calculator re-run with only “Parameters (billions)” changed; the gold point is the default example. Change the inputs above and this chart follows your numbers.
Parameters (billions)Total (GB)
25.073741824
49.073741824
613.07374182
817.07374182
1021.07374182
1225.07374182
1633.07374182

How to read the result

Add roughly 10–20% for activations, the CUDA context and framework overhead, which puts this example near 19–20 GB: too much for a 16 GB card at FP16, but comfortable after 8-bit or 4-bit quantisation (8 GB or 4 GB of weights). The KV cache scales directly with batch size, so serving 16 parallel 8K-token requests would need about 17 GB of cache alone.

Common mistakes

Keep in mind: This is a lower-bound estimate. Real usage depends on the inference engine, memory fragmentation, paging strategy and other buffers, so test on the target hardware before committing.

Frequently asked questions

Where do I find layers, KV heads and head dimension?

In the model's configuration file, often named num_hidden_layers, num_key_value_heads and head_dim (or hidden size ÷ attention heads).

Does a longer context slow generation as well?

Yes, each new token must attend to all cached tokens, so attention cost per token grows with context length.

Can the KV cache itself be quantised?

Some inference engines support 8-bit KV caches, which roughly halves cache memory at a small accuracy cost.

Sources: A. Vaswani et al., Attention Is All You Need, NeurIPS 2017 · J. Ainslie et al., GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints, EMNLP 2023

Checked 8 Nov 2026. Standard formulas and RFC/IEEE definitions, computed locally in your browser — nothing you type is sent anywhere.

💻 All tech & IT tools →🌍 Calculators by country →