Inside the Engineering of Million-Token Context LLMs
This article breaks down what it takes to support million-token context in large language models, moving beyond benchmark scores to the underlying engineering. It explains why long context has become a requirement for agentic workflows and repository-level code analysis, outlines the real bottlenecks (quadratic attention cost, linear KV cache growth, position encoding extrapolation, and memory bandwidth), and details four system-level levers: attention restructuring, memory and cache management, sparse MoE activation control, and kernel-scheduling co-design. It closes with practical guidance on when and how to use long context effectively, plus general selection and deployment considerations.
1. Introduction: What an Unannounced Open-Source Release Reveals
In late April 2026, an open-source LLM series released a preview version and opened its weights at the same time. Community discussion focused on two points: the scores had reached the top tier, and the context length was pushed directly to the million-token level. But reading only the leaderboards makes it easy to miss the part that actually matters—to support million-token context, the model underwent a systematic overhaul across the attention mechanism, memory management, batch scheduling, and low-level operators.
Two common misconceptions need clearing up first:
- The large-parameter and small-parameter versions are not in a “distillation relationship.” Both are independently pre-trained sparse Mixture-of-Experts (MoE) models; they simply differ in total parameter count and activation sparsity.
- Long context is not a “switch.” Both model tiers support million-token context by default, and the serving side no longer distinguishes long-text from short-text models, so routing and caching strategies can be designed uniformly.
From the published paper specs, the larger version has roughly 1.6 trillion total parameters with about 49 billion sparsely activated, while the smaller version has roughly 284 billion total parameters with about 13 billion sparsely activated. Both have a million-token context window. What developers should really pay attention to are the engineering constraints behind these numbers.
2. Why Million-Token Context Shifted from an Arms Race to a Necessity
We used to assume a few thousand tokens were enough for everyday Q&A, but once a model is placed in AI Agent and repository-level code analysis scenarios, token consumption balloons quickly.
Scenario 1: Multi-Turn Agent Tasks
An agentic coding tool running 30 turns accumulates the following in context each turn: user instructions, several source file contents, command execution results, and the model’s own reasoning traces. After dozens of turns, hundreds of thousands of tokens is the norm. Early models discarded prior reasoning content when new messages arrived—not out of design preference, but because context capacity and memory cost did not allow retaining it.
Scenario 2: Whole-Repository Code Understanding
A mid-sized project with 300 files and 150,000 lines of code lands roughly in the 800,000 to 1.2 million token range after tokenization. To complete cross-file refactoring, dependency mapping, and impact analysis, you must fit enough context into the window at once; otherwise you can only rely on retrieval slices, sacrificing global consistency.
Scenario 3: Long Documents and Persistent Memory
Tasks like contract review, log troubleshooting, and long-report generation require the model to maintain stable reference and citation ability across very long text. Meanwhile, an agent’s persistent memory keeps accumulating, and a 128K-level window is often exhausted in just a few turns.
3. The Real Bottleneck Is Not “Window Size”
Increasing the nominal window value is relatively easy; the hard part is keeping throughput and cost under control in real serving. There are four core bottlenecks:
- Attention overhead grows quadratically with sequence length, amplifying both computation and memory access for long sequences.
- Key-value cache (KV Cache) memory usage grows linearly with length, directly limiting concurrency.
- After position encoding extrapolation, the model’s effective receptive ability on long-range dependencies decays.
- Memory bandwidth and operator efficiency become the main constraints, while compute utilization actually drops.
Therefore, long-context capability must be treated as a systems engineering problem, not a single model-structure problem.
4. Four Levers for System-Level Overhaul
1. Attention Structure Redesign
The direction widely adopted in the industry is sparsification and low-rank compression: let each query attend to only a portion of the key history, while compressing lengthy history into a small number of reusable representations. This reduces both computation and cache size. In engineering, this is usually paired with block-wise computation, keeping attention dense within blocks and sparse across blocks, so it can be executed efficiently by low-level operators.
2. Memory and Cache Management
Viable approaches include paged cache management to reduce fragmentation, evicting cold data by request priority, and cross-request sharing of reusable prefixes. For multi-turn scenarios like agents, prefix sharing often brings an order-of-magnitude cost reduction, because the system prompt and tool descriptions are identical every turn.
An illustrative cache key design:
def cache_key(system_prompt, tools_schema, model_version):# When the prefix is identical, hit the same cache and avoid recomputationreturn hash((system_prompt, tools_schema, model_version))
3. Sparse MoE and Activation Control
Large total parameters with small activated parameters is the key to balancing capacity and inference cost. Activating only about 49 billion out of 1.6 trillion parameters means the computation per token is kept within an acceptable range. In engineering, expert load balancing matters to avoid a few hot experts causing long-tail latency.
4. Kernel and Scheduling Co-Design
Long-context performance ultimately comes down to the operator level: fused attention kernels, quantized memory access, continuous batching, and request chunk scheduling. Only when model structure and scheduling strategy are co-designed can the nominal window translate into real throughput.
5. Practical Guidance: How to Use Million-Token Context Well
- Clarify whether you truly need it. If a task can be solved with retrieval, a short window plus retrieval is usually more economical.
- Control the context “signal-to-noise ratio.” Proactively trimming irrelevant history is more effective than simply piling on length.
- Reuse stable prefixes. System prompts, tool definitions, and project conventions should be fixed at the front to hit the cache.
- Layered memory. Keep recent conversation verbatim and summarize far-range memory, balancing accuracy and cost.
- Focus on observability. Record token usage, cache hit rate, and time-to-first-token in container platforms and logging services to locate bottlenecks.
6. General Approach to Selection and Deployment
When deploying such models in the cloud, you can combine general capabilities: use a container platform to host inference services and scale elastically, use object storage to hold weights and datasets, use a message queue to smooth request peaks, and use monitoring and alerting to track memory and latency metrics. Baidu AI Cloud offers the same kind of general cloud product capabilities in these scenarios, and developers can do capacity planning based on their own traffic characteristics.
Suggested evaluation dimensions include: actual accuracy on long-text tasks, service cost per million tokens, tail latency under concurrency, and cache hit rate. Leaderboards can only serve as an initial filter; end-to-end evaluation on real business corpora is the basis for decisions.
7. Conclusion
The value of million-token context lies not in the number itself, but in moving tasks like agents and repository-level code understanding from “slice approximation” to “global visibility.” By understanding the four main threads—attention sparsification, cache reuse, sparse activation, and kernel scheduling—developers can make more rational trade-offs among cost, latency, and quality.
