ATRIUMsearch → argument graph
Article · 2026-08-04 · 6 moments

Why An LLM’s Memory Gets Expensive and How to Fix It

In this article, we will learn how LLMs use memory, how it gets expensive, and how to fix it. ✦ AI generated

01
Mechanism

Eviction techniques that drop tokens from the cache to save memory face a structural problem: whether a token matters depends on a question that hasn't arrived yet, so an aggressively trimmed cache can miss information needed later.

Explains that eviction approaches (like keeping a recent window plus attention sinks) face a fundamental problem: you cannot know which tokens future generation steps will need, so aggressive eviction causes retrieval failures on long documents.

transcript

Author: Eviction goes after the token count by dropping entries that the model is unlikely to need. The common approach keeps a window of the most recent tokens, since recent context usually matters most, along with a few tokens from the very start of the sequence. Those opening tokens turn out to play an outsized role. They absorb a large share of attention regardless of what they actually say, acting as anchors that keep the model's output stable. The trouble with eviction is structural. Whether a token matters depends on a question that has yet to arrive. A token we drop now can be exactly the one a later part of the generation needs, and once it is gone, the model generates as though that token had been absent the whole time. This shows up on retrieval tasks, where an aggressively trimmed cache handles a casual chat well and then misses a fact buried in the middle of a long document.

provides context · 1rebuts · 1

02
Mechanism

Grouped-query attention and multi-head latent attention both shrink the cache footprint per token, but GQA is the safe default while latent attention saves more memory at the cost of serving complexity.

Describes two architectural approaches: grouped-query attention (sharing key-value heads across query heads) and multi-head latent attention (compressing keys/values into a latent space), noting their trade-offs in memory savings, quality, and serving complexity.

transcript

Author: Grouped-query attention goes after the key-value head count. In a standard attention layer, every query head carries its own key and value head, so a model with 64 query heads stores 64 sets of keys and values. Grouped-query attention lets several query heads share one key-value head, which drops the number of stored sets sharply. For example, Llama 2 and 3 at 70B and Mistral 7B share down to 8 key-value heads, which cuts the cache by roughly eightfold against full multi-head attention. This is why a recent 70B model can hold a smaller cache than an older 7B one. Multi-head latent attention, introduced in the DeepSeek models, projects the keys and values down into a smaller latent representation before caching them, then expands them back when they are read. The savings are large. DeepSeek-V3 holds around 70 kilobytes per token, where comparable grouped-query models sit between 192 and 328. The cost lies in serving, since the compression adds work on every read and pairs awkwardly with some standard attention implementations, so it tends to pay off most once models and contexts grow large enough that cache traffic dominates.

provides context · 1

03
Mechanism

Paged attention and prefix caching eliminate memory fragmentation and enable cache sharing across requests, cutting wasted memory from 60-80% to under 4% and reducing latency by 50-90% on cache hits.

Explains how paged attention borrows OS-style page allocation to eliminate fragmentation, and how prefix caching lets requests sharing a common prefix reuse the same cached blocks, yielding major throughput and latency gains in production serving.

transcript

Author: Older serving systems reserved one large contiguous block per request, sized for the longest output it might produce. Most requests finished well short of that, leaving the reserved space idle, and the fragmentation added up. Paged attention borrows an idea from operating systems, which break memory into small fixed-size pages and hand them out on demand. The cache gets split into small blocks that can live anywhere in memory, tracked by a lookup table that maps each request to its blocks. The result is that systems that wasted 60 to 80 percent of cache memory to fragmentation dropped that figure below 4 percent, and throughput climbed by two to three times, all from packing the same data more tightly. Since the cache lives in shareable blocks, two requests that begin with the same text can point at the same physical blocks while each holds its own private continuation. This is the foundation of prefix caching, and the productized version that the major APIs call prompt caching. The win is large for any workload that repeats a prefix, such as an agent that sends the same multi-thousand-token system prompt on every call. OpenAI and Anthropic both report cost and latency reductions of 50 to 90 percent on cache hits, with cached tokens billed at a fraction of fresh ones.

04
Mechanism

Quantization halves the cache by moving from 16-bit to 8-bit storage, and halves it again at 4 bits, with quality costs that are negligible at 8 bits but become measurable on demanding tasks at 4 bits.

Describes how quantizing key-value vectors from 16-bit to 8-bit or 4-bit storage directly halves the cache size, with the quality trade-off becoming significant at 4 bits, especially on multi-needle retrieval tasks.

transcript

Author: Quantization goes after the bytes per number. The keys and values are usually stored at 16 bits each, and quantization rounds them to a smaller format such as 8 bits or 4 bits. Since the bytes-per-number term sits right there in the equation, moving from 16 bits to 8 halves the whole cache, and going to 4 bits halves it again. The appeal is that this applies to a model we already have and skips retraining entirely. The quality cost depends on how far we push it. Eight-bit storage often costs well under a percent of accuracy, which puts it within the noise for most workloads. Four-bit storage saves more and starts to show measurable losses on demanding tasks such as multi-needle retrieval, where the model has to pull several specific facts out of a long context.

05
Mechanism

The expense of long-context generation comes less from holding the cache and more from sweeping through all of it on every single decoding token — the cache is a bandwidth cost as much as a storage one.

Explains that the real cost driver is not storing the cache but reading it entirely on every token during the decoding phase, making it memory-bandwidth-bound rather than compute-bound.

transcript

Author: The first phase is prefill, where the model reads the entire input at once. It processes all of the input tokens in parallel and builds their key and value vectors into the cache in a single pass. Prefill keeps the GPU's math units busy, so we call it compute-bound, meaning the limit is how fast the chip can do arithmetic. The second phase is decoding, where the model produces the output one token at a time. Each new token runs an attention step against the whole cache, which means the model reads every stored key and value out of GPU memory before it can emit the next token. It repeats that read for every token it produces. The limit here is how fast the cache can move from memory into the compute units, so we call decoding memory-bound. The expense of long-context generation comes less from holding the cache and more from sweeping through all of it on every single token. A larger cache means more data crossing the memory bus per token, which shows up directly as slower and costlier generation.

gives example · 1supports · 1

06
Context

The KV cache grows with every token, and in a long context it can take up significant GPU memory — for a 70B model at 128K tokens, roughly 40 gigabytes.

Explains that the KV cache, which stores key and value vectors for every token, is the main driver of memory cost in long-context LLM inference, growing linearly with token count.

transcript

Author: A key part of the answer lies in a block of working memory called the KV cache. This memory is built up while the model generates a response. It is separate from the knowledge stored in the model's weights, and it holds the key and value vectors computed for every token of the input. The cache grows with every token, and in a long context, it can take up significant space on the GPU. For example, for a 70-billion-parameter model at a context of 128,000 tokens, it comes to roughly 40 gigabytes, a serious amount of GPU memory that grows with every user you add.

Highlight slides
Related episodes