Understanding Million-Token Context: Architecture and Training Practices
This article breaks down the core challenges of scaling large language model context windows to one million tokens across three layers: architecture, data, and infrastructure. It explains how to estimate KV Cache memory usage, compares compressed sparse attention with heavy-recompression attention, highlights key design details such as Partial RoPE and attention sinks, outlines a staged long-context training curriculum, and covers the infrastructure strategies needed to serve long-context workloads reliably.
1. Introduction: Why Million-Token Context Is Hard
Pushing the context window from a few thousand tokens to the million-token scale is not as simple as turning up a parameter. It challenges three things at once: memory capacity, computational complexity, and the effective utilization of information. The naive complexity of attention grows quadratically with sequence length, while KV Cache memory grows linearly. At a 1M context, the KV Cache often exceeds the model weights themselves, making it the first constraint that determines whether a single GPU can serve a request.
This article uses publicly available, general industry approaches as a reference and abstracts a reusable methodology: compress and sparsify at the architecture layer, construct long-range dependencies at the data layer, and coordinate memory and parallelism strategies at the infrastructure layer.
2. Do the KV Cache Math First
Standard multi-head attention needs to cache a set of Key/Value vectors for every token. With batch size 1 and a cache precision of 2 bytes, you can estimate it roughly as:
KVCache ≈ L_ctx × N_layers × N_kv_heads × (d_k + d_v) × 2 bytes
Plugging in L_ctx = 1,000,000 makes the scale obvious: with 60 layers, 8 KV heads, and a head dimension of 128, the KV for a single token in a single layer is on the order of 2KB. Multiply that by a million tokens and all layers, and the memory requirement easily reaches the hundreds of GB.
This leads to three practical cost-reduction paths:
- Reduce the number of KV heads (grouped-query or multi-query attention).
- Compress the KV dimension per token (low-rank or heavier recompression).
- Keep full KV only for a local window (sliding window plus a small number of global tokens).
Understanding these three points is the key to understanding the motivation behind the architecture designs that follow.
3. Architecture Layer: From MLA to Compressed Sparse Attention
3.1 Division of Labor Between Two Attention Branches
A typical next-generation long-context design drops the previous generation’s low-rank KV compression scheme and introduces two complementary mechanisms instead. One is a “compressed + sparse” attention that retrieves distant information globally at a lower cost. The other is a “heavy-recompression” attention that compresses history into an extremely low dimension to carry coarse-grained semantics over very long ranges. The former retains some selectivity and precision; the latter targets extreme memory savings. Combined, they make million-token context feasible for a single request.
3.2 Easy-to-Miss but Critical Details
- Local RMSNorm: Insert normalization at specific positions in the attention branch to suppress activation drift under long sequences and improve numerical stability.
- Partial RoPE: Apply rotary position encoding to only a subset of dimensions while leaving the rest with their original semantics, balancing extrapolation ability and expressiveness.
- Sliding window attention: Most layers perform full-precision attention only within a nearby window, delegating global interaction to a few branches.
- Attention Sink: Keep the full KV of a few tokens at the very front of the sequence as an “anchor” for the attention distribution, significantly mitigating softmax entropy collapse in long sequences.
A schematic layer structure can be written as:
# Pseudocode: expresses structural relationships only, not any specific implementationh = partial_rope(q, k, rope_dim_ratio=0.5)local = sliding_window_attention(h, window=4096)glob = compressed_sparse_attention(h, compress_ratio=16, topk=2048)sink = attention_sink(h, sink_tokens=4)out = combine(local, glob, sink)h = h + local_rmsnorm(out)
4. Data Layer: Long-Context Ability Is Fed, Not Declared
Architecture only sets the upper bound on capacity; effective context comes from data construction. Common practices include concatenating documents hierarchically to form long samples, inserting cross-paragraph coreference and retrieval-style question answering into long samples, constructing “needle in a haystack” localization tasks, and maintaining a proportion of short samples to prevent degradation of general capabilities.
The training paradigm typically has three stages: regular pretraining, long-context continued pretraining (progressively expanding the window from 32K to 128K and then to 1M), and finally long-range instruction tuning and preference alignment. The key is that the length curriculum must be gradual, and each stage needs a corresponding evaluation set to validate both retrieval and reasoning capabilities.
5. Infrastructure Layer: Making the Algorithm Actually Run
- Memory and parallelism: For long sequences, split the token dimension with sequence/context parallelism and the head dimension with tensor parallelism; the KV Cache needs paged management and prefix reuse.
- Compute and communication: Sparse branches naturally cause irregular memory access, so block alignment is needed to reduce fragmentation; the heavy-recompression branch relies mainly on small operators, so kernel launch overhead must not dominate.
- Serving: Long-context serving should support chunked prefill, KV reuse, and quantized caching, and can leverage general cloud capabilities such as container platforms, object storage, logging services, and monitoring and alerting for elastic scaling and observability.
- Evaluation: Beyond general benchmarks, include multi-hop retrieval, long-document summarization, and cross-file code comprehension, and separately monitor time to first token and throughput.
6. Conclusion
Million-token context is not a single trick but systems engineering across architecture compression, data curriculum, and infrastructure coordination. Start by using the memory formula to estimate the feasibility boundary of your own model, then decide on the compression ratio, window size, and parallelism strategy, and finally run long-range evaluations on real business data to avoid a pseudo-long context that “fits but cannot be used.”
