Rack & Token

Why an 8B model can need 21 GB of VRAM

On my server, Llama 3.1 8B is a 4.6 GB download. Loaded with its full 131,072-token context, it used 21.6 GB of GPU memory, more than four times its file size. The extra 17 GB is the context cache, and it's the number most people forget when picking a GPU.

What takes the memory

A loaded model holds three things in VRAM:

  • Weights: roughly the download size. Fixed for a given model and quantization.
  • The KV cache: the attention keys and values for every token of context the model is allowed to hold. It's sized for the full context length when the model loads, whether you use it or not.
  • Working space: compute buffers, usually under a gigabyte.

The formula

KV cache (bytes) = 2 × layers × KV heads × head size × context tokens × bytes per value

The 2 is one copy for keys and one for values. With the default f16 cache, each value takes 2 bytes. For Llama 3.1 8B (32 layers, 8 KV heads, head size 128) at 131,072 tokens:

2 × 32 × 8 × 128 × 131,072 × 2 bytes = 17,179,869,184 bytes = 16.0 GiB

Checked against my GPUs

I compared the formula with what Ollama reported while each model was loaded on two V100 32 GB cards (/api/ps, which reports binary gigabytes).

ModelDownloadContextFormulaMeasured
Llama 3.1 8B, Q4_K_M4.6 GB131,07221.2 GB21.6 GB
Qwen 2.5 14B, Q4_K_M8.4 GB32,76815.0 GB14.2 GB

"Formula" is weights plus KV cache plus 0.6 GB of working space. Both land within about 6% of the real reading, close enough to choose a card.

How to see it on your own machine

ollama ps

The SIZE column is the real memory footprint, and CONTEXT shows the context length it was loaded with. Compare that with ollama list, which shows only the download size.

Three ways to make it fit

  1. Ask for less context. Most chats and agent calls never get near 131K tokens. At 8,192 tokens, Llama 3.1 8B's cache is 1 GB instead of 16.
  2. Shrink the cache. Setting OLLAMA_KV_CACHE_TYPE=q8_0 roughly halves it, with flash attention enabled (OLLAMA_FLASH_ATTENTION=1).
  3. Pick one context size per model on a shared server. If two programs request the same model with different context lengths, Ollama reloads it each time they alternate.

How fast it runs once it fits

On my RTX 5060 Ti 16 GB, with a 4,096-token context, Hermes 3 8B generated 85.8 tokens per second and Phi-4 14B generated 45.0 (median of three runs).

Try your own model and card in the VRAM calculator.