VRAMScore
Architecture & Physics•9 min read•Updated 2026-11-28•PURE ENGINEERING REFERENCE

How LLM VRAM Works: Weights, KV Cache, Context Length & Batch Size Math Explained

A rigorous engineering breakdown of every gigabyte allocated inside your GPU or Apple Silicon unified memory during local LLM inference: parameter weights, GQA/MLA KV cache scaling, concurrent batch size, and CUDA/Metal runtime buffers.

Engineering Takeaways

  • Total LLM VRAM is never just model size on disk: you must sum Weights + KV Cache + Compute Activation & Runtime Overhead.
  • KV cache grows linearly with context length and concurrent batch size, and depends strictly on transformer layer count, KV head count (GQA), and head dimension.
  • Modern attention architectures drastically compress context memory: DeepSeek MLA reduces KV cache by over 90%, while Gemma 3's 5:1 sliding window caps local layers at 1,024 tokens.
  • Quantizing the KV cache from FP16 to Q8_0 (with Flash Attention enabled) cuts long-context memory overhead in half with virtually zero measurable quality loss.

1. Why 'Parameter Count × 0.5 GB' Causes Out-of-Memory Crashes

Almost every beginner trying to run Ollama, llama.cpp, or vLLM locally hits the same surprise: they download a 32-billion parameter model quantized to 4-bit (~19.5 GB on disk), load it onto a 24 GB graphics card, paste a large codebase into the prompt, and immediately receive a CUDA Out-of-Memory error or watch generation speed collapse from 50 tokens per second down to 4 tokens per second.

The reason is simple: static parameter weights are only one of four memory pools that must coexist inside VRAM simultaneously. During inference, your GPU memory must house (1) the quantized model weights, (2) the Key-Value (KV) attention cache for every token in the active context window, (3) temporary compute graph activation buffers, and (4) the CUDA, ROCm, or Apple Metal framework context itself. On Windows desktops, the Windows Display Driver Model (WDDM) also reserves 0.4 GB to 1.2 GB for the desktop compositor and open browser tabs.

When you treat VRAM budgeting as an exact accounting equation instead of a rough guess, you can predict whether a model fits 100% on GPU before downloading a single gigabyte of weights.

2. Component One: Static Model Weights and Effective Bits Per Weight

A raw neural network parameter stored in standard half-precision floating point (FP16 or BF16) occupies exactly 2 bytes (16 bits). Multiplying 70.6 billion parameters by 2 bytes yields 141.2 billion bytes, or roughly 131.5 GiB (141.2 GB decimal). Because consumer GPUs top out at 24 GB to 32 GB of GDDR6X/GDDR7, local inference almost always relies on integer or block-wise quantization formats such as GGUF K-quants, AWQ, or EXL2.

Crucially, a 4-bit quant like Q4_K_M does not average exactly 4.00 bits per weight. High-sensitivity tensors such as token embeddings, RMSNorm layers, attention output projections, and per-block scaling constants remain at 6-bit, 8-bit, or 16-bit precision so the model preserves reasoning accuracy. Across Llama 3.3, Qwen 3, and DeepSeek distillations, Q4_K_M averages roughly 4.85 effective bits per weight (~0.606 GB per billion parameters), Q5_K_M averages ~5.70 bpw, Q6_K averages ~6.56 bpw, and Q8_0 averages ~8.50 bpw.

# Exact Static Weight Footprint Formula
Weights_GB = Total_Parameters_Billions * (Effective_Bits_Per_Weight / 8)

# Worked Example: DeepSeek-R1 Distill 32B (32.8B params)
# Q4_K_M (4.85 bpw): 32.8 * (4.85 / 8) = 19.88 GB
# Q6_K   (6.56 bpw): 32.8 * (6.56 / 8) = 26.90 GB
# Q8_0   (8.50 bpw): 32.8 * (8.50 / 8) = 34.85 GB

3. Component Two: Exact KV Cache Mathematics (GQA, MLA & Sliding Window)

Autoregressive Transformers generate text one token at a time. Without caching, generating token 8,000 would require recomputing the attention Key (K) and Value (V) projections for all 7,999 preceding tokens at every layer — turning inference into an O(N^2) crawl. To keep generation linear in compute, inference engines store the K and V matrices for every past token in VRAM. That stored state is the KV cache.

For any standard Grouped-Query Attention (GQA) Transformer, the exact KV cache footprint is completely deterministic from the model's config.json architecture file:

