GGUF Format › Memory Requirements
Language AI HandbookAll chapters GGUF Format › Memory RequirementsOne section · Sections in this chapter10 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Understanding memory requirements helps you choose the right quantization level for your hardware. The relevant constraint is the total memory footprint during inference, not simply whether the model weights fit in RAM. That footprint includes three components: the model weights (whose size depends on quantization), the KV cache (which grows with context length and batch size), and temporary buffers for intermediate activations during the forward pass.
Getting these estimates right before downloading a multi-gigabyte model is time well spent. A model that barely fits in RAM will use virtual memory, causing dramatic slowdowns as the OS pages data in from disk. A rough rule of thumb is to have 20-30% headroom beyond your estimated peak memory to avoid this situation.
def estimate_model_size(params_billion, quant_type):
"""Estimate model size in GB for different quantization types."""
params = params_billion * 1e9
bits_per_weight = {
"F32": 32.0,
"F16": 16.0,
"Q8_0": 8.5,
"Q6_K": 6.6,
"Q5_K_M": 5.7,
"Q5_0": 5.5,
"Q4_K_M": 4.8,
"Q4_0": 4.5,
"Q3_K_M": 3.9,
"Q2_K": 3.4,
"IQ2_M": 2.7,
}
bits = bits_per_weight.get(quant_type, 4.8)
bytes_needed = (params * bits) / 8
gb = bytes_needed / (1024**3)
return gbModel Size Estimates (weights only) ================================================== 7B parameter model: F16 : 13.0 GB Q8_0 : 6.9 GB Q5_K_M : 4.6 GB Q4_K_M : 3.9 GB Q3_K_M : 3.2 GB Q2_K : 2.8 GB 13B parameter model: F16 : 24.2 GB Q8_0 : 12.9 GB Q5_K_M : 8.6 GB Q4_K_M : 7.3 GB Q3_K_M : 5.9 GB Q2_K : 5.1 GB 34B parameter model: F16 : 63.3 GB Q8_0 : 33.6 GB Q5_K_M : 22.6 GB Q4_K_M : 19.0 GB Q3_K_M : 15.4 GB Q2_K : 13.5 GB 70B parameter model: F16 : 130.4 GB Q8_0 : 69.3 GB Q5_K_M : 46.4 GB Q4_K_M : 39.1 GB Q3_K_M : 31.8 GB Q2_K : 27.7 GB

The model size estimates show the practical impact of quantization. A 7B model in FP16 requires about 14 GB, placing it out of reach for most laptops and consumer desktops. Q4_K_M quantization reduces this to approximately 4.1 GB, a 3.4 times reduction that brings it comfortably within the range of an 8 GB laptop. Moving to more aggressive quantization like Q2_K further reduces size to 2.9 GB, at the cost of noticeable quality degradation. These estimates cover only the model weights and do not include the KV cache, which can be substantial for long contexts.
KV Cache Memory Formula
To understand how much memory the KV cache requires, we need to account for the number of layers storing caches, the size of each cache entry, the number of tokens being cached, and the precision of the cached values. During inference, the model stores attention keys and values for all tokens processed so far, allowing subsequent token generation to attend back to them without recomputation. This cache grows linearly with sequence length, making its size a critical factor in memory planning.
The KV cache memory requirement follows a clean formula:
where:
- is the number of transformer layers. Each layer maintains its own independent key-value cache, storing the computed attention keys and values for all tokens processed so far.
- is the hidden dimension size (embedding length). Every cached key or value is a vector of this dimensionality.
- is the context length. It represents how many token positions need to be cached. The actual cache grows from 0 to tokens during inference.
- is the number of bits per KV value (16 for FP16, 8 for quantized cache, etc.). Lower precision reduces memory at some potential cost to attention quality.
- accounts for both key and value caches: every attention layer stores keys used for similarity computation and values used for information retrieval.
- converts bits to bytes.
- converts bytes to gigabytes.
The key insight is that every factor in the numerator contributes multiplicatively. Doubling any of , , or doubles the cache size. This linear scaling in context length is what makes long-context inference so memory-hungry: a model designed for 4K tokens but pushed to 128K tokens needs 32 times more KV cache memory.
Let us work through the full calculation step by step for a typical 7B model with:
- transformer layers
- hidden dimensions
- context length
- bits (FP16 precision)
Substituting into the formula:
So the KV cache for a 7B model at 4K context requires approximately 2 GB of additional memory beyond the model weights. This means a Q4_K_M model needs roughly 4.1 + 2.0 = 6.1 GB total, fitting comfortably in 8 GB of RAM but leaving little headroom for the OS and other applications.
We can build intuition for this number by breaking the calculation into stages. First, the total number of scalar values that need caching:
Over a billion numbers must be stored in memory just to hold the attention state for a 4K-token context. Next, converting to bytes using FP16 (2 bytes per value):
Finally, converting to gigabytes:
The scaling behavior has important practical implications. Since context length appears as a direct multiplier, extending the context window multiplies the KV cache requirement proportionally:
- At 8K context:
- At 16K context:
- At 32K context:
- At 128K context:
This makes KV cache management critical for long-context applications. Moving from 4K to 128K contexts requires 32 times more memory for the cache alone, which quickly exceeds even the most well-equipped consumer hardware. This scaling behavior explains why many long-context models employ specialized techniques like sliding window attention, sparse attention patterns, and quantized KV caches to limit memory growth.

The scaling chart makes the memory challenge visceral. A 70B model requires about 10 GB of KV cache at 4K context and exceeds 32 GB by 16K context. Even the modest 7B model hits the 8 GB threshold around 16K context, meaning that many laptop users running local models cannot use the full context window their model was trained with. Quantizing the KV cache (reducing from 16 to 8 or even 4 bits) offers a way to cut these requirements in half or more, and llama.cpp supports KV cache quantization as a deployment option.