AIAny. ← Back to AIAny

Lesson notes · One preview report · one bottleneck broken

DeepSeek-V4

By the end of this page you'll understand — with real numbers — how DeepSeek-V4 makes a one-million-token context routine: compressing the attention cache before attention ever reads it, so cost stops exploding as the context grows.

Subject Efficient long-context LLMs Date Preview · 2026 Source DeepSeek-V4 technical report Lab DeepSeek-AI Headline 1M tokens · 27% FLOPs · 10% KV cache vs V3.2

DeepSeek-AI · [email protected] — DeepSeek-V4 series preview (DeepSeek-V4-Pro · 1.6T params, 49B active · DeepSeek-V4-Flash · 284B params, 13B active).

The core idea

The wall on long context was never quality. It was the cost of attention.

A modern Transformer's attention (the step where each token looks back at every earlier token) costs work that grows with the square of the context length, and it must keep a KV cache (the stored key/value vectors of every past token) that grows linearly. Push to a million tokens and both blow up — so ultra-long context stayed a stunt, not a daily mode.

DeepSeek-V4 attacks the cost side head-on. Before attention ever looks at the past, it compresses the KV cache along the sequence — squashing many tokens into one cache entry — using two complementary schemes (CSA and HCA). At a one-million-token context, V4-Pro then spends only 27% of the per-token FLOPs and 10% of the KV cache of its predecessor V3.2, while still matching or beating it on quality.

Don't make attention cheaper by attending to less detail. Shrink what attention has to hold, before it ever looks.

PART A

The wall

Why a million-token context was a stunt, not a setting.

Step 1 · The black box & the prize

A model you can feed a million tokens — and it answers cheaply.

Here's the whole story in one picture. You hand the model a giant input — say an entire codebase plus a stack of design docs, about a million tokens (the word-pieces a model reads) — and ask a question. A label, a patch, an answer comes out. The box is DeepSeek-V4, a Mixture-of-Experts (MoE) model: only a small slice of its parameters fires per token. This box, and this million-token prompt, follow us through all ten steps.

prompt in ≈ 1,000,000 tokens DeepSeek-V4 MoE · native 1M context attention · experts · output the answer cheap per token 27% FLOPs 10% KV cache
Our running example: a ~1M-token prompt into DeepSeek-V4. The headline numbers (27% FLOPs · 10% cache vs V3.2) are measured at that 1M-token length.

The series ships in two sizes. V4-Pro is 1.6T total parameters with 49B active per token; V4-Flash is 284B total / 13B active. Both are pre-trained on more than 32T tokens (Pro on 33T) and natively support a 1M-token context. At that length, even the larger V4-Pro costs only a sliver of V3.2 — and V4-Flash drops further, to 10% of the FLOPs and 7% of the cache.

1M
token context — native, supported as a routine operating mode.
27%
of V3.2's per-token FLOPs for V4-Pro at 1M tokens.
10%
of V3.2's KV cache for V4-Pro at 1M tokens.
1.6T
params, 49B active (V4-Pro) — MoE keeps the active slice small.

From the abstract: “In the one-million-token context setting, DeepSeek-V4-Pro requires only 27% of single-token inference FLOPs and 10% of KV cache compared with DeepSeek-V3.2.”

Step 2 · Why long context is so expensive

Each new token must look at — and store — every token before it.

You've seen the box win on cost. Before we open it, sit with the problem it solves: where does the bill for long context actually come from?

In standard attention, generating one new token requires comparing its query against the key of every earlier token. Read 1,000 tokens, that's ~1,000 comparisons per new token; read a million, that's a million. The compute per generated token grows linearly with the context already seen — and over a whole sequence that's the famous quadratic cost (work proportional to length squared).

There's a second, sneakier bill: the KV cache. To avoid recomputing the past on every step, the model stores each earlier token's key and value vectors. That store grows one entry per token — so at a million tokens it can swell to tens of gigabytes of fast memory per request, which is what really caps how long a context you can afford to serve.

Comparisons for the newest token short context 3 to look at long context …a million to look at cost per token grows with the length already read KV cache (grows one entry per token) short → small long → tens of GB of fast memory per request
Two bills grow with context: the comparisons each new token must do, and the cache holding every past token. Both are what DeepSeek-V4 shrinks.
Key tension

