Gradient LabSign up free

Trending › Model launch

How DeepSeek-V4.1-Flash shrank its KV cache

DeepSeek-V4.1-Flash stores about 890 bytes of cache per token of context, roughly 4× less than V4-Flash. Three tricks: share the cache across layers, keep only the entries that matter, and store them in 4-bit numbers.

Watch it move. This is an animated lesson on Gradient Lab.

Start the animated lesson

Step by step

Concept

Every token leaves notes behind

When a transformer reads text, each layer turns every token into a key and a value (the things attention looks up). To write the next token, it needs the keys and values of all earlier tokens. Saving them instead of recomputing them is the KV cache.

Front row: the tokens so far (yellow is the newest). Purple stacks: the keys and values each layer saved for each token.

Problem

The cache grows with every token

The cache grows with the number of tokens times the number of layers. Agents re-read long histories, tool outputs and files, so contexts of hundreds of thousands of tokens are normal. At that length the cache, not the model weights, is what fills the GPU memory.

Same model, twice the text: twice the cache. A 1M-token context multiplies it by a million tokens.

Key idea

Trick 1: most layers borrow the cache

DeepSeek-V4.1-Flash splits the model into an encoder half and a decoder half. According to DeepSeek, only 4 of its 40 layers compute a full cache; the rest borrow one computed by an earlier layer. The idea builds on YOCO (“You Only Cache Once”) from 2024.

Grey slivers are layers that borrow instead of storing their own copy.

Build

Trick 2: read only what matters

Each layer doesn’t read the whole cache. It picks the 512 most relevant entries for the current token, plus an uncompressed window of the last 128 tokens. Reading less data per token is what makes long contexts cheap to serve.

Bright stacks: the few entries picked for this token. The newest tokens are always kept.

Build

Trick 3: store numbers in 4 bits

The cached numbers are stored in 4 bits instead of 8, with every group of 16 numbers sharing one 8-bit scale so they can be stretched back to the right size. DeepSeek fine-tuned the model to cope with the rounding. That alone roughly halves this part of the cache.

Same blocks, half the height: same information, stored more coarsely.

Insight

The result: about 890 bytes per token

Reported numbers: the full-input cache takes about 890 bytes per token, about 4× smaller than DeepSeek-V4-Flash and 437× smaller than the first DeepSeek model. DeepSeek also cut its price for cached input by 57%. Smaller caches mean more conversations per GPU and cheaper long contexts.

Figures are DeepSeek’s own, as reported by The Batch and The Decoder. Independent tests may differ.

Quick check: Why does the KV cache matter so much for long contexts?

Pseudocode

# without a cache: re-read the whole text for every new token
for each new token:
    for every layer:
        keys, values = compute for ALL earlier tokens   # slow

# with a KV cache: compute once, keep, reuse
cache[layer] += keys and values of the new token        # memory grows

# DeepSeek-V4.1-Flash keeps the cache small
full cache in only 4 of 40 layers; others borrow it
each layer reads the 512 most relevant entries + last 128 tokens
store cached numbers in 4 bits (16 share one scale)
# about 890 bytes per token of context

Go deeper

Sources

  1. The Batch (DeepLearning.AI): DeepSeek’s Flash leapfrogs Pro again
  2. The Decoder: DeepSeek V4.1-Flash cuts memory needs for AI agents
  3. DeepSeek-V4.1-Flash model page and technical report (Hugging Face)
  4. Sun et al. (2024), You Only Cache Once (YOCO)