A VRAM calculator for LLMs adds weights, KV cache and overhead. The formula step by step, three worked models from config.json, and what calculators miss.
Every LLM VRAM calculator adds three terms: weights (parameters x bits per weight / 8), the KV cache (2 x layers x KV heads x head dim x context tokens x bytes per value) and about 1 GB of runtime overhead. Devstral Small 2 24B at Q4_K_M with 32k context comes to about 20.7 GB. MoE models still store every expert, and sliding-window layers barely add to the cache.
- params x bits / 8
- 2 x layers x KV heads x head_dim x tokens x bytes
- about 20.7 GB
- 1024 MiB by default
Key takeaways
- Weights: parameters x bits per weight / 8, where Q4_K_M averages 4.89 bits and Q8_0 8.50 bits (llama.cpp's measured table). The GGUF file size is the better number when you have it.
- KV cache: 2 x layers x KV heads x head dim x tokens x bytes per value, all read from config.json. Devstral Small 2 24B costs 160 KiB per token at f16, Qwen3-30B-A3B 96 KiB, gpt-oss-20b 24 KiB.
- MoE models need memory for total parameters, not active ones; gpt-oss-20b keeps its experts in MXFP4, so its GGUF files are 11.5 to 13.8 GB whatever the quant label says.
- llama.cpp's own estimator matches the formula: its fit-params example reports 384 MiB of context for Qwen3-30B-A3B at 4,096 tokens, exactly 96 KiB per token.
- Online calculators mostly agree on weights and differ on KV cache, overhead, MoE and sliding-window attention. The most reliable check is llama-fit-params on your own machine.
A VRAM calculator for an LLM is three additions: the weights, the KV cache for your context length, and about 1 GB of runtime overhead. Every online calculator runs some version of that sum, and they disagree mostly on the last two terms. This page writes the formula out, fills it in for three real models from their config.json files, and checks the result against llama.cpp’s own memory estimator. Configs, file sizes and docs were fetched on 2026-11-03. If you only want to know which GPU tier fits which coding model, the local LLM hardware requirements page covers that; this one is about the math.
What goes into an LLM’s VRAM use?
An LLM’s VRAM use is weights plus KV cache plus overhead, and only the KV cache changes once the model is loaded.
- Weights are fixed by the parameter count and the quantization. They are usually the biggest term.
- The KV cache holds the attention keys and values for every token in the context window. It grows linearly with context and with the number of parallel sequences.
- Overhead is compute buffers, the CUDA or Metal context and allocator slack. llama.cpp’s
--fitoption, which is on by default, keeps a 1024 MiB margin per device, and that is a reasonable allowance to plan with.
Vision models add a fourth term if you load the image encoder: the Devstral Small 2 mmproj file is 0.88 GB at BF16 in the Unsloth repo. Skip it for text-only coding.
How do you calculate weight memory by quantization?
Weight memory is parameters times bits per weight, divided by 8, and the trap is using the nominal bit width. GGUF quant types mix precisions across tensors, so a “4-bit” quant averages more than 4 bits. llama.cpp’s quantize README measures it:
| Quant | Bits per weight | GB per billion parameters |
|---|---|---|
| Q4_K_S | 4.67 | 0.58 |
| Q4_K_M | 4.89 | 0.61 |
| Q5_K_M | 5.70 | 0.71 |
| Q6_K | 6.56 | 0.82 |
| Q8_0 | 8.50 | 1.06 |
| F16 | 16.00 | 2.00 |
Check it against a real file. Hugging Face lists 24.01 billion parameters for Devstral Small 2 24B. At Q4_K_M the formula gives 24.01 x 4.89 / 8 = 14.69 GB; the published Q4_K_M GGUF is 14.33 GB. Most of the gap is the vision tower, which is counted in the parameters but shipped as a separate file. At Q8_0 the formula gives 25.51 GB against a 25.06 GB file. So the formula lands within about 2 to 3% for a dense model, and the file size is exact. When a GGUF exists, use its size. For what each quant level costs in quality, see GGUF quantization explained.
How do you calculate KV cache memory for your context length?
KV cache memory is 2 x layers x KV heads x head dim x context tokens x bytes per value, and every input except the token count comes from the model’s config.json.
The 2 covers keys and values. Bytes per value is 2 for the default f16 cache in llama.cpp (--cache-type-k defaults to f16). The field names to look for are num_hidden_layers, num_key_value_heads and head_dim. Use KV heads, not attention heads: grouped-query attention shares each KV head across several query heads, and a calculator that uses num_attention_heads overstates the cache by that ratio, 4x for Devstral (32 heads, 8 KV heads) and 8x for Qwen3-30B-A3B (32 heads, 4 KV heads).
Sliding-window layers change the count. gpt-oss-20b’s config lists 24 layers, alternating sliding_attention and full_attention, with a 128-token window. Only the 12 full-attention layers grow with context. llama.cpp keeps a small window-sized cache for the others unless you pass --swa-full. Hybrid linear-attention models work the same way: count only the layers whose cache grows.
Per-token cost at f16 for the three example models, from their configs:
| Model | Layers (growing cache) | KV heads | Head dim | KV per token | 8k | 32k | 128k |
|---|---|---|---|---|---|---|---|
| Devstral Small 2 24B (dense) | 40 | 8 | 128 | 160 KiB | 1.34 GB | 5.37 GB | 21.5 GB |
| Qwen3-30B-A3B (MoE) | 48 | 4 | 128 | 96 KiB | 0.81 GB | 3.22 GB | 12.9 GB |
| gpt-oss-20b (MoE, SWA) | 12 of 24 | 8 | 64 | 24 KiB | 0.20 GB | 0.81 GB | 3.22 GB |
A calculator that ignores the sliding window treats all 24 gpt-oss layers as full and doubles the cache: 1.61 GB at 32k instead of 0.81 GB. Qwen3-30B-A3B supports 32,768 tokens natively and 131,072 with YaRN, per its model card, so the 128k column needs that setting.
The cache also scales with parallel sequences. Serving four requests at 32k costs four times the 32k figure in vLLM (max_num_seqs) or in llama-server with several slots (--parallel). Quantizing the cache to q8_0 roughly halves it; KV cache quantization covers the flags and the quality data.
Does the formula match what llama.cpp allocates?
Yes, to the MiB. The fit-params README shows Qwen3-30B-A3B at F16 on an RTX 4090 with a 4,096-token context, and llama.cpp’s memory breakdown reports 384 MiB of context memory. The formula gives 96 KiB x 4,096 = 384 MiB. The same breakdown lists 898 MiB of compute buffers, which is where the “about 1 GB of overhead” rule of thumb comes from.
We build CandorNo GPU? Use a hosted model
Uncensored AI, right in your browser.
Candor runs open models with refusals removed, so it answers the questions other assistants deflect. New accounts get free credits, no card needed.
Try Candor freeWorked examples: how much VRAM do three models need at Q4 and Q8?
Adding the GGUF file size, the f16 KV cache and a 1 GB overhead allowance gives these totals. File sizes are from Unsloth’s GGUF repos for Devstral Small 2, Qwen3-30B-A3B and gpt-oss-20b; all figures are decimal GB.
| Model | Quant | File | Total at 8k | Total at 32k |
|---|---|---|---|---|
| Devstral Small 2 24B | Q4_K_M | 14.33 GB | 16.7 GB | 20.7 GB |
| Devstral Small 2 24B | Q8_0 | 25.06 GB | 27.4 GB | 31.4 GB |
| Qwen3-30B-A3B | Q4_K_M | 18.56 GB | 20.4 GB | 22.8 GB |
| Qwen3-30B-A3B | Q8_0 | 32.48 GB | 34.3 GB | 36.7 GB |
| gpt-oss-20b | MXFP4 (ggml-org file; Unsloth’s Q8_0 is the same size) | 12.11 GB | 13.3 GB | 13.9 GB |
Three things the table shows:
- Context hits dense full-attention models hardest. Devstral gains 4 GB going from 8k to 32k; gpt-oss-20b gains 0.6 GB.
- MoE models are sized by total parameters. Qwen3-30B-A3B activates 3.3B parameters per token but stores all 30.5B, so the Q4_K_M file is 18.56 GB. Active parameters set speed, not memory. If it does not fit, llama.cpp can park expert weights in system RAM; see MoE CPU offload in llama.cpp.
- Some models ignore the quant label. OpenAI post-trained gpt-oss with its MoE weights in MXFP4 (21B total, 3.6B active, per the model card). Unsloth’s gpt-oss-20b files run from 11.46 GB (Q3_K_S) to 13.79 GB (F16), because the experts stay MXFP4 in every variant. A calculator that applies 8.5 bits per weight to 21B parameters predicts about 22 GB for “Q8_0”; the real file is 12.11 GB.
How accurate are online VRAM calculators?
Online VRAM calculators get dense-model weights right and diverge on everything else. Here is what five of them document, read on 2026-11-03:
| Tool | Weights | KV cache | Overhead | Known gaps |
|---|---|---|---|---|
| Hugging Face Accelerate estimator | From the model graph, int4 to fp32 | None | Says to add “up to an additional 20%” for inference | Load size only; no GGUF quant types |
| Flavio Copes calculator and guide | params x bits / 8 | Full formula with KV heads | 10% of weights | Dense models only; no MoE or sliding-window notes |
| Incoherency VRAM and context calculator | Presets or custom architecture | Uses KV heads; solves for max context and concurrency | Not documented | Formulas and overhead not published |
| DevTk VRAM calculator | 0.56 bytes per parameter for Q4_K_M | “Simplified GQA estimation” | ~500 MB in the FAQ, 1.0 GB in its example | Its 405B example shows a KV cache larger than the weights at 4k context, which the formula above does not produce for a grouped-query model at that length |
| VRAM.cpp (browser port of llama.cpp’s fit logic) | From the GGUF | llama.cpp’s own | llama.cpp’s own | Author calls MoE fitting “a bit wonky” (r/LocalLLaMA) |
The pattern: use a calculator that asks for KV heads (or reads config.json), lets you set context and parallel sequences, and treats MoE as total parameters. Then sanity-check sliding-window and native-MXFP4 models by hand, because few tools model them.
The closest thing to ground truth is llama.cpp itself. llama-fit-params runs the same fitting code the server uses and prints the context, layer and tensor-offload arguments that fit your free memory:
llama-fit-params --model Devstral-Small-2-24B-Instruct-2512-Q4_K_M.gguf -c 32768vLLM is different: it does not size to the model, it reserves a fraction of the card up front and gives the remainder to the KV cache. On vLLM’s main branch the default gpu_memory_utilization is 1.0 with its extensible KV cache and 0.92 otherwise (CacheConfig), so nvidia-smi shows a nearly full GPU whatever the model.
What do developers report vs the math?
Developers’ reports line up with the formula, and the gaps they hit are context and runtime differences, not weight math. These are community reports, not tests run for this page.
- “Fits” at 2k is not “fits” at 32k. A Hacker News commenter reviewing a model-fit tool pointed out that a 7B model that fits at 2k tokens can run out of memory at 32k, and that calculators which ignore context length are where people get burned (HN).
- The runtime changes the answer. One developer runs gpt-oss-20b in Ollama on a 16 GB Mac but gets “out of memory” from current llama.cpp with the MXFP4 file, regardless of parameter tuning (HN). The weights are 12.11 GB either way; the difference is how each runtime budgets context and buffers against the Mac’s GPU memory limit.
- Partial offload is the escape hatch, at a price. On a 16 GB M1 MacBook Pro, one user ran gpt-oss-20b in LM Studio with about 8k context and 8 of 24 layers on the GPU, and described it as slow but usable (HN). Another, with 16 GB of VRAM and 64 GB of RAM, says gpt-oss-20b runs fast on the GPU but anything that spills into system RAM drops tokens per second sharply (HN).
- Old models without GQA really are expensive. A 2024 Hugging Face discussion asked why a calculator showed a large context cost for deepseek-coder-6.7b-instruct compared with its siblings (discussion). The config explains it: 32 KV heads for 32 attention heads, no grouping, so 512 KiB per token at f16, more than three times Devstral’s 160 KiB. The calculator was right.
FAQ
How much VRAM do I need for an LLM?
Add the GGUF file size, the KV cache for your context, and about 1 GB. For Devstral Small 2 24B at Q4_K_M that is about 16.7 GB at 8k context and 20.7 GB at 32k. For gpt-oss-20b it is about 13 to 14 GB.
How do I calculate KV cache size for a given context length?
Multiply 2 x layers x KV heads x head dim x tokens x bytes per value, taking the first three from config.json. At f16, Qwen3-30B-A3B is 2 x 48 x 4 x 128 x 2 bytes = 96 KiB per token, so 32k tokens is 3.22 GB.
Do MoE models need less VRAM?
No. All experts must be in memory, so size by total parameters: Qwen3-30B-A3B stores 30.5B even though 3.3B are active per token. MoE models need less compute per token, and their experts can be offloaded to system RAM with less speed loss than a dense model.
Why does my GPU show more VRAM used than the calculator?
Usually the runtime. vLLM reserves most of the card up front for the KV cache, llama.cpp may pick a larger context than you expected when -c is unset, and multiple parallel slots multiply the cache. Overhead also varies by backend.
Is there a VRAM calculator that is actually accurate?
For GGUF models, llama.cpp’s llama-fit-params is the most reliable, because it runs the allocator’s own fitting logic on your hardware. For planning before download, any calculator that uses KV heads, your context and total MoE parameters will land within a few percent on dense models.
We build CandorSkip the setup
Want an uncensored model without hosting one?
Candor (candorai.dev) is an uncensored AI chat app that runs in your browser: it answers the questions mainstream assistants dodge, on health, money, law and work, without the lecture. New accounts get free credits, no card needed.
Facts from candorai.dev, checked Nov 10, 2026. Candor never changes how we rank or review models. Disclosure.
Sources (10)
- llama.cpp quantize README: bits per weight by quant type (ggml-org/llama.cpp)
- llama.cpp fit-params README: llama-fit-params and the memory breakdown (ggml-org/llama.cpp)
- llama.cpp server README: --fit, --fit-target, --swa-full, --cache-type-k, --parallel
- Devstral Small 2 24B Instruct 2512 model card and config.json, Hugging Face (Mistral AI)
- Qwen3-30B-A3B model card and config.json, Hugging Face (Qwen)
- gpt-oss-20b model card and config.json, Hugging Face (OpenAI)
- Unsloth GGUF repos: Devstral Small 2 24B, Qwen3-30B-A3B, gpt-oss-20b file sizes
- vLLM CacheConfig source: gpu_memory_utilization and cache_dtype
- Model memory estimator, Hugging Face Accelerate docs
- How much VRAM do you need to run an LLM locally?, Flavio Copes (updated Aug 2026)
Written by Refusal Rate editors from papers, model cards, official docs and pricing pages. Anything we ran ourselves is labeled as our own test, with the hardware and settings. Our method.