← Home

DeepSeek V4 Explained: Long-Context Engineering and Math

A verification-first look at DeepSeek V4 claims, plus the real math behind sparse attention, KV-cache scaling, and long-context training stability.

View as Markdown

I started this as notes on a third-party video about a supposed DeepSeek V4 release. That is not enough evidence for model specifications. As of June 11, 2026, I could not verify an official DeepSeek V4 paper, repository, model card, or announcement supporting the precise figures and benchmark claims below. They must therefore be treated as claims from the linked video, not established facts.

The useful part is the engineering underneath. The video stitches together several real ideas worth understanding on their own. I separate those ideas from the release story, work through the mathematics line by line, and show how I evaluate frontier-model claims before repeating them.

[!NOTE] New to how these models work under the hood? My write-up on how attention works sets up queries, keys, values, and the quadratic cost, which is what most of V4’s design is built to fight. I’ll lean on it here.

How do I verify a DeepSeek V4 claim before citing it?

I run a short checklist before I repeat any frontier-model number:

  1. Find the developer’s official announcement and paper.
  2. Match the exact model name to a repository or model card.
  3. Check whether parameter counts mean total or active parameters.
  4. Separate maximum context support from measured retrieval quality.
  5. Read benchmark methodology, baselines, sampling settings, and contamination discussion.
  6. Treat third-party videos and screenshots as leads, not primary evidence.

Until those checks pass, the correct language is “the source claims,” not “the model uses” or “the paper proves.” The rest of this article follows that rule.

What does the source claim about DeepSeek V4?

Verification status: unconfirmed. The “1.6 trillion parameters,” “1 million tokens,” training-token count, architecture names, and benchmark results in this section are attributed to the third-party source. Do not cite them as official DeepSeek specifications without a primary source.

The source gives two headline numbers. Each is a feature and also a problem:

  • 1.6 trillion parameters (the V4 Pro model). Parameters are the dials inside the model that store what it knows. More dials give more capacity, but the model is also much harder to train without it diverging.
  • 1 million tokens of context. That is roughly 750,000 words, about the entire Harry Potter series in the prompt, with the model still able to retrieve one specific number from one specific page. For agents, it means running for hours on a long task without losing the thread.

Neither is free. A 1.6T-parameter model is prone to training instability, and a 1M-token context threatens to consume all the compute at inference. The source frames the whole design as a list of fixes for those two failure modes. I accept the engineering logic; I do not accept the release story without a primary source.

Why is a 1-million-token context so expensive?

For every new token, attention asks “how does this relate to every token before it?” This is the mechanism from the 2017 Attention Is All You Need paper, and later models build on it.

For a 10-word sentence that is about 10 comparisons, which is negligible. But the cost grows with the square of the sequence length:

$$ \text{comparisons} \approx n^2 $$

Plugging in numbers shows how fast this grows. For $n = 2{,}048$:

$$ n^2 = 2048 \times 2048 = 4{,}194{,}304 $$

For $n = 100{,}000$:

$$ n^2 = 100{,}000 \times 100{,}000 = 10{,}000{,}000{,}000 $$

For $n = 1{,}000{,}000$:

$$ n^2 = 1{,}000{,}000 \times 1{,}000{,}000 = 1{,}000{,}000{,}000{,}000 $$
Context length $n$ Comparisons $n^2$
2,048 ~4.2 million
100,000 ~10 billion
1,000,000 ~1 trillion

I counted the comparisons at four lengths, next to the 2,048 cost. The 4,096 row shows what doubling does.

Table of context length, comparisons and multiple of the 2,048 cost. 2,048 gives 4,194,304. 4,096 gives 16,777,216, which is 4 times. 100,000 gives 10 billion, 2,384 times. 1,000,000 gives 1 trillion, 238,419 times.
Going from 2,048 to 4,096 doubles $n$ and multiplies the work by 4.

Double the context, quadruple the work. At a million tokens even high-end hardware struggles. Compute is not the only cost: the model also has to store the intermediate keys and values for every token it has seen, the KV cache. That grows linearly with context until it exhausts the memory budget.

flowchart LR
    A["New token"] --> B["Compare against<br/>every prior token"]
    B --> C["O(n²) attention cost"]
    B --> D["Store keys + values<br/>→ KV cache grows"]
    C --> E["Compute wall"]
    D --> E

