AI MEMORY ARCHITECTURE AND LLM INFERENCE
Increasing an AI model's context window can make it more useful for analyzing large documents, code repositories, and lengthy conversations. However, a longer context also increases the amount of memory required during inference.
One important component is the Key-Value (KV) cache. It stores attention information from previously processed tokens so that the model does not need to recompute the same key and value representations at every generation step.
This guide explains how KV cache memory changes between 32K and 128K context windows, how model architecture affects the calculation, and what techniques can help manage memory on consumer GPUs and larger inference systems.
KV Cache Memory Estimator
Estimate theoretical KV cache memory based on model architecture, context length, cache precision, and concurrent sequences.
This is a theoretical estimate. Actual memory usage depends on the inference engine, quantization format, runtime overhead, and model implementation.
The estimator uses approximate Q4 model-weight sizes and theoretical cache element sizes. It does not represent a hardware benchmark or guarantee that a configuration will run successfully.
KEY TAKEAWAY
For the same model architecture, cache precision, and batch size, increasing context from 32K to 128K requires approximately four times the KV cache memory. This is a linear relationship with sequence length, not an exponential increase. Model weights remain largely fixed, while the cache grows as additional tokens are processed.
Contents
1. Understanding KV Cache Memory
Autoregressive language models generate output one token at a time. Without caching, previously calculated key and value representations would need to be recomputed repeatedly. KV caching stores these representations for reuse during subsequent decoding steps.
The cache is particularly important for models using Grouped-Query Attention (GQA), which uses fewer key-value heads than query heads to help reduce memory consumption. For a standard transformer using conventional attention layers, the approximate KV cache requirement is:
KV Cache (Bytes) = 2 × Layers × KV Heads × Head Dimension × Bytes per Element × Tokens × Batch Size
The multiplier 2 accounts for the Key and Value tensors. The calculation assumes the same cache dimensions across layers and does not include allocator or metadata overhead.
For example, Llama 3.3 70B has 80 transformer layers, 8 key-value heads, and a head dimension of 128. With FP16 cache storage, each token requires approximately 327,680 bytes for its key and value representations across all layers. See our guide: How Grouped-Query Attention (GQA) Reduces LLM Memory Requirements.
2. 32K vs. 128K Context Scaling
Context length and KV cache memory scale approximately linearly when architecture, precision, and active sequence count remain unchanged. For the Llama 3.3 70B configuration described above, theoretical FP16 cache requirements are:
| Context | FP16 Cache | Relative Size |
|---|---|---|
| 4K | 1.34 GB | 1× |
| 32K | 10.74 GB | 8× |
| 64K | 21.47 GB | 16× |
| 128K | 42.95 GB | 32× |
These figures use decimal GB (1 GB = 1,000,000,000 bytes) and represent theoretical tensor storage rather than measured GPU allocations. Adding a ~42.8 GB quantized 70B weight footprint to a 42.95 GB FP16 cache yields ~85.75 GB before runtime overhead—explaining why a model can load at short context yet fail on long prompts. Related guide: Exact VRAM and GPU Requirements for 8B, 14B, 32B, and 70B Models.
3. How PagedAttention Manages Memory
Traditional KV cache allocation strategies often reserve contiguous chunks larger than a sequence immediately uses. PagedAttention, introduced in the vLLM project, organizes KV cache storage into fixed-size blocks:
Main PagedAttention Concepts
- Block-based allocation: KV cache data is stored in fixed-size blocks rather than one large contiguous buffer.
- Block tables: Logical token positions map dynamically to physical memory blocks.
- Dynamic allocation: Additional blocks are allocated as sequences grow.
- Memory sharing: Compatible engines can share cache blocks in supported prefix-caching workflows.
PagedAttention improves memory management and lowers fragmentation, but it does not remove the underlying storage requirements of key and value tensors. Compare serving engines in vLLM vs. Ollama vs. llama.cpp.
4. KV Cache Quantization: FP16 vs. FP8 vs. 4-bit
Lowering cache precision reduces raw cache storage, though actual support, metadata overhead, and numerical accuracy vary by GPU, model, and inference engine:
| Precision | Bytes per Element | Theoretical Reduction |
|---|---|---|
| FP16 / BF16 | 2 | Baseline |
| FP8 | 1 | 50% |
| 4-bit | 0.5 | 75% |
Evaluate output quality on representative prompts before deploying lower-precision caches in production, as scaling factors and metadata affect real-world savings.
5. Batch Size and Concurrent Inference
Running multiple sequences concurrently multiplies KV cache requirements approximately in proportion to active sequence count (e.g., 4 equal 32K sequences at 10.74 GB each require ~42.96 GB of cache storage alone).
Concurrency calculation
Total KV Cache ≈ Per-Sequence KV Cache × Number of Active Sequences
In multi-agent apps, context trimming and structured state help prevent runaway cache growth. See: LangGraph vs. CrewAI vs. AutoGen.

