LLM Inference Memory Ledger: Why KV Cache Dominates GPU Memory

    This article breaks down where GPU memory actually goes during LLM inference and why KV cache, not model weights, is the resource that scales with traffic. It explains how KV cache is calculated layer by layer and position by position, which assumptions the formula depends on, and the engineering techniques used to manage it: paged cache allocation, quantization and compression, attention structure changes, scheduling and prefix reuse, and tiered cluster coordination. It closes with a capacity estimation and load testing checklist for turning theoretical limits into usable serving capacity.

    1. The Underestimated Memory Bill

    Many teams plan inference capacity by asking a simple question: how big is the model, and how many GPUs does it need? Then they hit out-of-memory errors shortly after launch, while throughput stays flat. The reason is straightforward: static weights are only part of the picture, and the component that grows linearly with traffic is the attention cache.

    Consider a common set of numbers. A model in the 7B parameter range takes roughly 14GB in half precision. Pull a single request’s context to 4096 tokens, and the attention cache adds about 2GB. At 100 concurrent requests, that is roughly 200GB, while a mainstream accelerator card typically has only 80GB of memory. In other words, the cache footprint can be more than ten times the weight footprint.

    1.1 What Actually Consumes the Memory

    Inference memory splits into four parts: model weights, attention cache, runtime activations and temporary buffers, and framework plus communication overhead. The first two set the capacity ceiling; the last two determine stability. If capacity planning only looks at weights, it will inevitably underestimate the peak.

    1.2 Why the Cache Expands Faster Than the Weights

    Weights are a constant; the cache is a variable. Weights do not change with the number of requests, but the cache is proportional to concurrency multiplied by sequence length. Double the concurrency and double the context, and the cache grows fourfold. This is multiplicative amplification.

    2. Where KV Cache Comes From and How to Calculate It

    Autoregressive generation looks back at the keys and values of every previous token each time it produces a new one. Without caching, every step recomputes the entire prefix, and complexity grows quadratically with length. With caching, each step only computes the new token, trading repeated computation for memory. This is a classic case of trading space for time.

    2.1 Storage Granularity: Per Layer, Per Position

    The cache is not a single copy. Each layer stores one key and one value per position. The formula is:

    1. bytes per token = 2 (K/V) × num_layers × hidden_dim × bytes_per_value
    2. example: 2 × 32 × 4096 × 2 = 524288 bytes ≈ 0.5MB
    3. 4096 tokens → about 2GB; 100 concurrent → about 200GB

    2.2 Assumptions and Sensitivity to Variables

    This formula assumes multi-head attention with every head dimension fully retained. Once you introduce structures such as multi-query or grouped-query attention, the number of cached heads is compressed and capacity can drop by several times. Conversely, if the context grows from 4K to 32K, the cache grows another eightfold. Any capacity conclusion must be tied to three premises: model structure, sequence length, and precision. Quoting numbers without those premises is misleading.

    3. Engineering Techniques That Put Memory to Work

    3.1 Paged Cache Management

    Allocating large contiguous blocks produces heavy fragmentation, which leads to “enough total memory, but allocation fails.” Borrowing the paging idea from operating systems, split the cache into fixed-size blocks allocated on demand, and map logical sequences to physical blocks through an index table. This substantially improves effective utilization and allows requests of different lengths to run together.

    3.2 Quantization and Compression

    The cache can use a more aggressive precision than the weights, such as 8-bit or lower, combined with per-channel scaling to limit accuracy loss. You can also merge the keys and values of adjacent tokens, or evict selectively, keeping only the parts that materially affect generation. The compression ratio and generation quality must be evaluated per workload; there is no one-size-fits-all setting.

    3.3 Attention Structure Optimization

    Adopting multi-query or grouped-query attention, where multiple query heads share one set of key and value heads, is the most direct structural way to reduce the cache. Combined with sliding windows and local attention, memory growth over long contexts can be pushed from linear to nearly constant.

    3.4 Scheduling, Batching, and Reuse

    Bucket requests by length and use continuous batching so new requests can join at any time and finished requests can leave at any time, avoiding a stall while the whole batch waits. For identical prefixes such as system prompts, templates, and multi-turn history, apply prefix cache reuse so the repeated portion is stored only once. The gains are especially clear in multi-turn conversations and fixed-prompt scenarios.

    3.5 Tiering and Cluster Coordination

    Offload cold cache to host memory or storage further out, and keep hot data in GPU memory. Across multiple cards, use tensor parallelism to split weights and pipeline parallelism to split layers, then combine that with the elastic scaling and monitoring alerts of a container platform. Only then can you scale out before a peak arrives and reclaim capacity in the troughs.

    4. Capacity Estimation and Load Testing Checklist

    Before launch, answer at least five questions: How many bytes does the cache consume per token? What are the target concurrency and the average and peak context lengths? How much do weights and cache each occupy? How much headroom is reserved for activations and fragmentation? What cache hit rate do you expect? Then run stepped load tests, increasing pressure gradually while watching the memory curve, queueing latency, and the throughput inflection point, calibrating theoretical capacity into usable capacity.

    5. Conclusion

    The cost core of inference serving is shifting from fitting the model into memory to managing the cache. Treat the cache as a first-class citizen with paging, quantization, structural optimization, and reuse, and the concurrency a single card can serve often increases severalfold. Built on the general container, monitoring, and storage capabilities of cloud platforms such as Baidu AI Cloud, this approach can be implemented directly as a measurable, elastically scalable inference capacity system.