You can't just attend to fewer tokens — the model would go blind to most of its own context. The trick has to keep the information reachable while shrinking what attention physically holds and scans.

PART B

Two compressions

Squash the cache first — then attend. This is the heart of V4.

Step 3 · The one move

Merge a run of tokens into a single cache entry — before attention looks.

We just saw both bills are driven by the number of cache entries. So the move is simple to state: have fewer entries.

Instead of keeping one KV entry per token, V4 takes every m consecutive tokens and compresses them into one summary entry, using a small learned, softmax-weighted blend of those tokens. A sequence of length n becomes roughly n / m cache entries. Attention then runs over the compressed entries, not the raw tokens — so both the comparisons and the stored cache shrink by the same factor m.

raw KV entries · one per token merge every m compressed entries · one per group of m summary 1 summary 2 fewer entries = fewer comparisons + smaller cache both shrink by the factor m
The single move underneath everything in V4: compress runs of tokens into summary entries, then attend to the summaries.

That's the whole idea. The next two steps are just the two recipes V4 uses to do this compression — one gentle and selective (CSA), one aggressive but complete (HCA) — and why it interleaves both.

Step 4 · CSA — compress, then select

Compress gently (m = 4), then attend to only the top-k summaries that matter.

Step 3 gave us summary entries. Compressed Sparse Attention (CSA) uses a light compression and then adds a second saving on top: sparsity.

CSA first compresses gently — every m = 4 tokens into one summary (so 1M tokens → 250K summaries). Then a tiny, fast scorer called the lightning indexer ranks those summaries for the current query and a top-k selector keeps only the best k (V4-Pro uses k = 1024; Flash uses 512). Core attention runs over just those k — the rest are skipped entirely. A small sliding window of the most recent raw tokens is added back so nearby detail isn't lost.

Try it · CSA cost

Drag the context length and watch how few entries attention actually scans.

1,000,000
1,000,000
raw tokens (what plain attention scans)
250,000
compressed summaries (÷ m = 4)
1,024
top-k summaries actually attended (k = 1024)

What to notice: as the context grows, the raw count explodes and the compressed count grows with it — but the number attention scans stops at k. Beyond ~4K tokens, CSA's cost is essentially flat. Counts are illustrative; k and m are V4-Pro's real settings.

Why this is bounded

Compression alone shrinks cost by the factor m. Top-k selection caps the comparisons at a fixed k no matter how long the context gets — so attention work stops growing once the context is long enough.

Step 5 · HCA — compress hard, keep dense

Crush every 128 tokens into one — then look at all of them.

CSA compressed lightly and skipped most entries. Heavily Compressed Attention (HCA) takes the opposite trade: compress brutally, but attend to everything that's left.

HCA consolidates every m′ = 128 tokens into a single summary entry — 32× harder than CSA's m = 4. After that crush, so few entries remain that V4 can just run dense attention over all of them, with no top-k selector at all. The win comes entirely from the heavy compression; the simplicity (no sparse selection) keeps it cheap and stable. Like CSA, it keeps a small sliding window of recent raw tokens for local detail.

CSA · m = 4, then keep top-k many small summaries; attend to a chosen few HCA · m′ = 128, keep all (dense) few big summaries; attend to all of them Same idea, opposite dials: CSA = light + selective · HCA = heavy + complete
Two complementary recipes. Blue = CSA summaries (m = 4); amber = HCA summaries (m′ = 128). Grey = entries CSA's top-k skips. Both also add a sliding window of recent raw tokens (not shown).
CSA summary · gentle (m = 4) HCA summary · heavy (m′ = 128) attended skipped by top-k

Step 6 · The hybrid

Interleave CSA and HCA layers — the model gets both kinds of memory.

Now we have two attention recipes. V4 doesn't choose one — it alternates them layer by layer.

After the first couple of layers (pure sliding-window attention to lock in local structure), V4 stacks CSA and HCA layers in an interleaved pattern. CSA layers give fine, selectable detail; HCA layers give a cheap, complete bird's-eye view of the whole context. Together with the per-token RoPE (rotary position encoding, telling tokens how far apart they are) and an attention sink (a learned slot that lets a head choose to attend to almost nothing), the hybrid keeps attention cost bounded as the context grows.

