Autoregressive large language models are trained on fixed context windows, yet real-world applications (such as continuous coding agents, live conversation servers, and document streaming pipelines) require models to process unbounded token sequences. When standard LLMs operate on sequences longer than their pre-training context length, computational complexity and key-value (KV) cache memory scale quadratically and linearly, respectively.
A seemingly natural workaround is sliding window attention: retaining only the most recent tokens in the KV cache and evicting older keys and values. However, researchers observed that as soon as the very first token in a sequence is dropped from the cache, the model perplexity explodes catastrophically, jumping from single digits to tens of thousands.
The root cause is a phenomenon known as attention sinks. In modern Transformers, the first few tokens, regardless of their semantic content, absorb massive amounts of attention probability mass. Understanding why attention sinks emerge, how StreamingLLM exploits them, and how modern serving systems stabilize infinite streaming without retraining is essential for production LLM architecture.
1. Softmax Normalization and the Emergence of Attention Sinks
The self-attention mechanism computes attention weights by taking the softmax over scaled query-key inner products:
For any given query vector at sequence position , the attention weights across all preceding tokens are normalized such that the sum of attention weights equals 1:
This normalization enforces a strict mathematical constraint: even if a query token requires no relevant contextual information from past tokens (for example, when predicting common syntactic connectors, punctuation marks, or self-contained phrases), the total allocated attention mass must still sum to exactly 1.
Because standard softmax lacks an explicit "no-op" or bias mechanism to absorb superfluous attention weight, the network must assign that residual probability mass somewhere.
Empirical research from MIT, Meta, and CMU demonstrated that the model designates the earliest tokens in the sequence (primarily the beginning-of-sequence <s> / BOS token and the first 2 to 4 tokens) as permanent "sinks." Because these initial tokens appear in every attention window across all sequence positions throughout pre-training, their key projections evolve into universal targets for unneeded attention weights across intermediate and deep transformer layers.
2. Why Naïve Sliding Window Attention Collapses
In high-throughput serving systems, retaining all historical KV pairs for millions of tokens causes out-of-memory (OOM) failures. A standard rolling buffer approach discards the oldest KV pairs once the cache reaches capacity :
[t_0, t_1, t_2, ..., t_{W-1}] --> Cache full
Generate t_W: Drop t_0 --> [t_1, t_2, ..., t_W]
Generate t_{W+1}: Drop t_1 --> [t_2, t_3, ..., t_{W+1}]When is evicted, the attention mechanism is stripped of its primary sink. Without the sink tokens:
- Softmax Score Reallocation: The substantial attention mass (often 30% to 70% of total attention in deeper layers) that previously landed on is suddenly forced onto the remaining tokens in the window.
- Hidden State Distortion: Tokens inside the window receive abnormally high attention scores, altering the weighted sum of value vectors.
- Representational Drift: The modified activations propagate through subsequent Feed-Forward Network (FFN) layers, destabilizing layer normalizations and resulting in corrupted hidden representations.
As documented by Xiao et al. in Efficient Streaming Language Models with Attention Sinks, evicting the first token causes perplexity on models like LLaMA-2, Falcon, and MPT to surge past 10,000 within dozens of tokens, rendering the output entirely incoherent.
3. The StreamingLLM Hybrid Cache Architecture
StreamingLLM resolves this failure mode without fine-tuning or modifying model weights. Instead of a pure sliding window, it constructs a hybrid KV cache that preserves two distinct token groups:
- Attention Sink Tokens (): The first 4 tokens of the sequence () are permanently pinned in the KV cache.
- Rolling Window Tokens (): The most recent tokens are maintained as a dynamic FIFO buffer.

When a new token arrives, the system evicts the oldest token in the rolling window () while leaving the sink tokens untouched.
By retaining just 4 initial tokens, the softmax denominator maintains its baseline distribution, allowing attention heads to safely discard unnecessary weight onto the sinks while drawing active contextual semantics from the rolling window. Benchmarks show that StreamingLLM enables LLaMA-2, Pythia, and MPT models to stream over 4 million consecutive tokens with stable, flat perplexity curves identical to full-context recomputation.
4. Cache Re-Indexing and Rotary Position Embeddings
Retaining sink tokens alongside recent tokens introduces a positional encoding challenge. Modern LLMs utilize relative positional encodings such as Rotary Position Embeddings (RoPE) or ALiBi.
If absolute sequence positions (0, 1, 2, 3 for sinks and 4,000,000 for current tokens) are passed directly into RoPE, the distance between the sink tokens and the current query token exceeds the maximum context length seen during pre-training, causing out-of-distribution positional failures.
StreamingLLM addresses this via positional cache re-indexing:
- When computing attention between query and the cached keys , position IDs are assigned based on their logical position within the cache buffer rather than their original sequence indices.
- Sink tokens are assigned positions 0, 1, 2, 3.
- Window tokens are assigned continuous relative positions 4, 5, through .
Because the relative distance between the query and recent tokens remains within the pre-trained window , RoPE rotation angles stay within valid training bounds, preserving natural language modeling performance.
5. Architectural Solutions: SoftMax1 and Learnable Sinks
While StreamingLLM provides an inference-time solution for existing models, researchers have investigated architectural modifications during pre-training to address the root cause:
SoftMax1 (Zero Sink)
Standard softmax can be modified by adding a constant 1 to the denominator:
This formulation is mathematically equivalent to introducing a virtual token whose Key and Value vectors are all zeros. When no tokens are semantically relevant, the exponent terms shrink, and the attention weights sum to strictly less than 1, naturally allowing attention heads to execute a "no-op" without repurposing initial prompt tokens.
Dedicated Learnable Sink Tokens
Alternatively, models can be pre-trained by prepending a dedicated, learnable sink token to every sequence. By explicitly training a parameter vector to absorb excess attention, the first natural language token (such as the initial user prompt or greeting) is freed from serving as a computational dumping ground, preserving its full semantic fidelity across multi-turn interactions.
6. Practical Engineering Considerations for Inference Systems
Implementing attention sinks and streaming KV caches introduces distinct trade-offs in production:
- Full Attention: Memory complexity scales as with sequence length. Bounded to pre-trained context window. Requires no fine-tuning and preserves full context history.
- Naïve Sliding Window: Memory complexity is constant , but sequence processing collapses due to exponential perplexity spikes after evicting initial tokens.
- StreamingLLM: Memory complexity is constant , where and is the sliding window size. Operates stably on sequences exceeding 4 million tokens without fine-tuning, retaining the recent tokens.
- SoftMax1 / Sink Pre-training: Memory complexity is constant with no separate sink preservation needed, but requires architectural modification during pre-training.
Key Operational Caveats
- Streaming is Not Episodic Memory: StreamingLLM preserves language fluency and syntactic coherence over infinite streams, but it does not enable long-range retrieval of evicted facts. For applications requiring historical recall across long sessions, streaming KV caches must be paired with external retrieval mechanisms (such as vector search, semantic graphs, or hierarchical summarization).
- Integration in Serving Frameworks: High-throughput serving engines like vLLM and SGLang support chunked prefix caching and sliding-window KV management that incorporate sink token pinning, preventing unexpected memory fragmentation and degradation during long-running agent loops.
Sources
- Efficient Streaming Language Models with Attention Sinks (Xiao et al., ICLR 2024 / arXiv:2309.17453)
- RoFormer: Enhanced Transformer with Rotary Position Embedding (Su et al., 2021 / arXiv:2104.09864)
- Softpick: No Attention Sink, No Massive Activations with Rectified Softmax (arXiv:2504.20966)
- StreamingLLM GitHub Repository (MIT HAN Lab)



