Local LLM VRAM Estimation: Weights, KV Cache, and Sizing

    This article explains how to estimate the memory required to run a local LLM by breaking usage into model weights, KV cache, runtime overhead, and safety margin. It covers weight precision and quantization, KV-cache calculation from model config, measurement caveats, VRAM-based sizing guidance, Mac unified-memory evaluation, risk notes, OOM troubleshooting, and a selection workflow.

    1. Key takeaway: VRAM is determined by three parts

    Official documentation often lists parameter scale and theoretical VRAM under BF16 or FP16 precision, and those numbers usually include weights only. In local operation, actual usage is the sum of three parts:

    • Weights: Space occupied by model parameters. It is independent of conversation length.
    • KV cache: Space used by attention layers to store historical Keys and Values. It grows with context length and concurrency.
    • Runtime overhead: Space used by computation graphs, temporary buffers, GPU runtime context, and similar components.

    Use the following formula to estimate total usage:

    1. Total VRAM ≈ weights + KV cache + runtime overhead + safety margin

    Weights can generally be calculated directly. KV cache requires reading the model configuration. Runtime overhead and safety margin can only be determined through experience and measurement.

    2. Weights: easiest to calculate, but theoretical values are low

    The weight VRAM estimation formula is:

    1. Weight VRAM ≈ parameter count × bytes per parameter

    Common precision formats and their bytes per parameter are:

    Precision format Bytes per parameter Notes
    FP32 4 bytes Common in training, rarely used for local inference
    FP16 / BF16 2 bytes Common format for official weights
    INT8 1 byte Quantization quality loss is usually small
    INT4 0.5 bytes Mainstream choice for local deployment

    Take a model with about 4B parameters as an example:

    • BF16 original weights: about 8GB.
    • INT8 quantization: about 4GB.
    • INT4 quantization: theoretical value about 2GB.

    The 0.5 bytes for INT4 is a theoretical lower bound. Group quantization, such as sharing scaling factors every 32 or 128 parameters, also requires storing metadata such as scaling factors and zero points. Actual size is usually about 5% to 20% higher than the theoretical value. Therefore, the INT4 weights of a 4B model are actually about 2.1 to 2.5GB.

    The same method can estimate larger models. The INT4 weights of a 20B-class dense model are about 10 to 12GB, and the INT4 weights of a 30B-class dense model are about 15 to 18GB. These numbers include weights only; the next two parts still need to be added.

    3. KV cache: the main cause of long-context crashes

    KV cache is the part most easily underestimated. Each time the model generates a token, it stores that token’s Key and Value in every layer for later computation. The longer the conversation, the larger the cache.

    The KV cache estimation formula is:

    1. KV cache ≈ 2 × layers × KV heads × head dimension × context length × bytes per element × concurrency

    The 2 in the formula represents one copy for K and one for V. In models using grouped-query attention (GQA), the number of KV heads is usually smaller than the number of attention heads.

    Estimation steps:

    1. Open config.json in the model directory and look for num_hidden_layers (layers), num_key_value_heads (KV heads), and head_dim (head dimension). If head_dim is absent, calculate it as hidden_size divided by num_attention_heads.
    2. Determine cache precision. FP16 cache uses 2 bytes per element. Some frameworks support INT8 or FP8 KV cache, which can halve the byte count.
    3. Determine the target context length and the number of requests processed simultaneously (concurrency).
    4. Substitute the values into the formula.

    Example: assume a model has 32 layers, 8 KV heads, and a head dimension of 128. It uses FP16 cache and concurrency is 1.

    • Per token: 2 × 32 × 8 × 128 × 2 bytes = 131072 bytes, about 128KB.
    • 8K context (8192 tokens): about 1GB.
    • 32K context: about 4GB.
    • Concurrency 4, 8K context: about 4GB.

    This configuration is only an example. Use the actual model configuration for your numbers. Some frameworks preallocate KV cache based on maximum context length. In that case, peak usage will be higher than the result calculated from actual conversation length. Confirm the allocation strategy in the framework documentation.

    4. Runtime overhead and safety margin

    Runtime overhead includes the framework’s own memory management, temporary activation values during inference, and GPU driver or runtime context. In practice, this part is usually between 1GB and 2GB, depending on the framework, version, and batch size.

    The safety margin is used to handle peak fluctuations. For long-context scenarios, reserve 20% to 30% of total VRAM. If the margin is insufficient, a crash may still occur during a long response even when the estimated weights and KV cache are smaller than the VRAM capacity.

    5. Measurement conditions: why other people’s numbers do not match

    For the same model, VRAM usage can vary widely across environments. Before citing any VRAM number, confirm the following conditions:

    • Framework and version, such as llama.cpp, vLLM, Ollama, or MLX.
    • Quantization format and backend, such as GGUF Q4_K_M, AWQ, or GPTQ.
    • Context length: 2K, 8K, or 32K.
    • Batch size or concurrency.
    • Whether optimizations such as KV cache quantization or FlashAttention are enabled.

    If these conditions differ from your environment, other people’s numbers are only useful as order-of-magnitude references and should not be copied directly.

    6. VRAM tier selection reference

    The table below gives empirical guidance by available VRAM, assuming INT4 quantization by default:

    Available VRAM Common runnable scale Suggested context Suggested concurrency Notes
    8GB About 3B to 4B 4K to 8K 1 Small headroom; 7B-class models usually need shorter context
    12GB About 7B to 8B Around 8K 1 Be careful with long context; test peak usage first
    16GB About 8B (INT8 or INT4); 14B needs INT4 Around 8K 1 to 2 14B models need shorter context
    24GB About 14B (INT8), or 30B-class (INT4) 8K to 16K 1 to 2 30B INT4 weights are about 15 to 18GB; headroom is limited
    48GB About 70B (INT4) 4K to 8K 1 Needs shorter context and sufficient headroom
    64GB About 70B (INT4) Around 16K 1 to 2 Context and concurrency still need item-by-item estimation

    The numbers in this table are only for narrowing down options during selection. Before deployment, run the target framework, target context, and target concurrency once, and record peak VRAM.

    7. How to evaluate Mac unified memory

    Mac uses a unified memory architecture, where the CPU and GPU share the same physical memory. This differs from dedicated VRAM on a discrete GPU, so the evaluation method is different.

    Evaluation steps:

    1. Check total memory and use Activity Monitor to observe used memory when idle. The system, browser, development tools, and other resident apps all consume memory and cannot be fully allocated to the model.
    2. Subtract resident usage from total memory. The remainder is the memory available to the model.
    3. The memory the GPU can directly use is usually lower than total memory. macOS sets an upper limit on GPU-available memory. This limit varies by system version and machine model, so actual measurement is required. Before you have measured data, make a conservative estimate using a fraction of the remaining memory.
    4. Use the formulas above to calculate the sum of weights, KV cache, and runtime overhead, then add the safety margin. The result must be less than the available amounts obtained in steps 2 and 3.

    If model usage exceeds physical memory, the system enables swap. Access speed drops sharply, generation speed may slow significantly, and the interface may even lag. Therefore, on a Mac, the goal should be to keep the model fully in physical memory.

    Take a 16GB Mac running an 8B INT4 model as an example. First close large apps and extra browser tabs, then observe the memory pressure indicator during conversation. If memory pressure turns yellow or red, shorten the context or use a smaller model.

    8. Risk notes

    • Swap causes severe slowdown. When VRAM or memory is insufficient, the system moves data to disk or other storage, and speed drops noticeably.
    • Quantization affects quality. INT4 usually performs worse than INT8 or BF16 on reasoning, code, and math tasks. The size of the drop depends on the model and quantization method. When choosing a quantized version, compare it on your own tasks.
    • Long context needs headroom. If the estimate is close to the VRAM limit, reduce context length or concurrency.
    • Check specifications before buying hardware. For Mac, confirm unified memory capacity, not just the chip model. For PC, confirm discrete GPU VRAM capacity, not just the model name.

    9. OOM troubleshooting order

    When OOM occurs, troubleshoot in the following order, starting with the lowest-cost parameter changes:

    1. OOM at startup: This means weights plus basic overhead already exceed available memory. First confirm the theoretical and actual weight values, then switch to a lower quantization format (for example, INT8 to INT4), or use a smaller model.
    2. Crash after a long conversation: This is most likely KV cache growing with context. First reduce the maximum context length and clear conversation history. If the framework supports it, enable KV cache quantization.
    3. Crash when multiple requests run simultaneously: Reduce concurrency to 1 and test again. After a single request is stable, increase concurrency gradually.
    4. Still failing after parameter adjustments: Check the framework version. Some older versions have memory fragmentation or management issues. Update or roll back the version and compare results.
    5. Close other programs using VRAM or memory, and observe peak values rather than idle values. On NVIDIA GPUs, use nvidia-smi to monitor usage. On Mac, use Activity Monitor to observe memory pressure.

    10. Selection workflow

    1. Define the use case and context requirements, such as general Q&A, long-document analysis, or code completion.
    2. Confirm the candidate model’s parameter scale, and then evaluate it with the estimation method above.