token stream attention · HCA feed-forward · MoE experts attention · CSA feed-forward · MoE experts residual path (mHC, step 7) …repeat for 43–61 layers
One slice of the stack: attention layers alternate CSA and HCA; each block also has an MoE feed-forward and a residual path. V4-Flash is 43 layers, V4-Pro 61.
Headline result

Against a common BF16 GQA8 baseline (head dimension 128), the hybrid cuts the KV cache to about 2% of baseline at a 1M-token context — and even versus the already-efficient V3.2, to 10%.

Step 7 · mHC — keeping the signal stable

Constrain the residual mix so a 61-layer stack never blows up.

With attention efficient, the second upgrade is on the residual path you saw threading the stack — the lane that carries each block's input straight across.

Hyper-Connections (HC) widen that residual lane into several parallel streams and learn how to mix them. It helps — but stacking many such layers tends to blow up numerically. V4's fix, Manifold-Constrained Hyper-Connections (mHC), forces the mixing matrix B to be doubly stochastic (every row and column sums to 1, all entries ≥ 0), projected there by the Sinkhorn–Knopp algorithm. That guarantees its spectral norm (the most it can stretch any vector) is ≤ 1, so the residual transform is non-expansive: it can never amplify the signal, only redistribute it. Multiply many such matrices and the result stays well-behaved — stable to any depth.

mixing matrix B · rows & columns each sum to 1 .5 .3 .2 .2 .5 .3 .3 .2 .5 → 1.0 → 1.0 → 1.0 spectral norm ≤ 1 the mix can never stretch a vector, only redistribute it → stable to any depth (closed under multiplication)
A doubly stochastic matrix: each row and each column sums to 1. This bounds its spectral norm at 1, so deep stacks of mHC layers stay numerically stable.

Step 8 · Training it cheaply

A faster optimizer (Muon) and 4-bit expert weights (FP4).

Architecture done. Two training-side choices let a 1.6T-parameter model train and serve at this scale.

First, the Muon optimizer replaces most of Adam. Where Adam rescales each weight individually, Muon orthogonalizes the whole update matrix (via fast Newton–Schulz iterations) so all directions get a balanced step — giving faster convergence and steadier training. (Embeddings, norms and a few other modules still use AdamW.)

Second, the routed MoE expert weights — the bulk of the parameters — are trained and stored in FP4 (4-bit floating point) via quantization-aware training, slashing memory and, on future hardware, compute. The training itself was not painless: trillion-parameter MoE runs hit loss spikes traced to routing outliers, which the team tamed with two tricks (Anticipatory Routing and SwiGLU Clamping) that they note work empirically but aren't yet fully understood.

Honest footnote

The authors are upfront: to minimize risk they kept many preliminarily-validated components, leaving the architecture “relatively complex” — and flag that some stabilizing tricks are effective but not yet principled. A preview report, not a final word.

PART C

The payoff

Does the cheap long context actually hold up? The numbers.

Step 9 · The efficiency curves

As context grows, V4's cost barely moves — V3.2's keeps climbing.

We've built the machine. Now compare its bill to V3.2's across context length — this is the chart from the paper's Figure 1, made draggable.

At short contexts the savings are modest; the further you push, the wider the gap. At a million tokens V4-Pro lands at 27% of V3.2's per-token FLOPs and 10% of its KV cache; V4-Flash, with fewer active parameters, reaches 10% of FLOPs and 7% of cache.

Try it · cost vs V3.2

Drag the context length to see the relative cost of V4 against V3.2.

1M
100%
V3.2 baseline (FLOPs & cache)
27%
V4-Pro per-token FLOPs vs V3.2
10%
V4-Pro KV cache vs V3.2

What to notice: the advantage widens with length — the mechanisms from Part B only pay off once the context is long. Percentages are the paper's reported endpoints at each length (V4-Pro vs V3.2); intermediate points are interpolated for the demo.

V4-Pro-Max

And it doesn't trade away quality — selected results

SimpleQA-Verified
world knowledge · Pass@1
57.9
LiveCodeBench
coding · Pass@1 (CoT)
93.5
GPQA Diamond
science reasoning · Pass@1
90.1
MRCR 1M
1M-token retrieval · MMR
83.5
Terminal Bench 2.0
agentic coding · Acc
67.9
0100
Agent Internally, V4-Pro-Max beats Claude Sonnet 4.5 and approaches Opus 4.5 on agentic coding.
Long context On academic 1M-token benchmarks (MRCR, CorpusQA), V4-Pro surpasses Gemini-3.1-Pro.
Coding On Codeforces, V4-Pro-Max would rank 23rd among human competitors.
The gap On world knowledge it still trails frontier closed models by ~3–6 months.

