Million-Token Context LLM Architecture: Hybrid Attention, KV Cache Compression, and Long-Context Training
This article breaks down why million-token context in large language models is not simply a matter of enlarging the window, and walks through the three architectural upgrades that make it practical: hybrid attention that splits quadratic complexity, low-rank KV cache compression that raises the memory ceiling, and long-context data engineering during training. It also covers dual-tier model designs that decouple context length from parameter count, plus a four-dimension framework for evaluating whether long context is genuinely usable in production.
1. The Problem: Million-Token Context Is Not Just a Bigger Window
As model capability increasingly depends on compute spent at inference time (test-time scaling), models must not only generate longer chains of thought but also retrieve, compare, summarize, and make decisions across extremely long contexts. At that point, the standard Transformer attention mechanism exposes two structural bottlenecks.
The first bottleneck is compute. Standard self-attention scales quadratically with sequence length: make the sequence ten times longer and the theoretical compute grows a hundredfold. In autoregressive decoding, every new token requires an access pass over all historical keys and values, so the longer the context, the higher the per-token inference FLOPs. Both time to first token (TTFT) and time per output token (TPOT) degrade at the same time.
The second bottleneck is memory. Autoregressive generation must cache the key/value representations of historical tokens at every layer, and this KV cache grows linearly with “layers × sequence length × hidden dimension.” At million-token context lengths, the KV cache often consumes more memory than the model weights themselves, and it crowds out the space available for batching, which directly suppresses throughput.
So the core question for ultra-long context is not “can we fit a million tokens,” but three things: can we afford the compute, can we store it, and can we keep it stable.
2. Capability Tiers: Two Options Under One Architecture Generation
A common productization strategy is to offer two tiers under the same architecture generation: a high-capability version and a high-efficiency version, both natively supporting the same ultra-long context length. The point of this design is to decouple “model scale” from “context capability”—context length is an architectural property, while parameter count is a capacity property. Businesses can trade one off against the other according to their cost budget without giving up long-document processing.
| Tier | Total parameters (illustrative) | Activated parameters (illustrative) | Typical use cases |
|---|---|---|---|
| High-capability | Trillion-scale MoE | Tens of billions | Complex long-document reasoning, cross-document analysis |
| High-efficiency | Hundred-billion-scale MoE | Within tens of billions | High-concurrency long-document summarization, retrieval-augmented QA |
Note that the table above is a generic illustration of architectural form and does not represent the parameters or pricing rules of any specific product. The value of sparse MoE is this: total capacity sets the knowledge ceiling, activated parameters set the per-token compute cost, and only by separating the two can FLOPs and memory both be controlled in long-context scenarios.
3. Upgrade One: Hybrid Attention to Break Apart Quadratic Complexity
Hybrid attention is the mainstream approach to ultra-long context today: some layers perform “global but sparse” information aggregation, while others perform “local but dense” detail modeling, with the two stacked alternately.
- Sparse global branch (understandable as compressed sparse attention, CSA): historical tokens are first compressed into a small number of representative representations, and the current query attends only to those compressed representations, reducing the cost of global dependency from O(L) to something related to the number of compressed blocks.
- Dense local branch (understandable as windowed attention, HCA): full attention is preserved inside a sliding window, ensuring precision for nearby syntax and coreference relations.
The key to this pairing is the division of labor: local layers handle precision, global layers handle recall. Engineering-wise, three details must be solved.
- Compression granularity. Blocks that are too small degrade back into standard attention; blocks that are too large lose detail. Chunking is usually done by “semantic completeness” rather than a fixed length.
- Inter-layer ratio. The ratio of global to local layers determines the number of hops over which long-range dependencies propagate. An imbalanced ratio produces the “remote information never arrives” failure.
- Position encoding extrapolation. At million-token lengths, rotary position embeddings need an adjusted base frequency or a segmented strategy; otherwise resolution at distant positions is insufficient.
Illustrative pseudocode:
def hybrid_attention(x, layer_idx, block_size=512, window=4096):if layer_idx % 2 == 0:# Global branch: compress history first, then attend to compressed representationskv = compress_blocks(x, block_size) # roughly linear complexityreturn sparse_attn(q=x, kv=kv)# Local branch: full attention inside the window to preserve precisionreturn sliding_window_attn(q=x, kv=x, window=window)
4. Upgrade Two: KV Cache Compression Sets the Memory Ceiling
If hybrid attention solves “compute,” cache compression solves “storage.” The common industry approach is low-rank/latent-variable: instead of caching full keys and values, each token’s history is compressed into a low-dimensional latent vector, and the required representations are reconstructed from that latent vector during attention. The benefit is that cache size is decoupled from hidden dimension and depends only on the latent dimension.
Three things must be in place in engineering:
- Cache paging and reuse. Split the KV cache into fixed-size blocks (similar to OS paging), swapping them in and out on demand to avoid fragmented large contiguous memory allocations.
- Prefix sharing. Common prefixes such as system prompts and long documents are stored once and shared across requests, substantially reducing duplicated overhead.
- Quantization and eviction policies. Apply more aggressive quantization or summarization to remote history while keeping near history at high precision, forming a tiered cache that is “precise near, coarse far.”
Memory budget ≈ weights + Σ(per-token latent dim × layers × bytes per precision) × concurrent sequence length
Practical advice: run a small-scale load test first to measure “cache usage per request,” then work backward to the number of concurrent requests you can support, avoiding a sudden throughput drop after launch.
5. Upgrade Three: Training Stability and Long-Document Data Engineering
Million-token context is not an inference-time switch; it is a training-time capability. Common practices include progressively extending context length (short-sequence pretraining → medium-long sequences → ultra-long sequence annealing), tuning learning rate and initialization to suppress activation spikes under long sequences, and constructing retrieval-style and multi-hop reasoning data from long-document corpora. Without this layer, models exhibit a pseudo-long-context phenomenon: they can read the input but cannot use it.
6. Evaluation: How to Tell Whether Long Context Is Actually Usable
Acceptance should be assessed across four dimensions rather than the window number alone:
- Effectiveness: needle-in-a-haystack retrieval across long distances, conflict resolution across multiple documents, and accuracy of structured extraction from long text.
- Efficiency: whether the growth curves of TTFT, TPOT, and per-token inference cost versus context length are approximately linear.
- Stability: output format compliance under ultra-long inputs, plus truncation and repetition rates.
- Cost controllability: cache hit rate, prefix reuse rate, and memory watermark under concurrency.
7. Conclusion
The competition around million-token context has shifted from “how big is the window” to “how expensive is each token.” Hybrid attention flattens the compute, low-rank caching holds down memory, and training-time long-document engineering makes the capability real—none of the three can be missing. For developers, evaluation should factor in the attention architecture, the caching mechanism, and observability metrics together. On cloud platforms such as Baidu AI Cloud, general capabilities like container platforms, object storage, log services, and monitoring and alerting can be combined to build a closed loop for long-context load testing and cost observation, and only then should tier and concurrency strategies be decided.
