How the memory for a million tokens shrank 150,000 times
The GPU memory needed for 1M tokens has gone from 130 TB before 2022 to under 1 GB in DeepSeek-V4.1-Flash. Let's go through each of the breakthroughs that made this possible.

In 2022, a model reading a million tokens would have needed more than 130 TB of GPU memory on top of its weights. That's the memory of about 1,700 H100s (tens of millions of dollars of hardware) for one conversation.
DeepSeek-V4.1-Flash, released in September 2026, handles the same million tokens in 890 MB, 150,000 times less. That fits on virtually any GPU from the past decade. It's a big part of why you can rent a million-token context window today, and why providers can keep a conversation's memory around and charge less when you send the same text again.
This post goes through each of the advancements that made this possible. This has not been a linear, additive journey. Ideas that failed in 2019 because they degraded quality too much are back in 2026. There is no standardized set of techniques all models use yet, but in the end, the amount of memory needed comes down to four numbers multiplied together, and each technique makes one of the four smaller.
Memory needed to handle 1M tokens
Where the memory goes
First, we need to understand where all of this memory goes.
An LLM turns text into tokens, and each token into a high-dimensional vector. The vectors pass through a stack of layers, and each layer rewrites them. At the end, the last token's vector is used to pick the next token.
The part of a layer that uses memory is called attention, which uses three key pieces of information: keys, values and queries. In this step tokens exchange information, which lets the model work out what a word means given the words before it. Without attention, "bank" would mean the same thing in "river bank" and in "bank account".
What the KV cache holds, in four steps
Each token becomes a list of numbers that moves up through the model's 80 layers.
a token's numberskeys and values, kept
Sizes from Llama 1 65B: 80 layers, 64 heads, 128 numbers per key or value.
When a token looks back at the earlier ones, it brings a query and compares it with each earlier token's key. Each comparison gives a score, which says how much that earlier token matters to this one. That score is then multiplied by the values of the earlier tokens. In "river bank", "bank" gives "river" a high score and takes in most of its value.
Each layer does this many times at once. A layer runs several attentions side by side, called heads. Each head has its own weights, so it makes its own queries, keys and values and learns to look for something different.
Our starting model is Meta's Llama 1 65B, from February 2023. It has 80 layers and 64 heads per layer, and a key or a value is 128 numbers of 2 bytes each. We'll call what one token leaves in one layer its entry. Here that's a key and a value in each of 64 heads, or 2 × 64 × 128 = 16,384 numbers. The cache for one token is four numbers multiplied together:
The cache for one token in Llama 1 65B
entry per tokenwhat one token leaves in one layer
×80layers that cacheevery layer builds its own cache
×16,384numbers per entry64 keys + 64 values, 128 numbers each
×2bytes per number16 bits for each number
=2,621,440bytes per token2.5 MB per token, 2.5 TB for a million tokens
That's 2.5 MB per token, or 2.5 TB for a million tokens. Keep those four numbers in mind. Most of this post is about making them smaller, one at a time.
The score table: 130.5 TB
Until 2022, the cache wasn't even the biggest problem. The scores were. There's one score for every pair of tokens, and when a model read a prompt, standard code computed them for all the tokens at once and held the whole table in memory. Meta's own code for Llama 1 still works that way.
The score table, for four tokens
Every token gets a score for each token it looks back at. Side by side, the scores make the score table.
The scores shown are illustrative.
A million tokens squared is about 1.1 trillion scores. At 2 bytes each, that's 2 TB for one head, and a layer computes its 64 heads together, so 128 TB. The table is thrown away after each layer, so it only has to exist once. It still has to fit.
1M tokens: 130.5 TB. That's 128 TB of scores plus 2.5 TB of cache, about 1,700 H100s.
FlashAttention, the free step: 2.5 TB
FlashAttention, by Tri Dao and colleagues in 2022, got rid of the score table. It splits the queries, keys and values into tiles small enough for the GPU's fast on-chip memory, and computes the scores one tile at a time. The scores are made and used on the chip and never written to the GPU's main memory, so they take the space of one tile instead of terabytes. It's also faster, because it skips all that memory traffic.
What makes FlashAttention special is that it costs nothing. The result is exactly the same as before, because it's the same computation done in a different order. It also needs no change to the model. It's a change to the software that runs the model, so every existing model got it.
1M tokens: 2.5 TB. 52 times less, and still 32 H100s of memory for one conversation.
That was the last free step. With the score table gone, the cache is the only memory that grows with the conversation, and the only way to shrink it is to cut one of the four numbers. Each cut changes what the model computes.
Grouped-query attention: 320 GB
Numbers per entry: 16,384 to 2,048.
The obvious waste is that all 64 heads store keys and values of their own. In 2019 Noam Shazeer proposed multi-query attention (MQA): keep the 64 query heads, which aren't cached anyway, but make them all share one key and one value. The entry shrinks 64 times and the cache drops to 40 GB. Google's PaLM shipped it. The catch was quality. If every head has to read the same key and value, the heads can't each look for something different.
Which keys and values each query head reads
One layer of 80. Every head holds 128 numbers per token, so the cache grows with the number of red cells.
Grouped-query attention (GQA), from Google in 2023, took the middle road. The query heads are split into groups, and each group shares one key and one value. Llama 2 70B, Llama 1 65B's successor from July 2023, has the same 80 layers and 64 query heads, in 8 groups. Its entry is 8 keys and 8 values, or 2,048 numbers.
1M tokens: 320 GB. Eight times smaller, and still four H100s.
Multi-head latent attention: 68.6 GB
Numbers per entry: 2,048 to 576.
Grouped-query attention shrinks the entry by making heads share. DeepSeek's V2 paper in 2024 found a way to shrink it further while giving every head keys and values of its own. It doesn't store them at all.
That works because of where keys and values come from. Every head computes its key and its value from the same input, the token's vector, using matrices that are part of the model's weights. So 64 keys and 64 values were never 128 separate pieces of information. They all come from one vector, and anyone holding that vector and the weights can compute them again. Nobody cached the vector instead, because it's big. In Llama 1 65B it's 8,192 numbers for the vector, versus 2,048 numbers for the GQA KV cache.
Multi-head latent attention (MLA) makes that shared input small. Instead of storing the whole token vector, first it is projected into a lower dimension (a latent vector) and that's what is being cached. DeepSeek-V3 compresses each token from 7,168 numbers to only 512, plus an extra 64 that encode the token's position.
Each head has its own matrix that expands the latent back into a key and a value when they're needed, so by compressing tokens into latent vectors, we are able to bring back one key and one value per head, instead of the shared paradigm from grouped-query attention.
What one token leaves in the cache, per layer
Llama 2 70B has 80 layers and DeepSeek-V3 has 61. While generating tokens, the up-projection doesn't run. It folds into the query and output weights, and the heads read the 576 cached numbers directly.
DeepSeek-V3, from December 2024, has 61 layers and 128 heads. Its entry is 512 + 64 = 576 numbers.
1M tokens: 68.6 GB. It fits in one H100, with 11 GB to spare.
KV cache quantization: 46.9 GB
Bytes per number: 2 to about 1.
Every cache so far spends 2 bytes on each number. Attention doesn't need that much precision. Keys are only used to compute scores, and a score that's slightly off still ranks the same tokens first.
Storing a number in fewer bits means we must lose precision. At 8 bits per number, there are only 256 possible values. At 4 bits per number, only 16. This is called quantization. DeepSeek-V3.2, from late 2025, stores its latents in 8 bits. An entry drops from 1,152 bytes to 656, which on its own would bring the cache down to 39 GB.
DeepSeek Sparse Attention
DeepSeek's memory usage is actually higher than that, because they added a new innovation precisely to handle huge context windows efficiently. You see, once you're at 1M tokens, each new token needs to be compared to all of the previous million tokens to see how each of them affects the newly added token. In terms of compute, that starts being a problem, because this needs to happen for every head, for every layer.
DeepSeek-V3.2 added DeepSeek Sparse Attention (DSA), which is a mechanism to only look at the 2,048 most relevant tokens when calculating attention, instead of having each token attend to every possible previous token. That makes sense. Once you're at 1M tokens, there's probably a lot of conversational history that might not directly apply to the current topic being discussed.
In order to support this, V3.2 adds an indexer. The indexer needs a small key of its own for every token, which puts 8 GB back.
1M tokens: 46.9 GB.
Fewer entries than tokens: 3.4 GB
Entries per token: 1 to 1/4 and 1/128.
Every cache so far got smaller by shrinking the entry. The number of entries never changed. It was one per token, in every layer. DeepSeek-V4, released in April 2026 with a 1M-token context, keeps fewer entries than tokens.
It does that by merging neighbors. Each token is still compressed to a small vector, as in MLA. But the layer no longer caches one vector per token. It takes the vectors of 4 consecutive tokens, computes a weighted average of them, and caches only the average. The four originals are thrown away.
It's the same idea as shrinking a photo. Four neighboring pixels become one, and the picture still reads fine, but you can't zoom back in.
V4 has two kinds of layer, and they alternate. One kind merges every 4 tokens into an entry. A million tokens still leave 262,144 entries there, so it keeps V3.2's indexer to pick the 512 most relevant ones. The other kind merges every 128 tokens into an entry. That turns a million tokens into 8,192 entries, few enough to read them all.
What 128 tokens leave in a layer's cache
Before V4, 128 tokens left 128 entries in a layer. In V4 they leave 32 in one kind of layer and 1 in the other.
a tokena cache entry
In V4, an entry is a weighted average of the tokens above its line.
Merging loses detail, and detail matters most in the most recent text. So every layer also reads the latest 128 tokens unmerged, from a small cache that stays the same size however long the conversation gets.
V4 also drops MLA's expansion step. An entry is one vector of 512 numbers that all 64 heads read directly, as both key and value. That's multi-query attention's single shared head from 2019, back with a vector four times as wide.
1M tokens: 3.4 GB for V4-Flash, the smaller V4 model. That's almost 14 times smaller than V3.2.
Fewer layers that cache: 890 MB
Layers that cache: 41 to 4. Bytes per number: about 1 to about half. Entries per token: up again, to 1/2 and 1.
The last of the four numbers is the number of layers that cache. In every model so far, each layer builds a cache of its own, and V4-Flash has 41 of them. In DeepSeek-V4.1-Flash, from September 2026, only four do.
Which layers keep a cache of their own
Layer types from each model's config.json. In V4-Flash and V4.1-Flash every layer also keeps the last 128 tokens uncompressed, a fixed 3 MB or so per conversation, not counted here.
The other layers borrow. One layer builds a cache, and the layers after it read that cache instead of making their own. The model has 40 layers and four caches: three in the lower half, each shared by six layers, and one shared by the whole upper half.
Sharing saves so much that V4.1 can undo most of V4's merging. V4 had 41 caches to pay for, so it merged hard to keep each one small. With four, V4.1 spends part of the saving on detail. The three caches in the lower half merge 2 tokens per entry, where V4 merged 4. The cache in the upper half doesn't merge at all, so every token has an entry of its own again. The layers that merged 128 tokens into one are gone.
V4.1 does cut the bytes again, from 8 bits per number to 4. With only 16 levels to round to, DeepSeek trains the model with the 4-bit cache in place, so the weights learn to work with it.
1M tokens: 890 MB. That's about 1% of an H100, and almost 4 times smaller than V4-Flash, with less merging.
Four numbers, each one cut
Here are the four numbers at every step. The number each step cut is in bold.
| Model | Entries per token | Layers that cache | Numbers per entry | Bytes per number | 1M tokens |
|---|---|---|---|---|---|
| Llama 1 65B, 2023 | 1 | 80 | 16,384 | 2 | 2.5 TB |
| Llama 2 70B, 2023 | 1 | 80 | 2,048 | 2 | 320 GB |
| DeepSeek-V3, 2024 | 1 | 61 | 576 | 2 | 68.6 GB |
| DeepSeek-V3.2, 2025 | 1 | 61 | 576 | about 1 | 46.9 GB |
| DeepSeek-V4-Flash, 2026 | 1/4 and 1/128 | 41 | 512 | about 1 | 3.4 GB |
| DeepSeek-V4.1-Flash, 2026 | 1/2 and 1 | 4 | 512 | about half | 890 MB |
From V3.2 on, the totals also include the indexer's small key for each entry.
The cache is 2,900 times smaller than where it started. Add the score table FlashAttention removed, and the memory for a million tokens fell 150,000 times.
In this post, we have followed Llama 2 and then DeepSeek. Other labs have included these same techniques in their models: GLM-5, Kimi K2 and Mistral Large 4 all use MLA. Others took different turns. Google's Gemma models interleave sliding-window layers with a few full ones, and Qwen3-Next and Kimi Linear replace most attention layers with linear attention, whose memory doesn't grow with the conversation at all. We covered those hybrids in an earlier post.
Recap: 130 TB to 890 MB
In 2022, one conversation of a million tokens needed 130.5 TB of GPU memory, about 1,700 H100s. DeepSeek-V4.1-Flash needs 890 MB, about 1% of one H100. That's 150,000 times less in four years.
The first cut was free. Most of the 130.5 TB was a table of scores that the model used once and threw away. FlashAttention stopped storing it, with no change to the model or its answers. That left the KV cache, which is what the model keeps for every token it has read.
Every cut after that changed the model. Newer models keep fewer numbers for each token and fewer bits for each number. They merge neighboring tokens into one entry, and they let many layers share one cache. Most of these cuts give up a little quality for a lot of memory.
The smaller cache changes what providers do with it. At 2.5 TB, a provider had to free a cache the moment a conversation went quiet. At 890 MB it's a file that can be written to a an SSD or even streamed through intra-datacenter network.