So a bigger context window is not a small engineering step. It is the central bottleneck for long-context models.

How does sparse attention cut the pair count?

Dense causal attention over $n$ tokens forms roughly $n(n+1)/2$ usable query-key pairs — each query can only look at tokens at or before its position.

Start with $n = 1{,}000{,}000$:

$$ n + 1 = 1{,}000{,}001 $$
$$ n(n+1) = 1{,}000{,}000 \times 1{,}000{,}001 = 1{,}000{,}001{,}000{,}000 $$
$$ \frac{n(n+1)}{2} = \frac{1{,}000{,}001{,}000{,}000}{2} = 500{,}000{,}500{,}000 $$

That is about $5 \times 10^{11}$ pairs per head and layer in the dense case.

A sparse design lets each query attend to only a local window of width $w$ plus $r$ retrieved distant tokens. Pair count drops toward $n(w+r)$. Take the source’s illustrative values $w = 128$ and $r = 32$:

$$ w + r = 128 + 32 = 160 $$
$$ n(w+r) = 1{,}000{,}000 \times 160 = 160{,}000{,}000 $$

That is $1.6 \times 10^{8}$ pairs. Compare to the dense count:

$$ \frac{500{,}000{,}500{,}000}{160{,}000{,}000} \approx 3{,}125 $$

That is over 3,000 times fewer pairs on paper. It does not show that any claimed implementation achieves that speedup, because routing, retrieval, memory movement, and kernel utilization add overhead. It does show why sparse patterns are attractive, and why I’d expect long-context designs to use something like them whether or not V4 exists.

I plotted the ratio against the window $w$, for $r = 32$ and $r = 256$. The dashed line is the $w = 128$ case I just worked.

Line chart of dense pairs divided by sparse pairs against window size, for 32 and 256 retrieved tokens. At a window of 128 the ratio is 3,125 with 32 retrieved tokens and 1,302 with 256. Both curves fall as the window grows.
At $w = 128$ the ratio is 3,125 with $r = 32$ and 1,302 with $r = 256$. It assumes $n = 10^6$ and ignores all overhead.

How much memory does a 1M-token KV cache actually need?

The KV cache stores one key vector and one value vector per token, per key/value head, per layer. For $L$ layers, $h_{kv}$ key/value heads, head dimension $d_h$, sequence length $n$, and $b$ bytes per stored value:

$$ M_{KV} = 2 L h_{kv} d_h n b $$

The leading factor $2$ is keys plus values. I’ll walk through a plausible toy config at $n = 1{,}000{,}000$ with FP16 ($b = 2$ bytes):

Symbol Value
$L$ (layers) 32
$h_{kv}$ (KV heads) 8
$d_h$ (head dim) 128
$n$ (sequence length) 1,000,000
$b$ (bytes per value) 2

Step 1 — layers × heads:

$$ L h_{kv} = 32 \times 8 = 256 $$

Step 2 — multiply by head dimension:

$$ L h_{kv} d_h = 256 \times 128 = 32{,}768 $$

Step 3 — multiply by sequence length (one vector per token):

$$ L h_{kv} d_h n = 32{,}768 \times 1{,}000{,}000 = 32{,}768{,}000{,}000 $$

Step 4 — keys + values:

$$ 2 L h_{kv} d_h n = 2 \times 32{,}768{,}000{,}000 = 65{,}536{,}000{,}000 $$

Step 5 — bytes (FP16):

$$ M_{KV} = 65{,}536{,}000{,}000 \times 2 = 131{,}072{,}000{,}000 \text{ bytes} $$

Step 6 — convert to GiB (divide by $1024$ three times):

$$ \frac{131{,}072{,}000{,}000}{1024} = 128{,}000{,}000 \text{ KiB} $$
$$ \frac{128{,}000{,}000}{1024} = 125{,}000 \text{ MiB} $$
$$ \frac{125{,}000}{1024} \approx 122.07 \text{ GiB} $$

That is one sequence for one user, with no activations, weights, or optimizer state, only the cache. Quantization, grouped-query attention, eviction, compression, or distributed storage is therefore essential at million-token scale. The arithmetic holds whatever the marketing slide says.

I varied that toy configuration a few ways. The first row is the 122.07 GiB above.