Scores are V4-Pro-Max (maximum reasoning-effort mode) from the report's evaluation tables. “Max” mode spends more reasoning tokens than the High / Non-Think modes.

Step 10 · How it was taught

Train domain specialists, then fold them into one model by imitation.

Efficiency and architecture explain the cost; one last piece explains the skills — V4's two-stage post-training.

First, specialist training: for each domain (math, code, agent, instruction-following) a separate expert is built — supervised fine-tuning to lay foundations, then reinforcement learning with GRPO (a reward-driven policy-optimization method) to sharpen it. Second, on-policy distillation: one unified model acts as a student, generating its own answers and learning to match the specialist teachers on them — folding many narrow experts into a single generalist. Notably, this replaced V3.2's mixed-RL stage entirely.

stage 1 · specialist teachers (SFT + RL) math code agent instruction on-policy distillation unified student one generalist model DeepSeek-V4 three reasoning-effort modes: Non-Think / High / Max
Two-stage post-training: independent domain specialists (stage 1) are consolidated into one student via on-policy distillation (stage 2). The student learns by imitating teachers on its own generated outputs.
What is the single core trick of DeepSeek-V4?

Compress the KV cache along the sequence — merge runs of tokens into summary entries — before attention reads it. Fewer entries means both fewer comparisons and a smaller cache, so cost stops exploding with context length.

How do CSA and HCA differ?

CSA compresses gently (every m = 4 tokens → 1) then keeps only the top-k summaries (sparse). HCA compresses hard (every m′ = 128 → 1) but attends to all of them (dense). V4 interleaves both across layers.

Why does compression help more at long context?

The savings scale with how much there is to compress. At short lengths there's little to squash; at a million tokens, CSA's top-k caps the work at a fixed k and HCA's 128× crush shrinks the cache dramatically — so the gap to V3.2 widens with length.

What does mHC fix?

Plain Hyper-Connections become numerically unstable when stacked deep. mHC constrains the residual mixing matrix to be doubly stochastic, bounding its spectral norm at ≤ 1, so the residual transform is non-expansive and stable to any depth.

Is it strictly better than V3.2?

On efficiency, dramatically — 27%/10% of FLOPs/cache at 1M tokens. On quality, V4-Pro-Base beats V3.2-Base across most benchmarks. But it still trails frontier closed models on world knowledge by ~3–6 months, and the architecture is deliberately complex.

What is on-policy distillation here?

A single student model generates its own outputs and learns to match the domain-specialist teachers on those outputs (minimizing reverse-KL). It consolidates several narrow experts into one generalist, replacing V3.2's mixed-RL stage.

What it unlocks — a million-token context as a routine setting opens the door to longer test-time reasoning, large cross-document analysis, and long-horizon agentic workflows that previously couldn't fit, or couldn't be afforded, in a single context.

“This enables us to routinely support one-million-token contexts, thereby making long-horizon tasks and further test-time scaling more feasible.” — the abstract, DeepSeek-V4 technical report
The profound impact

Making the long context cheap, not just possible.

Plenty of models claimed a large context window. DeepSeek-V4's bet is different: not that a million tokens is possible, but that it can be routine — cheap enough to use every day. If that holds, the ceiling on what a model can reason over moves.

lineage · sparse attention

From MLA & DSA to compress-first

V4 extends DeepSeek's own line of attention efficiency — latent attention, then sparse attention — into compressing the cache along the sequence before attending. The pattern: shrink what attention holds, not what it can see.

now · test-time scaling

Longer thinking, longer horizons

Cheap long context is the substrate for reasoning models that think for tens of thousands of tokens and agents that act over long horizons — both gated, until now, by attention's cost.

open · the frontier gap

An open model near the frontier

V4-Pro-Max redefines the open-model state of the art and, internally, rivals closed leaders on agentic and long-context tasks — while staying open-weight, with the architecture published for the community to refine.

Compress the cache before you attend — and a million-token context stops being a stunt.