How much VRAM does Llama 3.1 8B need at 128K context? Long-context cost

By TechCompare · Updated · How we check

If you're not actually using 128K of context, don't allocate for it. Inference engines like llama.cpp and vLLM let you cap context at runtime. Setting it to 16K or 32K instead of 128K cuts the total from 24.3 GB to 7.8 GB or 10.1 GB. That saves more VRAM than any quantization change you could make, since the weights only take 4.9 GB.

Calculator

Estimated VRAM required

24.3 GB

8B params at Q4_K_M, 131,072 token context, batch 1, inference.

Weights
4.9 GB
KV cache
17.2 GB
Overhead
2.3 GB
Doesn't fit in 24 GB of VRAM at Q4_K_M with 128K of context. Lower the context to 64K (14.9 GB) to fit a single RTX 3090. Or keep all 128K of context with Q3_K_M (23.3 GB). Quants below Q4_K_M lose noticeable quality.

Estimate accuracy: Weights within ~3% of most GGUF files from Q4_K_M up, ~7% for Q3_K_M and Q2_K. KV cache within ~5% for standard GQA models, ~10% for MLA or hybrid state estimates. Real VRAM may vary with framework (vLLM vs llama.cpp vs Transformers), Flash Attention, and driver overhead.

Hardware that fits

Unified memory machines show a model budget, not their installed memory. We hold back 8 GB for the system, or use the vendor's GPU limit when that's lower. It's a planning estimate, not a hard limit.

RTX 5090
Consumer
32 GB
76% used
A100 40GB
Datacenter
40 GB
61% used
RTX PRO 5000 48GB
Pro
48 GB
51% used
Apple M5 Pro 64GB
Unified
~56 GB
44% of estimate

Just barely too small

RTX 3090
Consumer
24 GB
short by 0.3 GB
Apple M5 32GB
Unified
~24 GB
short by 0.3 GB

Llama 3.1 8B at Q4_K_M with the full 128K context needs about 24.3 GB of VRAM, against the 6.6 GB it uses at 8K context, even though the model itself is unchanged. The KV cache alone takes about 17.2 GB, 3.5 times the weight memory.

How this is calculated

KV cache scales linearly with context length and the cost is independent of quantization (KV cache is its own dtype). For Llama 3.1 8B (32 layers, 4096 hidden), each token in context uses 131,072 bytes of FP16 KV memory (8 KV heads of size 128 in each layer, for keys and values), so 128K tokens is roughly 17.2 GB across all layers.

Switching to Q8 KV halves that to 8.6 GB, and reducing the working context to what you actually need is the biggest lever.

Verdict

Cap the context before anything else. At 8K context Llama 3.1 8B at Q4_K_M needs about 6.6 GB total (4.9 GB of weights plus a 1.1 GB cache plus 0.6 GB overhead), and at 128K context the same model needs 24.3 GB because the FP16 KV cache grows to about 17.2 GB (8 KV heads at head_dim 128 across 32 layers times 128K tokens times 2 bytes, then doubled for keys and values).

That 17.7 GB swing comes from a runtime flag. It's more than the 4.9 GB the weights take up, so no quantization change at the weight layer can match it.

Cap context in your inference engine to what you actually need (16K or 32K is usually plenty), and switch to Q8 KV quantization, which halves the cache row to about 8.6 GB and keeps quality loss small.

More Llama scenarios

Kimi K3.1 Pro at Q4_K_M
A 2.78T expert pool with 24 latent attention layers and fixed state in the other 69.
View details
Mistral Large 4 at Q4_K_M
A 1T expert pool with 63 latent cache owners and 44 layers that also store an index key.
View details
Muse Spark Open at Q4_K_M
Only 16 of its 62 layers grow with context, and the other 46 hold a 2,048 token window.
View details

Frequently asked questions

Why does long context cost so much VRAM?
Every token in the context window stores its key and value vectors, one per attention head per layer. At 128K tokens that's hundreds of millions of values cached for the duration of the request.
Does prompt caching help with long contexts?
Yes, prefix caching reuses the KV cache across requests that share a prompt prefix. It doesn't reduce memory for one request but dramatically reduces re-computation across many.
What is Q8 KV cache quantization and when should I enable it?
Q8 stores the KV cache in 8 bits per value instead of 16, halving the cache's VRAM while keeping quality loss below the perceptible floor on most tasks. Enable it when context length is the binding constraint: a 128K conversation that won't fit with FP16 KV often fits comfortably at Q8. Skip it when you're memory-rich, since FP16 is the default for a reason and Q8 buys nothing if length isn't your bottleneck.