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 / QuantBytes per Parameter8B Model Weights32B Model Weights70B Model WeightsTypical Deployment Engine
FP16 / BF16~2.0 bytes~16.0 GB~64.0 GB~140.0 GBMulti-GPU training / reference
FP8 / Q8_0~1.0 byte~8.0 GB~32.0 GB~70.0 GBvLLM (fp8), high-accuracy serving
GGUF Q4_K_M / INT4~0.55 bytes~4.4 GB~17.6 GB~38.5 GBOllama, 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,768 tokens) and batch size 1, the KV cache consumes ~10.7 GB all by itself.
  • Quantized to **FP8 / Q8_0 KV cache (1 byte/element): drops by half to ~5.35 GB**.
  • Plus weights (38.5 GB in Q4_K_M) + overhead (~1.5 GB) = ~50.7 GB with FP16 KV versus ~45.35 GB with Q8_0 KV—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:

  1. 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.
  2. Engine-Level KV Cache Quantization & PagedAttention:
  • In Ollama: Set OLLAMA_KV_CACHE_TYPE=q8_0 (or q4_0 for extreme contexts) and watch OLLAMA_NUM_PARALLEL—because Ollama reserves parallel KV slots up front even when idle.
  • **In llama.cpp (llama-server):** Pass --ctk q8_0 --ctv q8_0 to quantize Key and Value caches on the fly.
  • In vLLM: PagedAttention eliminates contiguous memory fragmentation by allocating KV blocks dynamically, and --kv-cache-dtype fp8 cuts KV footprint in half. If vLLM OOMs at boot, lower --gpu-memory-utilization from 0.90 to 0.80 to 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_0 KV cache in Ollama before buying more RAM:** On 24GB–64GB workstations, switching OLLAMA_KV_CACHE_TYPE=q8_0 recovers gigabytes of context headroom with negligible quality drift.
  • **Audit OLLAMA_NUM_PARALLEL on 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_M vs. Q8_0 on JSON/tool-calling tasks:** Quantization degrades subtle instruction following and tool-calling syntax before general prose—always run your JSON validation suite after dropping below Q4_K_M.

Primary Sources