Table of KV cache size. The worked case with FP16 is 122.07 GiB, 128 KiB per token. INT8 gives 61.04 GiB. FP32 gives 244.14 GiB. 4 KV heads give 61.04 GiB. 500,000 tokens give 61.04 GiB.
Halving $b$, $h_{kv}$ or $n$ halves the cache.

What is the claimed fix: three views of the same document?

The source says DeepSeek does not pick one attention trick. It runs three in parallel, and each covers a different weakness. Think of them as three simultaneous readings of the same text:

  1. Compressed long-range. The distant past gets aggressively squeezed — blocks of old tokens collapsed into compact summaries so the model can still reach far back without paying full price for every token.
  2. Selective retrieval. Compression alone loses precision. Ask about an exact phrase or a specific number buried half a million tokens ago and a one-block summary has thrown it away. So a second pathway selectively pulls back the specific distant tokens that matter, uncompressed.
  3. Sliding-window. The most recent stretch — say the last 128 tokens — is kept word-for-word, zero compression, full fidelity. Recent context is usually the most relevant, so it gets the most exact treatment.
flowchart TB
    DOC["1M-token sequence"] --> C1["Compressed<br/>(distant past → summaries)"]
    DOC --> C2["Selective retrieval<br/>(exact distant tokens)"]
    DOC --> C3["Sliding window<br/>(last ~128 tokens, exact)"]
    C1 --> MIX["Combine per layer"]
    C2 --> MIX
    C3 --> MIX
    MIX --> OUT["Long-range recall +<br/>broad context +<br/>sharp local focus"]

By interleaving these layer by layer, the source claims V4 gets long-range retrieval, broad understanding, and sharp immediate focus at once — while sidestepping the quadratic blow-up. The payoff the video reports: against DeepSeek V3.2 (already one of the most compute-efficient models around), V4 Pro runs at 3.7× lower FLOPs — despite being bigger and having a far longer context. That’s the part I’d want to see in a reproducible benchmark before I repeat it.

How do you keep a trillion-parameter signal from exploding?

Depth is the other problem. Signals pass through hundreds of layers, and at every layer the values can blow up or vanish. The classic fix is residual connections: bypass lanes that let the signal skip around a layer instead of being distorted by it. More recently the field moved to hyperconnections, which widen those lanes to give the signal more room.

The source’s finding is that past a trillion parameters, even standard hyperconnections produce catastrophic spikes. So it introduces manifold-constrained hyperconnections (MHC), which constrain the residual mixing to a manifold of doubly stochastic matrices, where every row and every column sums to 1. Intuitively, the layer can reroute and remix signal across lanes but cannot amplify the total, so the signal cannot grow without bound.

The difficulty is enforcing that constraint at every layer. The source’s answer is the Sinkhorn–Knopp algorithm: before each layer, run alternating row and column normalizations, about 20 iterations, until the matrix is close to doubly stochastic.

flowchart LR
    M["Raw mixing matrix"] --> R["Normalize rows"]
    R --> C["Normalize columns"]
    C --> Q{"Doubly stochastic?<br/>(~20 iters)"}
    Q -- "no" --> R
    Q -- "yes" --> L["Layer processes signal"]

Worked example: one Sinkhorn–Knopp step on a $2 \times 2$ matrix

Start with a positive matrix:

$$ A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} $$

Row sums:

$$ 1 + 2 = 3 $$
$$ 3 + 4 = 7 $$

Row-normalize (divide each entry by its row sum):

$$ A^{(1)} = \begin{bmatrix} \frac{1}{3} & \frac{2}{3} \\ \frac{3}{7} & \frac{4}{7} \end{bmatrix} $$

Column sums of $A^{(1)}$:

$$ \frac{1}{3} + \frac{3}{7} = \frac{7 + 9}{21} = \frac{16}{21} \approx 0.762 $$
$$ \frac{2}{3} + \frac{4}{7} = \frac{14 + 12}{21} = \frac{26}{21} \approx 1.238 $$

Columns do not sum to 1 yet. Column-normalize (divide each column by its column sum):

$$ A^{(2)} = \begin{bmatrix} \frac{1/3}{16/21} & \frac{2/3}{26/21} \\ \frac{3/7}{16/21} & \frac{4/7}{26/21} \end{bmatrix} $$
$$ = \begin{bmatrix} \frac{7}{16} & \frac{14}{26} \\ \frac{9}{16} & \frac{12}{26} \end{bmatrix} $$
$$ \approx \begin{bmatrix} 0.438 & 0.538 \\ 0.563 & 0.462 \end{bmatrix} $$