6. Hardware Memory Requirements for 70B Models
| Hardware | Available Memory | Practical Consideration |
|---|---|---|
| RTX 4090 | 24 GB VRAM | Insufficient for typical full-resident 70B Q4 weights and long context. |
| Dual RTX 3090 | 48 GB combined VRAM | May accommodate some 70B Q4 configurations at shorter contexts, depending on weight size and runtime overhead. |
| Dual RTX 4090 | 48 GB combined VRAM | Similar aggregate capacity, with different performance and hardware characteristics. |
| Apple Silicon system | Depends on unified memory configuration | Unified memory can host larger model workloads, subject to available memory, bandwidth, and software support. |
| NVIDIA H100 | 80 GB HBM | Designed for demanding AI workloads, though 80 GB does not automatically fit every 70B 128K workload. |
Remember that two GPUs do not automatically merge VRAM into one unified pool—distribution requires tensor or pipeline parallelism across PCIe/interconnects. Related guides: PCIe x8 vs. x16 for Dual-GPU LLM Inference and Mac Studio Ultra vs. RTX 4090 for Local LLMs.
7. Practical Ways to Reduce KV Cache Memory
- Choose an appropriate context length: Use 16K or 32K if 128K is not required.
- Evaluate supported cache precision: Test FP8 or lower-precision cache formats for quality and stability.
- Control concurrent requests: Limit simultaneous long-context streams to prevent peak OOMs.
- Use prompt caching where appropriate: Reuse prefix states (Prompt Caching Guide).
- Monitor real memory usage: Profile weights, KV cache, and buffers under representative workloads.
8. Frequently Asked Questions
Why does moving from 32K to 128K context increase KV cache memory?
Increasing context from 32K to 128K multiplies sequence length by four, so theoretical KV cache memory also grows by roughly four times for a fixed architecture, precision, and batch size.
Does PagedAttention reduce the actual KV cache memory requirement?
PagedAttention reduces fragmentation and unnecessary pre-allocation through block-based management, but it does not eliminate the memory needed to store key and value tensors.
How much memory can FP8 KV cache save?
FP8 uses one byte per element instead of two bytes for FP16 (~50% smaller raw tensor data before engine overhead), subject to hardware and model support.
Can a 24GB GPU run a 70B model at 128K context?
A typical 4-bit 70B model requires ~40–45 GB for weights alone, exceeding 24GB before KV cache and runtime buffers are included.
Does increasing batch size increase KV cache memory?
Generally, yes—active sequences multiply total cache requirements, depending on sequence lengths and prefix sharing.
Is 128K context always better than 32K?
No. Longer contexts raise memory usage and can slow inference; choose the context length that fits your actual information needs.
Conclusion: Plan Memory Around the Complete Workload
Moving from 32K to 128K context roughly quadruples theoretical KV cache demand. Combining theoretical calculations with measurements from your actual model, quantization format, and workload is the most reliable way to size AI hardware.
Additional Technical Resources
Editorial Disclaimer: Memory figures in this article are theoretical estimates based on the stated model architecture and cache element sizes. Model weight sizes are approximate and depend on the quantization format and model files. Actual GPU memory usage varies with inference software, runtime allocations, hardware, cache implementation, and workload. This guide is intended for educational and technical planning purposes and does not guarantee specific hardware compatibility or performance.
