Scaling LLM Inference to Million-Token Contexts: Architecture Breakdown
This article breaks down why long-context inference costs grow non-linearly and how modern LLM architectures address it. It covers dual-path attention that combines sparse retrieval with global coverage, token compression and layer scheduling, mixed FP4/FP8 precision with KV cache quantization, training stability techniques for context extension, post-training and deployment considerations, and a three-part benchmarking approach to verify real speedups.
1. Where the Cost of Long Context Actually Goes
When large models process very long contexts, cost does not grow linearly. It concentrates in three places:
- Attention compute scales quadratically with sequence length.
- KV cache memory grows linearly with length and often becomes the concurrency bottleneck.
- Position encoding extrapolation degrades accuracy, so the model “can see” information but cannot effectively use it.
Any architecture designed for million-token contexts must therefore answer three questions at once: how to reduce per-token compute, how to compress the cache, and how to avoid losing information.
2. Dual-Path Attention: Sparse Retrieval and Global Coverage in Parallel
A common approach splits attention into two parallel paths. One path applies mild compression (for example, 4x) followed by top-k sparse selection, handling precise retrieval. The other applies aggressive compression (for example, 128x) followed by dense attention, handling global coverage. The first preserves accuracy; the second preserves recall. The two are stacked in an interleaved fashion across layers.
Input L = 1,000,000├─ CSA: 4x compression -> 250,000 -> top-k (~1024) sparse attention└─ HCA: 128x compression -> ~7,800 -> dense attention
The key design point is that aggressive compression shortens the sequence to a size that dense compute can handle, avoiding the risk of the indexer missing critical information. Mild compression retains fine-grained retrieval capability. In practice, the first few layers usually keep sliding-window or dense attention to build local representations.
3. Compression Mechanism and Layer Scheduling
Compression is typically done with lightweight projections that aggregate several consecutive tokens into a summary unit, then add position information. For layer scheduling, a common pattern is a local window in the first layer and alternating dual paths in the remaining layers, balancing quality and throughput. Evaluation should look beyond average latency to the tail: P99 latency, cache hit rate, and the compute curve across different context length ranges.
4. Mixed Precision and Memory Strategy
Mixed use of FP4 and FP8 is the current mainstream direction: low precision for matrix multiplications to raise throughput, while sensitive operators (such as normalization, softmax, and residual accumulation) keep high precision. The KV cache can use low-precision storage with block-wise quantization, reducing memory to roughly one tenth of the original. Note that low precision amplifies the impact of outliers, so scaling and per-channel calibration are required.
5. Training Stability and Long-Context Extension
Instability in large-scale training mainly comes from activation explosions and abnormal optimizer states. Common industry practices include clipping attention logits, controlling residual scaling for hyper-connections, and adding stability constraints at the optimizer level. Long-context extension usually proceeds in three stages: short-sequence pretraining, curriculum learning at medium lengths, and a final stage of length extrapolation, gradually increasing the position encoding base.
6. Post-Training and Deployment
On the post-training side, on-policy distillation aligns smaller models to the distribution of larger ones; on the reward side, a combination of “rules + rubric guidance” is preferred to reduce reward drift. On the deployment side, attention is needed on: communication overhead of expert parallelism, write-ahead logging and recovery mechanisms, and declarative management of operator and cache layouts. If relying on general cloud capabilities, you can combine container platforms, object storage, message queues, logging services, and monitoring/alerting to achieve elastic scaling and observability.
7. How to Verify It Is Actually Faster
A three-part stress test is recommended: first, compare per-token compute and cache usage; second, measure throughput and P99 across different length ranges; third, evaluate long-context recall tasks (such as multi-hop retrieval and cross-segment summarization). Only when all three pass can the architectural gains be considered real.
