Cost Tutorials
How to Calculate LLM Memory and VRAM Requirements for Inference
To estimate LLM inference memory, add loaded weight memory, KV-cache memory for the planned tokens and concurrent sequences, peak activations and workspaces, runtime overhead, and a measured safety margin. Parameter count alone estimates only the weight component. The final capacity decision should be verified by measuring per-device peak memory under representative long-context and concurrency workloads.
Start with the deployment, not the model name
Memory requirements depend on the exact checkpoint and serving configuration. Record:
- Total resident parameter count
- Architecture type, including dense or mixture of experts
- Stored weight precision or quantization method
- Compute data type
- Number of layers
- Hidden size
- Number of attention heads
- Number of key-value heads
- KV-cache data type
- Input and output sequence distribution
- Maximum concurrent sequences
- Runtime, kernels, and parallelism strategy
- Adapters or auxiliary models loaded with the base model
A family name such as “7B” or “70B” is not a complete specification. Config files and the actual loaded checkpoint are the source for architecture values.
Step 1: estimate raw weight memory
For a dense checkpoint:
raw weight bytes = parameter count × stored bits per parameter ÷ 8
Illustrative raw payloads for 7 billion parameters are:
| Stored representation | Ideal raw weight payload |
|---|---|
| 32 bits | 28 GB |
| 16 bits | 14 GB |
| 8 bits | 7 GB |
| 4 bits | 3.5 GB |
These are decimal gigabytes and exclude every other allocation. Quantized checkpoints also need scales, possible zero points, packing metadata, and layers retained at another precision. The loaded runtime representation can differ from the file size.
For mixture-of-experts models, distinguish total resident parameters from parameters activated per token. Active parameters help explain compute, but memory planning generally starts from all weights resident on the device or device group. Offloading and expert parallelism can change placement.
Read what LLM quantization is before converting a bit label into a memory assumption.
Step 2: add quantization overhead and unquantized modules
A practical weight estimate has this shape:
loaded weight memory = packed weights + scales + zero points + higher-precision modules + runtime representation overhead
Do not apply a universal percentage without measuring the format. Group size, metadata representation, model architecture, and runtime affect overhead.
When a framework exposes a loaded memory-footprint function, record that result after model initialization. Also inspect per-device allocations because automatic placement may leave one accelerator much fuller than another.
Step 3: calculate KV-cache memory
For many decoder-only transformers, a useful conceptual estimate is:
KV bytes = layers × 2 × KV heads × head dimension × bytes per cache value × cached tokens × concurrent sequences
The factor of two represents keys and values.
Use:
head dimension = hidden size ÷ attention heads
Use the model’s key-value head count, not automatically the attention-head count. Multi-head attention, grouped-query attention, and multi-query attention store different numbers of KV heads.
“Cached tokens” should include the tokens retained for each active sequence, including prompt tokens and generated tokens so far. A scheduler with variable sequence lengths will have a distribution rather than one fixed number.
Worked KV-cache example
Consider an illustrative architecture with:
- 32 layers
- 8 key-value heads
- Head dimension 128
- Two bytes per KV value
- 8,192 cached tokens per sequence
- Four concurrent sequences
The estimate is:
32 × 2 × 8 × 128 × 2 × 8,192 × 4 = 4,294,967,296 bytes
That is 4 GiB of KV-cache payload across the four sequences, or about 1 GiB per sequence. The example is not tied to a named model. Block allocation, metadata, reserved cache pools, and runtime implementation can change measured memory.
This calculation shows why context and concurrency must be multiplied together. A model can fit for one request and fail when several long requests become active.
Step 4: add activations and temporary workspaces
Inference does not retain the full backward-pass state required for training, but it still allocates intermediate tensors. Memory can be used by:
- Layer activations
- Attention operations
- Matrix-multiplication workspaces
- Logits and sampling buffers
- Prefill batches
- Compilation or graph-capture buffers
- Tensor-parallel communication buffers
- Tokenization and request metadata
Peak workspace depends on sequence length, batch composition, kernels, and runtime. Long-prompt prefill can create a different peak from decoding.
The reliable method is to measure peak allocated and reserved memory while running representative warm-up and stress workloads.
Step 5: include runtime and allocator overhead
The framework, accelerator context, memory allocator, loaded libraries, and fragmentation consume capacity. Some runtimes reserve a large memory pool for KV blocks or future allocations. Reserved memory is not necessarily wasted, but it reduces what remains available to other processes.
Record:
- Memory immediately after process startup
- Memory after model loading
- Memory after warm-up
- Peak during long prefill
- Peak during concurrent decoding
- Allocated versus reserved memory when available
Subtracting these snapshots helps identify each category.
Step 6: add adapters and auxiliary models
A deployed AI system may load more than one checkpoint:
- LoRA or other adapters
- Embedding models
- Rerankers
- Draft models for speculative decoding
- Safety classifiers
- Vision encoders
- Audio encoders or decoders
They may share a device or run on separate devices. Include their weights, caches, and workspaces in the per-device budget.
A model router that selects among several fully resident models can require much more memory than a router that loads one model at a time.
Step 7: calculate per-device placement
For multiple GPUs, total memory is not enough. Estimate and measure each device:
device memory = local weights + local KV cache + local workspaces + communication buffers + runtime overhead
Tensor parallelism can split tensors, pipeline parallelism can place layers on different devices, and expert parallelism can distribute experts. The split may be uneven. Embedding layers, output heads, and buffers can create a larger allocation on one device.
Do not divide the total estimate by GPU count unless the runtime documentation and measurements show an even distribution.
Step 8: reserve output and concurrency capacity
Build memory scenarios from the service objective:
| Scenario | Input | Output allowance | Concurrency | Purpose |
|---|---|---|---|---|
| Median | measured median | normal response | steady state | ordinary capacity |
| Tail | 95th percentile | high percentile | peak | user experience |
| Guardrail | maximum accepted | configured maximum | controlled | admission policy |
A maximum context window is not the same as a sensible production default. Admission control can reject, queue, or trim requests before they cause out-of-memory failures.
The VRAM definition explains why “fits” and “serves reliably” are separate questions.
Step 9: apply measured headroom
After measuring the worst accepted workload:
required capacity = measured peak × (1 + safety margin)
The margin should reflect observed variance, runtime upgrades, workload changes, and failure policy. There is no universal correct percentage.
Headroom also affects economics. Reserving capacity improves reliability but reduces maximum utilization. Include the deployable throughput, not theoretical saturation throughput, when calculating self-hosted cost.
Use the open-model token cost calculator after the memory-safe concurrency and throughput have been benchmarked.
A complete inference-memory equation
Use this structure for planning:
total required memory = loaded weights + KV cache + peak activations/workspaces + auxiliary models + runtime/allocator overhead + safety margin
Maintain two versions:
- A formula-based estimate used before hardware selection
- A measured result from the exact deployed stack
When they differ, update the model rather than hiding the difference in a generic multiplier.
Training memory is a different calculation
Training and full fine-tuning can require gradients, optimizer states, master weights, and saved activations for backward propagation. Mixed-precision Adam training can use many bytes per parameter before activation memory is counted.
Do not reuse an inference-fit calculation for training. Parameter-efficient fine-tuning reduces trainable state but still has its own activation and optimizer budget.
Validation procedure
Before choosing hardware:
- Load the exact checkpoint and quantization.
- Record memory after loading.
- Warm the selected kernels.
- Run median input and output lengths.
- Run tail sequence lengths.
- Increase concurrency to the service target.
- Exercise adapters and auxiliary models.
- Record per-device peak allocated and reserved memory.
- Verify latency and throughput at the safe point.
- Repeat after runtime or model upgrades.
The GPU VRAM fit tool can narrow hardware choices before this benchmark.
Frequently asked questions
How much VRAM does a 7B model need?
There is no single answer. Raw weight memory depends on precision, while total serving memory also depends on KV cache, context, concurrency, runtime buffers, and overhead.
How do I calculate KV-cache memory?
Use the model’s layer count, key-value head count, head dimension, cache data type, active cached tokens, and concurrent sequences. Then verify against the runtime’s measured allocation.
Can system RAM replace GPU VRAM?
Some runtimes support CPU offload or unified-memory execution, but transfers and lower bandwidth can reduce performance. A model running is not proof that it meets latency requirements.
Should I use checkpoint file size as the memory estimate?
No. File size can exclude runtime conversion, KV cache, activations, workspaces, allocator reservations, and other loaded components.
Sources
How to Calculate LLM Memory and VRAM Requirements for Inference. ByteCosts. Updated 2026-06-21. https://bytecosts.com/blog/how-to-calculate-llm-memory-requirements/