Indexer Overhead in Ultra-Long Context: The Next Bottleneck for Sparse Attention

    This article explains why sparse attention still has O(T²) prefill cost when the indexer must score every historical key. It covers how latent-space caching, coarse selection, and exact attention on candidate keys can reduce indexing cost, along with the key design trade-offs, an engineering checklist, and guidance on when this optimization is worth the effort.

    1. The Attention Cost of Ultra-Long Context

    In full attention, every query must compute relevance against every earlier key. With a context length of T, the prefill stage costs O(T²), and decoding each new token costs O(T). When the context grows from 1 million to 10 million tokens, the attention computation alone grows by about 100 times.

    Sparse attention splits this work into two steps:

    1. An indexer selects the k most relevant keys for each query.
    2. Attention is computed only over these k keys, reducing the attention cost to O(Tk).

    When k is much smaller than T, attention cost grows noticeably more slowly. Many long-context services now use similar designs, which shows that attending to a small number of tokens is already feasible in engineering practice.

    2. The Overlooked Bottleneck: Finding the Small Subset

    The benefit of sparse attention depends on how efficiently the indexer works. To pick k keys, the indexer must first score all keys. This scoring step still traverses the entire token history for each query, so the overall prefill cost remains O(T²).

    One study measured the prefill time of a single global attention layer at 10M tokens and found that about 98% of the time went to the indexer, not the attention computation itself. This suggests that attention computation is already cheap enough, and scoring is what sets the upper limit on speed.

    This finding shifts the optimization focus. Earlier work mainly reduced the number of keys involved in computation. As contexts grow longer, the focus moves to lowering the cost of the scoring step.

    3. Why Looking at Only a Few Keys Works

    Sparse attention assumes that the model depends on only a small number of tokens. One study tested this in a 128k-token setting, keeping only the 512 keys most relevant to each query, about 0.8% of all keys. On long-context benchmarks, the model retained about 98% of the accuracy of full attention.

    This result suggests that attention distributions are naturally sparse over long sequences. The challenge is retrieving that sparse subset efficiently, not whether attention needs all keys.

    This conclusion comes from a specific model and specific benchmarks. When moving to other models or tasks, the relationship between sparsity and accuracy must be validated again.

    4. Approach: Indexing in a Compressed Latent Space

    Scoring cost is proportional to vector dimension. If queries and keys are first projected into a low-dimensional latent space, similarity is computed there, and the final attention is computed in the original dimension, the scoring cost can be reduced with controlled ranking error.

    This approach typically has three stages:

    • Caching: Precompute latent representations for each key and update them incrementally as the sequence grows.
    • Coarse selection: Use the latent representation of the new query to approximately score the cached latent keys, and take the top-k candidates.
    • Exact computation: Return to the original dimension and compute exact attention only over the candidate keys.

    The following is illustrative code showing the data flow. It is not a specific implementation:

    1. # Illustrative: two-stage sparse attention in latent space
    2. def sparse_attention(q, K, V, k_top, proj, K_lat_cache):
    3. q_lat = proj(q) # Project the query into the low-dimensional latent space
    4. scores = q_lat @ K_lat_cache.T # Score in low dimension; per-call cost drops with dimension
    5. idx = topk_indices(scores, k_top) # Select k candidate keys
    6. attn = softmax((q @ K[idx].T) / scale) # Exact computation in the original dimension
    7. return attn @ V[idx]

    In this structure, K_lat_cache is the key piece. It precomputes the data needed for scoring, so each decoding step does not re-project the entire history.

    5. Key Trade-offs

    Design requires balancing three dimensions:

    • Projection dimension: Lower dimensions speed up scoring but increase ranking error, which may cause critical tokens to be missed.
    • Cache overhead: Latent representations need extra memory and should be planned together with the KV cache.
    • Candidate count: A larger k gives more stable accuracy but raises the cost of the exact computation stage.

    A common practice is to keep a fixed local window and several special positions, such as the start of the sequence, and merge them with the sparse selection results. This lowers the risk of accidentally dropping nearby context or global anchors.

    6. Engineering Checklist

    1. Baseline measurement: Record the time spent on indexing, selection, and attention computation separately to confirm where the bottleneck lies. Latent-space indexing is worth investing in only when indexing accounts for a large share of the time.
    2. Accuracy evaluation: Compare sparse attention with full attention on tasks such as long-context retrieval, multi-hop question answering, and code understanding.
    3. Dimension sweep: Measure both accuracy and latency across different projection dimensions, and choose the inflection point of the curve rather than relying on a single data point.
    4. Memory budget: Estimate the footprint of the latent cache with the rough formula: number of layers × number of heads × sequence length × latent dimension × bytes per element.
    5. Boundary handling: Test window boundaries, sequence starts, and delimiter positions specifically, to confirm that wrong selections do not drop structural information.

    7. When the Investment Pays Off

    • Short contexts: Indexing overhead usually takes a small share of time, so optimize KV cache layout and batching strategy first.
    • Contexts of 1 million to 10 million tokens: Prefill latency may become the bottleneck for user experience, and indexer optimizations pay off more clearly.
    • Decoding-heavy workloads: Analyze KV read bandwidth in the decoding stage first, since the bottleneck may not be scoring.

    Decisions should be based on baseline measurements. Without measurement data, do not assume the indexer is the bottleneck.