{"slug": "how-the-memory-for-a-million-tokens-shrank-150000-times", "title": "How the memory for a million tokens shrank 150,000 times", "summary": "DeepSeek-V4.1-Flash, released in September 2026, cut the GPU memory needed to hold a million-token context from more than 130 TB in 2022 to 890 MB, a 150,000-fold reduction, according to an analysis of the model's arXiv paper. The 2022 figure comprised 128 TB of attention score tables plus 2.5 TB of key-value cache, equivalent to roughly 1,700 H100 GPUs, while the 890 MB footprint fits on virtually any GPU from the past decade. The reduction rests on techniques including FlashAttention, introduced by Tri Dao and colleagues in 2022, which eliminated the full score table and brought the requirement to 2.5 TB, with further gains from shrinking the four multiplied factors that determine per-token cache size.", "body_md": "# How the memory for a million tokens shrank 150,000 times\n\nThe 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.\n\nIn 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.\n\n[DeepSeek-V4.1-Flash](https://arxiv.org/abs/2609.19969), released in September\n2026, handles the same million tokens in 890 MB, 150,000 times less. That fits on\nvirtually any GPU from the past decade. It's a big part of why you can rent a\nmillion-token context window today, and why providers can keep a conversation's\nmemory around and charge less when you send the same text again.\n\nThis 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.\n\n## Where the memory goes\n\nFirst, we need to understand where all of this memory goes.\n\nAn 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.\n\nThe part of a layer that uses memory is called *attention*, which uses three\nkey pieces of information: *keys*, *values* and *queries*. In this step tokens\nexchange information, which lets the model work out what a word means given\nthe words before it. Without attention, \"bank\" would mean the same thing in\n\"river bank\" and in \"bank account\".\n\nWhen a token looks back at the earlier ones, it brings a *query* and compares\nit with each earlier token's *key*. Each comparison gives a *score*, which says\nhow much that earlier token matters to this one. That score is then multiplied\nby the *values* of the earlier tokens. In \"river bank\", \"bank\" gives \"river\"\na high score and takes in most of its value.\n\nEach layer does this many times at once. A layer runs several attentions\nside by side, called *heads*. Each head has its own weights, so it makes its\nown queries, keys and values and learns to look for something different.\n\nOur starting model is Meta's [Llama 1 65B](https://arxiv.org/abs/2302.13971),\nfrom February 2023. It has 80 layers and 64 heads per layer, and a key or a\nvalue is 128 numbers of 2 bytes each. We'll call what one token leaves in one\nlayer its *entry*. Here that's a key and a value in each of 64 heads, or\n2 × 64 × 128 = 16,384 numbers. The cache for one token is four numbers\nmultiplied together:\n\nThat'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.\n\n## The score table: 130.5 TB\n\nUntil 2022, the cache wasn't even the biggest problem. The scores were. There's\none score for every pair of tokens, and when a model read a prompt, standard\ncode computed them for all the tokens at once and held the whole table in\nmemory.\n[Meta's own code for Llama 1](https://github.com/facebookresearch/llama/blob/llama_v1/llama/model.py#L141-L145)\nstill works that way.\n\nA 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.\n\n**1M tokens: 130.5 TB.** That's 128 TB of scores plus 2.5 TB of cache, about\n1,700 H100s.\n\n## FlashAttention, the free step: 2.5 TB\n\n[FlashAttention](https://arxiv.org/abs/2205.14135), by Tri Dao and colleagues in\n2022, got rid of the score table. It splits the queries, keys and values into\ntiles small enough for the GPU's fast on-chip memory, and computes the scores\none tile at a time. The scores are made and used on the chip and never written\nto the GPU's main memory, so they take the space of one tile instead of\nterabytes. It's also faster, because it skips all that memory traffic.\n\nWhat 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.\n\n**1M tokens: 2.5 TB.** 52 times less, and still 32 H100s of memory for one\nconversation.\n\nThat 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.\n\n## Grouped-query attention: 320 GB\n\n*Numbers per entry: 16,384 to 2,048.*\n\nThe obvious waste is that all 64 heads store keys and values of their own. In\n2019 Noam Shazeer proposed\n[multi-query attention](https://arxiv.org/abs/1911.02150) (MQA): keep the 64\nquery heads, which aren't cached anyway, but make them all share one key and\none value. The entry shrinks 64 times and the cache drops to 40 GB. Google's\nPaLM shipped it. The catch was quality. If every head has to read the same key\nand value, the heads can't each look for something different.\n\n[Grouped-query attention](https://arxiv.org/abs/2305.13245) (GQA), from Google\nin 2023, took the middle road. The query heads are split into groups, and each\ngroup shares one key and one value.\n[Llama 2 70B](https://arxiv.org/abs/2307.09288), Llama 1 65B's successor from\nJuly 2023, has the same 80 layers and 64 query heads, in 8 groups. Its entry is\n8 keys and 8 values, or 2,048 numbers.\n\n**1M tokens: 320 GB.** Eight times smaller, and still four H100s.\n\n## Multi-head latent attention: 68.6 GB\n\n*Numbers per entry: 2,048 to 576.*\n\nGrouped-query attention shrinks the entry by making heads share. DeepSeek's\n[V2 paper](https://arxiv.org/abs/2405.04434) in 2024 found a way to shrink it\nfurther while giving every head keys and values of its own. It doesn't store\nthem at all.\n\nThat 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.\n\nMulti-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.\n\nEach 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.\n\n[DeepSeek-V3](https://arxiv.org/abs/2412.19437), from December 2024, has 61\nlayers and 128 heads. Its entry is 512 + 64 = 576 numbers.\n\n**1M tokens: 68.6 GB.** It fits in one H100, with 11 GB to spare.\n\n## KV cache quantization: 46.9 GB\n\n*Bytes per number: 2 to about 1.*\n\nEvery 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.\n\nStoring a number in fewer bits means we must lose precision. At 8 bits per number,\nthere are only 256 possible values. At 4 bits per number, only 16.\nThis is called *quantization*. [DeepSeek-V3.2](https://arxiv.org/abs/2512.02556),\nfrom late 2025, stores its latents in 8 bits. An entry drops from 1,152 bytes to 656,\nwhich on its own would bring the cache down to 39 GB.\n\n### DeepSeek Sparse Attention\n\nDeepSeek'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.\n\nDeepSeek-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.\n\nIn order to support this, V3.2 adds an *indexer*. The indexer\nneeds a small key of its own for every token, which puts 8 GB back.\n\n**1M tokens: 46.9 GB.**\n\n## Fewer entries than tokens: 3.4 GB\n\n*Entries per token: 1 to 1/4 and 1/128.*\n\nEvery cache so far got smaller by shrinking the entry. The number of entries\nnever changed. It was one per token, in every layer.\n[DeepSeek-V4](https://arxiv.org/abs/2606.19348), released in April 2026 with a\n1M-token context, keeps fewer entries than tokens.\n\nIt 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.\n\nIt'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.\n\nV4 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.\n\nMerging 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.\n\nV4 also drops MLA's expansion step. An entry is one vector of 512 numbers that\nall 64 heads read directly, as both key and value. That's\n[multi-query attention's](#grouped-query-attention-320-gb) single\nshared head from 2019, back with a vector four times as wide.\n\n**1M tokens: 3.4 GB** for V4-Flash, the smaller V4 model. That's almost 14\ntimes smaller than V3.2.\n\n## Fewer layers that cache: 890 MB\n\n*Layers that cache: 41 to 4. Bytes per number: about 1 to about half. Entries\nper token: up again, to 1/2 and 1.*\n\nThe last of the four numbers is the number of layers that cache. In every\nmodel so far, each layer builds a cache of its own, and V4-Flash has 41 of\nthem. In [DeepSeek-V4.1-Flash](https://arxiv.org/abs/2609.19969), from\nSeptember 2026, only four do.\n\nThe 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.\n\nSharing 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.\n\nV4.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.\n\n**1M tokens: 890 MB.** That's about 1% of an H100, and almost 4 times smaller\nthan V4-Flash, with less merging.\n\n## Four numbers, each one cut\n\nHere are the four numbers at every step. The number each step cut is in bold.\n\n| Model | Entries per token | Layers that cache | Numbers per entry | Bytes per number | 1M tokens | \n|---|---|---|---|---|---|\n| Llama 1 65B, 2023 | 1 | 80 | 16,384 | 2 | 2.5 TB | \n| Llama 2 70B, 2023 | 1 | 80 | **2,048** | 2 | 320 GB | \n| DeepSeek-V3, 2024 | 1 | 61 | **576** | 2 | 68.6 GB | \n| DeepSeek-V3.2, 2025 | 1 | 61 | 576 | **about 1** | 46.9 GB | \n| DeepSeek-V4-Flash, 2026 | **1/4 and 1/128** | 41 | 512 | about 1 | 3.4 GB | \n| DeepSeek-V4.1-Flash, 2026 | 1/2 and 1 | **4** | 512 | **about half** | 890 MB | \n\nFrom V3.2 on, the totals also include the indexer's small key for each entry.\n\nThe 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.\n\nIn this post, we have followed Llama 2 and then DeepSeek. Other labs have included\nthese same techniques in their models: GLM-5, Kimi\nK2 and Mistral Large 4 all use MLA. Others took different turns. Google's Gemma\nmodels interleave sliding-window layers with a few full ones, and Qwen3-Next\nand Kimi Linear replace most attention layers with linear attention, whose\nmemory doesn't grow with the conversation at all. We covered those hybrids in\n[an earlier post](https://three.dev/blog/your-business-can-fit-in-a-smaller-model).\n\n## Recap: 130 TB to 890 MB\n\nIn 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.\n\nThe 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.\n\nEvery 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.\n\nThe 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 an SSD or even streamed through an intra-datacenter network.", "url": "https://wpnews.pro/news/how-the-memory-for-a-million-tokens-shrank-150000-times", "canonical_source": "https://three.dev/blog/how-the-memory-for-a-million-tokens-shrank", "published_at": "2026-11-24 00:00:00+00:00", "updated_at": "2026-12-03 09:43:34.717589+00:00", "lang": "en", "topics": ["large-language-models", "artificial-intelligence", "ai-research", "ai-infrastructure"], "entities": ["DeepSeek", "DeepSeek-V4.1-Flash", "Meta", "Llama 1 65B", "Tri Dao", "FlashAttention", "H100"], "also_reported_by": [], "takeaway": "FlashAttention removed the full score table, cutting a million-token context from 130.5 TB to 2.5 TB, and DeepSeek-V4.1-Flash's 890 MB result comes from shrinking the four multiplied factors — layers, heads, head dimension and bytes per value — that set per-token cache size.", "recommendation": {"advice": "Eliminate the full attention score table with tiled FlashAttention-style computation, then shrink the four factors behind per-token KV cache size to fit long contexts on ordinary GPUs.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/how-the-memory-for-a-million-tokens-shrank-150000-times", "markdown": "https://wpnews.pro/news/how-the-memory-for-a-million-tokens-shrank-150000-times.md", "text": "https://wpnews.pro/news/how-the-memory-for-a-million-tokens-shrank-150000-times.txt", "jsonld": "https://wpnews.pro/news/how-the-memory-for-a-million-tokens-shrank-150000-times.jsonld"}}