A New Approach to Cutting the Cost of Million-Token LLM Inference
This article examines how long-context LLM inference becomes cost-prohibitive as context windows scale to millions of tokens, and how combining three mechanisms—multi-head latent attention, sparse attention, and cross-layer index reuse (IndexShare-style)—makes it practical. It explains the core idea of refreshing sparse indexes only once every few layers instead of at every layer, provides pseudocode, outlines engineering caveats, analyzes complexity and trade-offs, and offers evaluation and deployment guidance. Readers will learn where the savings come from, when reuse can hurt accuracy, and how to tune group size with observability.
1. The Cost Wall of Long-Context Inference
When the context window grows from a few thousand tokens to the million-token range, the quadratic growth of attention computation quickly consumes the compute budget. The industry typically responds in two ways. The first is to narrow the candidate range for attention, known as sparse attention. The second is to compress the representation of the key-value cache, known as latent attention. The former reduces “how much is looked at,” while the latter reduces “how large each representation is.” Only when the two are combined does long-context inference become engineering-feasible.
Even with a trimmed candidate range, however, the indexer itself still has to run at every layer: each layer must re-score, re-rank, and select the top-k tokens for the current query. As the number of layers grows, this overhead takes up a larger share of total latency and becomes a new bottleneck.
2. Architecture Breakdown: How the Three Mechanisms Divide the Work
A recent architectural evolution in an open-source model family offers a reference combination pattern. Its technical elements can be described neutrally as follows:
- Multi-head latent attention: Projects the key-value cache into a low-dimensional latent space before caching it, then restores it on demand during inference, substantially reducing cache capacity and bandwidth pressure.
- Sparse attention: Selects only a small set of the most relevant historical tokens for each query, reducing attention complexity from quadratic to roughly linear.
- Cross-layer index reuse (IndexShare-style): Instead of recomputing the sparse index at every layer, performs a full index pass every few layers and lets subsequent layers directly reuse the already-selected token set.
The relationship among the three is progressive: latent attention solves “fitting it in memory,” sparse attention solves “computing less,” and index reuse solves “selecting more cheaply.”
3. Core Idea: Reducing Index Computation from Once per Layer to Once Every Four Layers
In the conventional approach, the indexer’s output is a cost term on the same order as the number of layers. The key assumption behind cross-layer reuse is that attention distributions in adjacent layers are highly similar: the critical tokens identified in a shallow layer are likely to remain important in the following few layers.
The implementation can be abstracted as the following pseudocode:
# Refresh the sparse index every group_size layers; reuse it on the othersif layer_idx % group_size == 0:topk_idx = indexer(query, key_cache) # full scoring + rankingshared_idx = topk_idxelse:shared_idx = shared_idx # reuse the previous group's resultattn_out = sparse_attention(query, key_cache, shared_idx)
Three engineering points deserve attention. First, index quality is most sensitive at group boundaries, which can be mitigated with weighting based on layer position. Second, the index cache must be aligned with the lifecycle of the key-value cache to avoid misalignment during incremental decoding. Third, the group size is the direct knob trading accuracy for speed; 2 to 4 layers is generally a safe choice.
4. Complexity and Benefit Analysis
Let the number of layers be L and the group size be g. Index-related overhead drops from O(L·C_index) to O((L/g)·C_index), roughly a three-quarter reduction at g=4. Combined with the cache compression from latent attention, both memory footprint and memory-bandwidth pressure decrease in million-token scenarios, leaving room to improve time-to-first-token and decoding throughput.
The benefits, however, are not unconditional. When a task requires capturing fine-grained differences across layers—such as multi-hop reasoning or precise retrieval localization—excessive reuse may cost accuracy. Evaluation must therefore consider both task metrics and system metrics.
5. Deployment Recommendations and Evaluation Method
- Establish a baseline first: Fix input length, batch size, and sampling parameters, then record accuracy, time-to-first-token, per-token latency, and peak memory usage.
- Sweep the group size: Use g=1 as the accuracy upper bound, increase g step by step, and find the inflection point where the accuracy drop becomes acceptable.
- Make layers observable: Add hit-rate statistics and index-drift metrics on the reuse path to determine which layers are suitable for reuse.
- Combine with general cloud capabilities: Long-context services typically also need object storage to hold long documents, a message queue to smooth request spikes, a container platform to host elastic inference replicas, and logging plus monitoring and alerting for observability.
- Leave headroom in capacity planning: The index cache and key-value cache share the memory budget, so stress tests should cover the combination of the longest context and the highest concurrency.
Baidu AI Cloud has general engineering experience in long-context inference and elastic compute scheduling, which can serve as an infrastructure-level reference for deploying the architecture described above.
6. Conclusion
The value of cross-layer index reuse lies in trading a minimal architectural change for a considerable reduction in inference cost, without altering the basic premise of sparse attention. For developers, the real difficulty is not the implementation but finding the balance point between group size and task accuracy, and continuously validating it with observability.
