Engineering

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.

Ximo Guanter
Ximo Guanter, Co-founder & CTO

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

Scale
1 GB10 GB100 GB1 TB10 TB100 TBGPU memory beyond the weights, log scaleone H100: 80 GBMulti-head, no FlashAttentionLlama 1 65B, 2023KV cachescore table130.5 TBMulti-head + FlashAttentionsame model, cache only2.5 TBGrouped-query attentionLlama 2 70B, 2023320 GBMulti-head latent attentionDeepSeek-V3, 202468.6 GB8-bit cache + indexerDeepSeek-V3.2, 202546.9 GBFewer entries than tokensDeepSeek-V4-Flash, 20263.4 GBShared caches + 4-bitDeepSeek-V4.1-Flash, 2026890 MB

Memory needed to handle 1M tokens

Scale
1 GB10 GB100 GB1 TB10 TB100 TBGPU memory beyond the weights, log scaleone H100: 80 GBMulti-head, no FlashAttentionLlama 1 65B, 2023KV cachescore table130.5 TBMulti-head + FlashAttentionsame model, cache only2.5 TBGrouped-query attentionLlama 2 70B, 2023320 GBMulti-head latent attentionDeepSeek-V3, 202468.6 GB8-bit cache + indexerDeepSeek-V3.2, 202546.9 GBFewer entries than tokensDeepSeek-V4-Flash, 20263.4 GBShared caches + 4-bitDeepSeek-V4.1-Flash, 2026890 MB

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

layer 1layer 3layer 80layer 2of 80numbers76 moreTokenswords or pieces of wordseach token becomes a list ofnumbers that moves up throughthe model's 80 layersKV cachea key and a value per token,kept for the conversationkeysvalueskeyvaluekeyvaluekeyvaluekeyvalue64 headseach keeps its own key and value64 keysone per head64 valuesone per head128 numbers eachevery layer keeps its own keysand values, for every token80 layers× 64 heads× a key and a value= 2.5 MB per tokenThecatsaton

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.

What the KV cache holds, in four steps

layer 1layer 3layer 80layer 2of 80numbers76 moreTokenswords or pieces of wordseach token becomes a list ofnumbers that moves up throughthe model's 80 layersKV cachea key and a value per token,kept for the conversationkeysvalueskeyvaluekeyvaluekeyvaluekeyvalue64 headseach keeps its own key and value64 keysone per head64 valuesone per head128 numbers eachevery layer keeps its own keysand values, for every token80 layers× 64 heads× a key and a value= 2.5 MB per tokenThecatsaton

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

1

entry per tokenwhat one token leaves in one layer

×80

layers that cacheevery layer builds its own cache

×16,384

numbers per entry64 keys + 64 values, 128 numbers each

×2

bytes per number16 bits for each number

=2,621,440

bytes per token2.5 MB per token, 2.5 TB for a million tokens

The cache for one token in Llama 1 65B

1

entry per tokenwhat one token leaves in one layer

×80

layers that cacheevery layer builds its own cache

×16,384

numbers per entry64 keys + 64 values, 128 numbers each

×2

bytes per number16 bits for each number

=2,621,440

bytes 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

each row:a tokenlooked ateach column: a token looking backScore tablehow much each token mattersto each token looking backTheThecatcatsatsatonon

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.

The score table, for four tokens

each row:a tokenlooked ateach column: a token looking backScore tablehow much each token mattersto each token looking backTheThecatcatsatsatonon

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

Multi-head attentionLlama 1 65B, 64 KV heads1M tokens:2.5 TBQKVMulti-query attentionsame shape, 1 KV head1M tokens:40 GBQKVGrouped-query attentionLlama 2 70B, 8 KV heads1M tokens:320 GBQKVquery head, not cachedkey head and value head, cached for every token

One layer of 80. Every head holds 128 numbers per token, so the cache grows with the number of red cells.

Which keys and values each query head reads

Multi-head attentionLlama 1 65B, 64 KV heads1M tokens:2.5 TBQKVMulti-query attentionsame shape, 1 KV head1M tokens:40 GBQKVGrouped-query attentionLlama 2 70B, 8 KV heads1M tokens:320 GBQKVquery head, not cachedkey head and value head, cached for every token

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

Grouped-query attentionLlama 2 70B1M tokens:320 GBtoken vector8,192 numberscached2,048 numbers8 keys + 8 values, 128 numbers eachMulti-head latent attentionDeepSeek-V31M tokens:68.6 GBtoken vector7,168 numberscompresscached576 numberslatent 512, position 64up-project128 keys + 128 values40,960 numbersrebuilt from the latent, never cachedcached for every token, drawn to scalenot cached

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.

What one token leaves in the cache, per layer

Grouped-query attentionLlama 2 70B1M tokens:320 GBtoken vector8,192 numberscached2,048 numbers8 keys + 8 values, 128 numbers eachMulti-head latent attentionDeepSeek-V31M tokens:68.6 GBtoken vector7,168 numberscompresscached576 numberslatent 512, position 64up-project128 keys + 128 values40,960 numbersrebuilt from the latent, never cachedcached for every token, drawn to scalenot cached

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

One entry per tokenbefore V41M tokens:1,048,576 entriesOne entry per 4 tokensV4, one kind of layer1M tokens:262,144 entriesOne entry per 128 tokensV4, the other kind of layer1M tokens:8,192 entries

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.

What 128 tokens leave in a layer's cache

One entry per tokenbefore V41M tokens:1,048,576 entriesOne entry per 4 tokensV4, one kind of layer1M tokens:262,144 entriesOne entry per 128 tokensV4, the other kind of layer1M tokens:8,192 entries

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

DeepSeek-V3.2, 61 layers61 caches, one entry per token each: 48,068 bytes per tokenDeepSeek-V4-Flash, 43 layers41 caches, 21 at 4:1 and 20 at 128:1: about 3,514 bytes per tokenDeepSeek-V4.1-Flash, 40 layers4 caches, 3 at 2:1 in the encoder, 1 at 1:1 for the decoder: 890 bytes per tokenencoder, layers 1 to 20decoder, layers 21 to 40builds a cache, with one entry per1 token2 tokens4 tokens128 tokensreads the cache another layer builtkeeps only the last 128 tokens

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.

Which layers keep a cache of their own

DeepSeek-V3.2, 61 layers61 caches, one entry per token each: 48,068 bytes per tokenDeepSeek-V4-Flash, 43 layers41 caches, 21 at 4:1 and 20 at 128:1: about 3,514 bytes per tokenDeepSeek-V4.1-Flash, 40 layers4 caches, 3 at 2:1 in the encoder, 1 at 1:1 for the decoder: 890 bytes per tokenencoder, layers 1 to 20decoder, layers 21 to 40builds a cache, with one entry per1 token2 tokens4 tokens128 tokensreads the cache another layer builtkeeps only the last 128 tokens

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.

ModelEntries per tokenLayers that cacheNumbers per entryBytes per number1M tokens
Llama 1 65B, 202318016,38422.5 TB
Llama 2 70B, 20231802,0482320 GB
DeepSeek-V3, 2024161576268.6 GB
DeepSeek-V3.2, 2025161576about 146.9 GB
DeepSeek-V4-Flash, 20261/4 and 1/12841512about 13.4 GB
DeepSeek-V4.1-Flash, 20261/2 and 14512about half890 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.

Ship on evidence, not vibes.

See how your agent fails. Hand the fix to your coding agent.