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.
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.
Just barely too small
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
Related guides
Frequently asked questions
Why does long context cost so much VRAM?
Does prompt caching help with long contexts?
What is Q8 KV cache quantization and when should I enable it?
Related tools
RAM Latency Calculator
Convert DDR3/DDR4/DDR5 timings (CL, tRCD, tRP, tRAS) into true latency in nanoseconds.
Use toolPower Cost Estimator
Estimate annual electricity costs for your PC, Server, or TV.
Use toolData Transfer Calculator
Calculate file transfer time or the speed you need over USB, Wi-Fi or Ethernet, with latency.
Use toolMemory and Storage Throughput Visualizer
Visualize the large speed gap between CPU cache, RAM, and storage.
Use tool