Worked example: Llama 3.1 8B on an 8 GB GPU
Using Q4_K_M at about 4.5 effective bits per weight, an 8B model needs about 8 x 4.5 / 8 = 4.5 GB for weights before runtime overhead. The Ollama Llama 3.1 8B row lists a 4.9 GB model file, which is a useful real-world check against the formula.
With the Llama 3.1 8B architecture, an 8,192-token FP16 KV cache is 2 x 32 layers x 8 KV heads x 128 head dimension x 8,192 tokens x 2 bytes / 2^30 = 1.0 GB. Add a 1.5 GB runtime reserve and a 10% safety margin: (4.5 + 1.0 + 1.5) x 1.10 = 7.7 GB.
That default scenario fits into 8 GB VRAM, but the margin is only about 0.3 GB. If you raise context to 32,768 tokens, the KV cache becomes 4.0 GB and the same setup no longer has a comfortable 8 GB GPU fit.
How we calculate local model memory
The estimate separates model weights from the KV cache. Weight memory uses the model parameter count and the selected effective bits per weight. KV cache memory uses the Llama-style formula: 2 x layers x KV heads x head dimension x context tokens x bytes per KV value, divided by 2^30.
Can I run Llama 3.1 8B on 8 GB VRAM?
Often yes at Q4 with moderate context, but the margin is small. The default example fits because the estimated total is 7.7 GB. A longer context, multiple sessions, or a heavier runtime can push the same setup into spill territory.
How context length changes local AI memory
Context length affects KV cache memory linearly. If you raise context from 8,192 to 32,768 tokens, the KV cache is about four times larger for the same architecture and KV precision.
What to change when the model spills out of VRAM
First reduce context length or active sessions. Then test Q4 if you were using Q8 or FP16. If the model weights alone are larger than VRAM, expect CPU/RAM offload or a smaller model to be the more realistic path.
Benchmark anchor vs real tokens per second
The speed estimate is an anchor from a cited llama.cpp CPU benchmark, scaled only as a planning hint. Real throughput depends on memory bandwidth, GPU backend, CPU generation, drivers, batch size, prompt shape, and runtime settings.
Source notes and assumptions
Reference values are stamped as of June 2026. Verify Ollama model sizes, context windows, and provider documentation before using the result for a purchase decision. Runtime reserve, safety margin, and size-adjusted speed are editable planning assumptions.
Primary sources
Model rows come from the Ollama model library pages for Llama 3.1, Qwen2.5, and Mistral. KV cache behavior is cross-checked against the Hugging Face Transformers KV cache documentation. Quantization notes use the Hugging Face bitsandbytes documentation and the 2026 llama.cpp quantization benchmark. Llama 3.1 architecture values are from Meta's Llama 3 Herd of Models paper.