TL;DR
In a trained autoregressive language model, the first few tokens of a sequence collect a large share of attention in almost every layer, even when they carry no meaning. The cause is the softmax, which forces each query's attention weights to sum to one, so a query with no good match still has to put its weight somewhere. Keeping the key and value states of the first four tokens alongside a recent window lets a model run on streams of millions of tokens without fine-tuning.
Where you'll see it: long chat sessions and streaming decoders that must run past their training context with a fixed memory budget, and any inference server that uses a sliding-window or StreamingLLM-style KV cache.
Intuition
A bin for unwanted attention
Think of attention as a fixed budget of attention weight that every token must spend, one unit per query. If the current token has a strong match, most of the unit goes there. If it has no strong match, the unit still has to go somewhere. A model that cannot say "nothing here" needs a place to dump the leftover weight.
The first token is a convenient place. Under autoregressive training, every later token can see it, so the model can learn to route leftover weight there. The first token's value vector is then close to harmless, and the weight it absorbs does little damage. The first token ends up as a sink: not because it means anything, but because it is always visible and always cheap to use as a dump.
Why a window breaks it
A sliding-window cache keeps only the most recent tokens. It looks like a clean way to bound memory. But once the sequence is longer than the cache, the first token is evicted, and the sink goes with it. The leftover weight of every query now has to land on recent tokens, which were never trained to absorb it. The output distribution shifts, and perplexity jumps by orders of magnitude. The original paper shows this on Llama-2-13B: with a 1024-token window the perplexity on a long book is 5158, and adding back four initial tokens drops it to 5.40.
The fix
Keep a few sink tokens and a window of recent tokens. The sink takes the leftover weight as it did during training, and the window supplies the content. Memory stays fixed at the sink size plus the window size, and the model behaves as if the text were still in view.
- 3 other tokens
- 10 other tokens
The math
Let be the query of token , and let and be the key and value of token . The usual attention output is
where is the head dimension and is the scaled score. The weights are a probability distribution over the visible tokens, so . This normalisation is the root of the phenomenon. The Softmax has no way to output "no attention at all".
A sliding window restricts the sum to the last tokens:
The normalisation now runs over the window only. Any mass the model used to put on evicted tokens is redistributed across the window, and the model never saw that pattern in training.
StreamingLLM keeps a set of sink positions together with the window:
The attention sum runs over instead of the full window. The cache holds entries, a fixed size per layer, so memory does not grow with the stream.
Positions inside the cache
A second detail matters. Rotary position embeddings (see Rotary Position Embedding) encode the distance between query and key. When the cache is a rolling buffer, the original absolute positions are no longer contiguous, so the paper caches each key before its rotation is applied and then reassigns positions within the cache at every decoding step. Distances seen by the model stay within the range it was trained on, which is part of why the method needs no fine-tuning.
Cost
For a head with heads and per-head dimension , each cached token costs values per layer (keys and values). Going from full attention to the StreamingLLM cache changes this from to , which is constant in . Per-token decoding then costs instead of .
Worked examples
Take a query that matches nothing in its context. The sink token scores , and three ordinary tokens score .
- Exponentials: for the sink, and for each of the three others.
- Denominator: .
- Sink share: .
- Each ordinary token gets .
- If the sink score rises to , the sink share becomes .
Interpretation: a sink does not need a large score to take most of the weight, because the other tokens sum to a fixed floor. Training pushes the sink score up until the leftover weight is absorbed there. The ordinary tokens get small weights, and their values barely move the output.
The paper's Table 2 measures perplexity on 400K tokens of the concatenated PG-19 test set. The table below uses its values for a cache of 2048 tokens, comparing window attention with no sink (0 + 2048) against four sink tokens and a window of 2044 (4 + 2044).
| Model | 0 + 2048 (window only) | 4 + 2044 (sink + window) | Ratio |
|---|---|---|---|
| Falcon-7B | 17.90 | 12.12 | 1.48 |
| MPT-7B | 460.29 | 14.99 | 30.7 |
| Pythia-12B | 21.62 | 12.09 | 1.79 |
Working the MPT row: , so the window-only cache is about thirty times more confused. For Falcon the gap is small but real, and for MPT it is dramatic.
Interpretation: the size of the failure depends on the model. Models differ in how strongly they concentrate attention on the first token, so the same cache policy can be a minor or a catastrophic change.
Table 3 of the paper compares small 160M-parameter models pre-trained from scratch: a vanilla one (no designated sink) and one with a learnable sink token at the start of every training sequence. The streaming cache is 1024 tokens in total.
| Cache config | Vanilla | Learnable sink |
|---|---|---|
| 0 + 1024 | 27.87 | 1235 |
| 1 + 1023 | 18.49 | 18.01 |
| 4 + 1020 | 18.05 | 18.02 |
Working from the table: the vanilla model drops from 27.87 to 18.05 as sinks are added, so it needs several initial tokens to be stable. The model pre-trained with a learnable sink is already at 18.01 with one sink token, so the one token is enough.
Interpretation: the sink does not need to be the first real word. It needs to be a fixed token that every sequence starts with during training. Adding an explicit sink at pre-training time concentrates the leftover weight in one place, which is why one token suffices there.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| Window with no sink tokens | Perplexity explodes once the first token is evicted | Keep 4 sink tokens (the paper's default) |
| Task needs a fact from far back in the stream | The fact has been evicted, and no sink brings it back | Retrieval, a full-attention layer, or a summary step |
| Model trained with a sink token from the start | Works with a single sink, and cache is smaller | Use a model whose training included a sink token |
| Larger cache assumed to help | The paper finds perplexity does not consistently fall as the cache grows | Measure the cache size on the task, not by intuition |
| Positions not re-indexed in the cache | Distances outside the trained range, degraded output | Cache keys before rotary embedding and re-apply positions |
The method keeps a model stable, but it is a cache policy, not a memory. Tokens outside the sink and the window are gone. The paper's StreamEval experiment checks answers to questions about recent lines, which the method handles well. It does not claim the model can use information from far outside the window.
History
Window attention is an old idea. The Longformer paper by Beltagy et al. (2020) combined local windowed attention with a few global tokens for long documents, and Mistral 7B later used a rolling buffer cache of recent tokens. Those systems did not analyse why window attention sometimes fails, and the failure was not well understood.
In September 2023, Xiao, Tian, Chen, Han and Lewis posted "Efficient Streaming Language Models with Attention Sinks" to arXiv. They observed that the first tokens of a sequence attract a surprising amount of attention in trained models, named the phenomenon, and showed that keeping four initial tokens restores stability for window attention. They also proposed pre-training with a learnable sink token. The paper ties the observation to the softmax normalisation and reports results for Llama-2, MPT, Falcon and Pythia. In the same period, Darcet et al. reported a similar effect in vision transformers, where a few background patch tokens collect attention and can be fixed by adding dedicated register tokens.
Check yourself
QuestionIn one sentence, what is an attention sink, and why does a model develop one?Show answer
An attention sink is an early token that receives a large share of attention whatever its meaning. A model develops one because the softmax forces each query's weights to sum to one, and the model needs somewhere to put weight when no earlier token matches. Early tokens are visible to every later token under autoregressive training, so they are the natural place for that leftover weight.
QuestionA query has a sink with score and four ordinary tokens with score . What share of attention goes to the sink, to two decimal places?Show answer
The sink contributes , and each ordinary token contributes , so the denominator is . The sink share is , or 40 percent. Each ordinary token gets .
QuestionWhy does a plain sliding-window cache fail even when the recent tokens are the most relevant ones?Show answer
The failure is not about relevance. Training taught the model to send leftover attention to the first tokens. When the window evicts them, the leftover weight lands on recent tokens instead, which were never trained to absorb it, and the attention output shifts away from what the model expects. Perplexity can rise by orders of magnitude even when the recent tokens are the right content to attend to, as the Llama-2-13B example in the paper shows.
QuestionYou are building a support chatbot on a model with an 8K training window. Conversations run for weeks, and customers sometimes ask about an order placed months earlier. What cache design do you use, and what will fail?Show answer
Use a StreamingLLM-style cache: four sink tokens plus a window of recent turns, with keys cached before rotary embedding and positions re-indexed inside the cache. This keeps memory fixed and output stable over long streams. It will fail on the question about the order placed months earlier, because that content has been evicted and the sink does not preserve it. The right fix is retrieval from a store of past orders, which supplies the old fact as new text in the window.
Sources
- Guangxuan Xiao et al., "Efficient Streaming Language Models with Attention Sinks," 2023. arXiv:2309.17453
- Ashish Vaswani et al., "Attention Is All You Need," 2017. arXiv:1706.03762
- Iz Beltagy et al., "Longformer: The Long-Document Transformer," 2020. arXiv:2004.05150
- Timothée Darcet et al., "Vision Transformers Need Registers," 2023. arXiv:2309.16588