Sparse Attention at 10 Million Tokens: The Hidden Cost of Finding the Right Keys

    As context windows grow from millions to tens of millions of tokens, sparse attention shifts the bottleneck from computing attention to selecting which keys to attend to. This article breaks down why the indexer becomes a quadratic hidden cost, how latent-space sparse attention moves retrieval into a compressed space to achieve near-linear scaling and 22x-plus speedups, and what design constraints preserve accuracy. It also covers evaluation dimensions and practical adoption steps for long-context inference.

    1. The Problem: Longer Contexts Change the Cost Structure

    Over the past two years, long-context capability has shifted from an optional feature to a default specification. Million-token context windows now appear in the default configurations of several model services, and architectures that mix sparse attention with linear attention are widely adopted. These approaches share the same premise: instead of having every query scan all historical tokens, each query selects only a few of the most relevant keys for computation.

    This approach has been repeatedly validated at the million-token scale. But as contexts push toward 10 million tokens, an initially negligible overhead starts to spiral out of control.

    Consider a global attention layer performing prefill at 10 million tokens. Most of the time is not spent on attention itself, but on the indexer that decides which tokens to look at. In other words, the cost center has shifted.

    The nature of the problem therefore changes. Sparse attention has long proven that “looking at only a small subset” is enough to preserve accuracy. What actually blocks ultra-long contexts is that “finding that small subset” still grows quadratically with context length.

    2. Cost Breakdown: Why Sparse Attention Carries a Hidden Bill

    The cost of standard full attention is straightforward: with context length T, prefill computation is roughly O(T²), and decoding each token costs about O(T). Going from one million to ten million tokens increases prefill attention computation by roughly 100x.

    Sparse attention divides the work like this: a lightweight indexer first selects the k most relevant keys for each query, then the actual attention computes only over those k keys, reducing attention itself to O(Tk).

    This works under one assumption: the model genuinely needs only a few keys. Industry validation at moderate lengths is fairly consistent—at around 100K–150K tokens, letting each query attend to only the few hundred keys it is truly most relevant to (a few thousandths of all keys) still retains over 98% of full-attention accuracy on long-context benchmarks.

    But here lies a paradox: for the indexer to know which few hundred keys are most relevant, it must first score every key. So the indexing stage itself falls back to O(T²). Existing engineering techniques—lowering scoring precision, compressing dimensions, or merging multiple keys into a block before scoring—only shrink the constant factor without changing the order of magnitude. As the context grows, this bill inflates again and eventually consumes all the gains from sparsification.

    The conclusion is clear: to make ultra-long contexts truly usable, the retrieval step must also become sub-quadratic.

    3. The Core Idea: Move Retrieval into Latent Space

    A class of methods called latent-space sparse attention offers an answer: instead of doing full scoring in the original token space, first project the query and keys into a low-dimensional latent space, perform relevance retrieval in that compressed space, then map the results back to the original space for exact attention.

    You can think of it as two-stage filtering:

    1. Stage 1 (coarse filtering, low-dimensional latent space):
    2. Dot product between each query and the latent representation of all historical tokens → take top-k candidates
    3. Complexity target: close to O(T · d_latent), where d_latent is much smaller than the head dimension
    4. Stage 2 (exact computation, original space):
    5. Full attention only over the k candidate keys
    6. Complexity: O(T · k · d_head)

    The key point is that the coarse filtering stage no longer performs high-precision, high-dimensional scoring for every key. Instead, it reuses a set of low-dimensional, cacheable latent representations. These representations can be maintained incrementally and updated on the fly during decoding, so the candidate set needed for the second stage is almost “looked up” rather than “recomputed.”

    In terms of magnitude, the original O(T²) indexing overhead is reduced to near-linear in T, while the attention body remains O(Tk). Adding the two together brings the overall complexity from quadratic down to sub-quadratic—the dividing line for whether ultra-long contexts can be deployed.

    4. Why Accuracy Does Not Collapse

    These methods preserve accuracy through three design constraints:

    1. Coarse filtering and exact computation share the same semantic space. The latent representations are not an independently trained side path; they are derived from the existing projection matrices, ensuring that similarity rankings in the low-dimensional space closely match those in the original space.
    2. The top-k value stays small and configurable. k determines both the exact-computation cost and the recall ceiling; in practice, a few hundred is enough to cover the vast majority of effective context.
    3. Positional information is not compressed away. The latent representations retain the necessary positional encoding components, preventing long-range dependencies from being erased during compression.

    Engineering also requires recall monitoring: comparing the coarse-filtered candidates against the true top-k from full scoring offline is the direct way to judge whether the latent dimension is sufficient or whether k needs to increase.

    5. Results and Evaluation

    At 10 million tokens in a single-layer prefill, these methods achieve more than 20x speedup over native sparse attention; on another architecture where the indexer accounts for a larger share, the speedup can reach several tens of times. Meanwhile, accuracy on long-context retrieval, multi-hop reasoning, and long-document QA tasks remains close to the original model.

    It is worth emphasizing that these speedups depend heavily on three variables: context length, the indexer’s share of total time, and latent-space dimension. The longer the context and the higher the indexer share, the greater the benefit; conversely, below a few hundred thousand tokens, the gains may not be obvious.

    Evaluation should cover four dimensions:

    • End-to-end prefill latency and time to first token;
    • Indexing stage time share (to confirm whether the bottleneck has truly shifted);
    • Long-context benchmark accuracy, especially tasks requiring retrieval across a wide range;
    • GPU memory usage and cache hit rate, since incremental maintenance of latent representations adds persistent memory overhead.

    6. Adoption Recommendations

    To evaluate a similar approach in your own inference stack, proceed in this order:

    1. Measure first, optimize second. Use per-layer timing breakdowns to confirm whether the indexer has become the primary bottleneck, avoiding investment in the wrong place.
    2. Start with a small k. Validate the accuracy floor with a small candidate count, then gradually increase it to the balance point between accuracy and cost.
    3. Make the latent-space dimension configurable. The optimal compression ratio varies widely across models and tasks.
    4. Coordinate with caching strategies. Latent representations are well suited to persistent caching, and pairing them with paged cache management can further improve decoding performance.
    5. Keep a full-attention fallback path. For accuracy-sensitive scenarios, allow per-request switching back to exact retrieval.

    The competition in ultra-long contexts has shifted from “how large is the window” to “how low is the inference cost per token.” When retrieval itself is compressed to near-linear, 10-million-token contexts become engineering-affordable for the first time. For teams that need to control costs in long-document understanding, code-base-level analysis, and multi-turn long conversations, this may be a more worthwhile direction than continuing to expand the window.