Model ArchitectureLayersKV MechanismKV @ 8K (FP16)KV @ 32K (FP16)KV @ 128K (FP16)
Llama 3.1 8B32GQA (8 KV heads)1.00 GB4.00 GB16.00 GB
Qwen 2.5 / DeepSeek 32B64GQA (8 KV heads)2.00 GB8.00 GB32.00 GB
Llama 3.3 70B80GQA (8 KV heads)2.50 GB10.00 GB40.00 GB
Gemma 3 27B625:1 Sliding Window (1024)0.78 GB2.35 GB8.60 GB
DeepSeek-R1 671B61MLA Latent Compression0.55 GB2.20 GB8.80 GB
# Exact KV Cache Formula (Bytes)
KV_Bytes = 2 (K and V)
         * Num_Layers
         * Num_KV_Heads
         * Head_Dim
         * Context_Length
         * Batch_Size
         * Precision_Bytes   # 2 for FP16 default, 1 for Q8_0 KV, 0.5 for Q4_0 KV

# Worked Example: Llama 3.3 70B at 32,768 Context (FP16 KV, Batch=1)
# Layers=80, KV_Heads=8, Head_Dim=128
# Bytes = 2 * 80 * 8 * 128 * 32,768 * 1 * 2 = 10,737,418,240 bytes = 10.00 GiB!

4. How Batch Size & Concurrent Office Users Multiply Memory Demand

Single-user desktop chat always runs at batch size 1. However, the moment you deploy a shared Ollama or vLLM server for a software team, law office, or automated agent swarm, every simultaneous request requires its own independent KV cache slots. Serving ten concurrent engineers on Llama 3.3 70B at 16K context does not require ten copies of the model weights — weights are shared across all requests — but it DOES multiply the KV cache roughly tenfold.

With FP16 KV cache, a single 16K context session on Llama 3.3 70B consumes 5.0 GB. Ten simultaneous sessions consume ~42 to 50 GB of KV cache alone (slightly less when prefix caching shares identical system prompts under vLLM PagedAttention). That is why a 48 GB dual-GPU machine easily serves one user at 70B Q4_K_M, whereas a ten-user office server requires 96 GB to 128 GB of aggregate VRAM or FP8/Q8 KV cache compression.

5. Component Three: Compute Graph Activations & Runtime Overhead

Even with zero context tokens loaded, initializing CUDA kernels, cuBLAS workspaces, Flash Attention scratchpads, and intermediate layer activations allocates between 0.7 GB and 2.5 GB of VRAM depending on active parameter width and prompt prefill chunk size (n_ubatch). Large prompt ingestion (e.g., pasting a 20,000-token PDF) momentarily spikes activation memory during the prefill pass before settling back to steady-state KV size during token decode.

For rock-solid production stability, always maintain at least 1.0 GB to 1.5 GB of free VRAM headroom on discrete desktop GPUs, and respect macOS's ~75% default wired GPU memory ceiling on Apple Silicon Macs unless you explicitly raise iogpu.wired_limit_mb while leaving at least 12–16 GB free for macOS.

  • Enable Flash Attention (OLLAMA_FLASH_ATTENTION=1) to keep attention scratch memory O(N) instead of O(N^2) during long prompts.
  • Enable Q8_0 KV Cache (OLLAMA_KV_CACHE_TYPE=q8_0) to slice KV memory usage in half with near-zero perplexity impact.
  • Set explicit context limits (num_ctx) per workflow: use 8K–16K for daily chat and reserve 64K–128K exclusively for full-repository RAG.

Frequently Asked Technical Questions

Why does my 20GB Q4_K_M model crash on a 24GB GPU when I increase context to 32K?

Because a 32B model's weights occupy ~19.8 GB, leaving only ~4.2 GB free. Raising context to 32K with default FP16 KV cache adds ~8.0 GB plus ~1.2 GB of CUDA runtime overhead (29.0 GB total), overflowing 24 GB VRAM.

Does increasing batch size duplicate the LLM weights in VRAM?

No. Model weights are loaded into VRAM once and shared across all parallel requests. Only the KV cache and temporary activation tensors scale linearly with concurrent users or batch size.

How much VRAM does Q8_0 KV cache quantization save?

Switching KV cache precision from default FP16 (2 bytes per element) to Q8_0 (1 byte per element) cuts KV cache memory nearly in half (~48–50% savings) with negligible impact on reasoning or coding accuracy.

Why does Gemma 3 27B use far less KV cache at 128K context than Llama 3.3 70B?

Gemma 3 uses a 5:1 interleaved attention ratio where 5 out of every 6 layers use a fixed 1,024-token local sliding window, so only 1/6th of layers grow with full global context length.

Interactive Tools & Referenced Models

Open Live VRAM Simulator