Downloading an open-weight model like DeepSeek-V4.1-Flash or Meta Llama 4 only to hit a CUDA Out-Of-Memory error on the very first 32k-token prompt is a rite of passage in local AI. Most builders check the size of the .gguf or safetensors file on disk, verify it is smaller than their GPU's VRAM, and forget the second giant consumer of memory: the runtime KV cache.
Understanding the exact memory formula across model weights, KV cache, and runtime buffers lets you size consumer GPUs, Apple Silicon unified memory, and vLLM servers accurately before pulling multi-gigabyte checkpoints.
The Three-Part Local VRAM Formula
Every local inference engine—whether Ollama, llama.cpp, or vLLM—allocates GPU/unified memory across three buckets:
Total VRAM ≈ Model Weights + (KV Cache per Token × Context Length × Parallel Slots) + Runtime Overhead (1–2 GB)
Bucket 1: Model Weight Footprint by Quantization Format
Quantization shrinks each neural network parameter from 16 bits down to 8 or 4 bits, trading minimal accuracy loss for massive memory savings:
| Format / Quant | Bytes per Parameter | 8B Model Weights | 32B Model Weights | 70B Model Weights | Typical Deployment Engine |
|---|---|---|---|---|---|
| FP16 / BF16 | ~2.0 bytes | ~16.0 GB | ~64.0 GB | ~140.0 GB | Multi-GPU training / reference |
| FP8 / Q8_0 | ~1.0 byte | ~8.0 GB | ~32.0 GB | ~70.0 GB | vLLM (fp8), high-accuracy serving |
GGUF Q4_K_M / INT4 | ~0.55 bytes | ~4.4 GB | ~17.6 GB | ~38.5 GB | Ollama, llama.cpp, workstations |
Notice why Q4_K_M GGUF remains the workhorse format for single-GPU and Apple Silicon setups: it uses roughly 0.55 bytes per parameter (slightly above 0.50 due to block scaling factors), fitting a 32B-class model comfortably inside a 24GB card or 32GB laptop with room left over for context.
Bucket 2: Why Long Context Explodes KV-Cache Memory
The Key-Value (KV) cache stores attention tensors for every token in the active context window. Its size per token depends on the model's layer count, Key-Value attention heads (Grouped-Query Attention dramatically shrinks this compared to older multi-head models), and cache precision:
KV Bytes per Token = 2 (Key + Value) × n_layers × n_kv_heads × head_dim × bytes_per_element
Consider a typical 70B architecture (80 layers, 8 GQA KV heads, 128 head dimension):
- At FP16 KV cache (2 bytes/element):
2 × 80 × 8 × 128 × 2= 327,680 bytes (~320 KB) per token. At a 32k context (32,768tokens) and batch size 1, the KV cache consumes ~10.7 GB all by itself. - Quantized to **FP8 /
Q8_0KV cache (1 byte/element): drops by half to ~5.35 GB**. - Plus weights (
38.5 GBinQ4_K_M) + overhead (~1.5 GB) = ~50.7 GB with FP16 KV versus ~45.35 GB withQ8_0KV—determining whether a job fits cleanly on a 48GB GPU or crashes at startup.
Architectural Breakthroughs & Engine Flags That Save VRAM
Two developments in 2026 made long-context local inference far easier on modest hardware:
- Lower-KV Model Architectures: Newer open-weight designs like DeepSeek-V4.1-Flash use a Causal Encoder–Decoder layout specifically engineered to compress KV-cache requirements across HBM and SSD tiers, reducing memory bottlenecks during long coding-agent histories.
- Engine-Level KV Cache Quantization & PagedAttention:
- In Ollama: Set
OLLAMA_KV_CACHE_TYPE=q8_0(orq4_0for extreme contexts) and watchOLLAMA_NUM_PARALLEL—because Ollama reserves parallel KV slots up front even when idle. - **In
llama.cpp(llama-server):** Pass--ctk q8_0 --ctv q8_0to quantize Key and Value caches on the fly. - In vLLM: PagedAttention eliminates contiguous memory fragmentation by allocating KV blocks dynamically, and
--kv-cache-dtype fp8cuts KV footprint in half. If vLLM OOMs at boot, lower--gpu-memory-utilizationfrom0.90to0.80to leave headroom for CUDA driver buffers.
Copyable Local Hardware Sizing Prompt
Calculate the exact memory budget and recommended quantization settings for running the open-weight model below on my hardware.
Provide:
1. Weight VRAM across FP8, Q8_0, and Q4_K_M.
2. KV-cache VRAM at 8k, 32k, and 64k context lengths comparing FP16 vs Q8_0/FP8 KV cache (assuming GQA architecture specs if provided).
3. Exact Ollama environment variables (`OLLAMA_KV_CACHE_TYPE`, `OLLAMA_NUM_PARALLEL`) or vLLM CLI flags (`--kv-cache-dtype`, `--gpu-memory-utilization`) that fit safely inside my available memory.
Model specs & hardware:
[MODEL NAME / PARAM COUNT / TARGET CONTEXT / GPU OR UNIFIED RAM GB]
What to Do Today
- **Enable
q8_0KV cache in Ollama before buying more RAM:** On 24GB–64GB workstations, switchingOLLAMA_KV_CACHE_TYPE=q8_0recovers gigabytes of context headroom with negligible quality drift. - **Audit
OLLAMA_NUM_PARALLELon single-user laptops:** Dropping parallel slots from 4 to 1 immediately cuts reserved KV-cache allocation by 75% for solo coding sessions. - **Verify
Q4_K_Mvs.Q8_0on JSON/tool-calling tasks:** Quantization degrades subtle instruction following and tool-calling syntax before general prose—always run your JSON validation suite after dropping belowQ4_K_M.