Row sums are now off again. Alternating row and column normalization converges toward a matrix where both sums are one. Such a constraint can control mixing magnitude, but stability claims still need ablations, training curves, and reproducible code, and I have not verified any of those for V4.

I measured how far the sums are from 1 after each step. Step 2 is the matrix $A^{(2)}$ computed above.

Table of the largest row-sum and column-sum error after each Sinkhorn step. Step 0 has errors 6 and 5. After step 2 the row-sum error is 0.024. By step 8 it is about 2.6 times 10 to the minus 8.
Each step fixes one set of sums and nudges the other, and the error shrinks.

A 20-step loop on every layer of a 1.6T-parameter model looks wasteful, since the rest of the design saves compute. The source claims fused GPU kernels, selective recomputation, and other GPU-level optimizations shrink the entire overhead to 6.7% of runtime. That is plausible in principle and unverified for this specific model.

How do you train a 33T-token run without the math blowing up?

Architecture is only half the story. The source says V4 Pro was trained on 33 trillion tokens, more text than a human could read in many lifetimes.

Dump all of that on a fresh model at once and it drowns, the way handing a beginner an advanced quantum mechanics textbook teaches them nothing. So the source describes a curriculum — start small, grow the context as the model stabilizes:

flowchart LR
    S["4K tokens<br/>(grammar, syntax,<br/>local patterns)"] --> M1["16K"]
    M1 --> M2["64K"]
    M2 --> L["1M tokens<br/>(full long-range)"]

Early on the model only sees ~4,000-token windows — enough to learn local structure — then 16K, 64K, all the way to the full million. Curriculum learning at long context is a real technique; the specific schedule here is a claim.

At this scale the main threat is a loss spike: the numbers blow up and the run crashes. The usual industry fix is to stop, roll back to an earlier checkpoint, and try again, which wastes the compute already spent. The source says DeepSeek leaned instead on self-stabilizing training plus the Muon optimizer (the algorithm deciding how to nudge the dials when the model gets something wrong), squeezing more stable learning out of each step so the run doesn’t need babysitting.

What benchmarks does the source report?

Verification status: unconfirmed. Treat every bullet as “the video claims,” not “I measured.”

  • Putnam 2025 — 120/120. A perfect score on one of the hardest undergraduate math competitions in the world.
  • 1M-token retrieval beats Gemini 3.1 Pro. Pushed to the absolute limit of its context window, V4’s recall accuracy edges out Google’s latest, which also runs a million tokens.
  • Outcompetes Opus 4.6 across the task suite the source reports.
  • On the independent Artificial Analysis leaderboard it lands as the second-best open-source model, just under Kimi 2.6 and crowding the top closed models from Google, Anthropic, and OpenAI.

I’d want the eval scripts, the needle-in-a-haystack prompts, and the contamination discussion before I repeated any of this in a paper or a tweet.

Why is the engineering story worth understanding anyway?

What stayed with me is that there is no single trick. The design combines interleaved sparse attention for the memory wall, manifold-constrained hyperconnections for the signal explosions, Sinkhorn–Knopp made cheap with fused kernels, and a curriculum plus a stable optimizer so a 33T-token run does not fail halfway. Many small engineering decisions stack until something that looked impossible becomes routine. The theme is not one breakthrough but many disciplined ones.

If the source is right about the open-weight release, the team worked under constraints: a smaller team, no top NVIDIA chips, and a fraction of the compute the closed labs use. It then put the weights on Hugging Face and documented infrastructure details that closed labs treat as secrets. In a field that has been closing up, a paper that explains how it was done is rare. I haven’t confirmed that release happened; the engineering patterns are real either way.

Strong intuition means knowing what the equations imply and knowing what the available evidence does not establish. The sparse-attention math says million-token context is a memory and compute problem with known structure. The verification checklist says I still don’t know whether DeepSeek V4 solved it the way the video says.

Watch: The insane engineering of DeepSeek V4 — AI Search. If the attention mechanics felt fuzzy, start with how attention works in transformers.

Support the writing

If this post helped, a coffee keeps the deep dives coming.

Buy me a coffee