Cutting Long-Context LLM Costs: Attention Compression and Selection Guide

    This article explains why long contexts become a bottleneck for large models, compares three common cost-reduction approaches—sparse attention, hierarchical compression, and KV-cache precision and sharing optimizations—and outlines the data flow of hierarchical compressed attention. It also highlights the trade-offs in accuracy, applicable sequence length, and implementation complexity, and notes the need for unit-cost evaluation and method selection.

    1. Why Long Context Becomes a Bottleneck

    Standard self-attention computes association scores between every token in a sequence and all other tokens. For a sequence of length n, the score matrix of a single layer and a single head contains n² elements. When the input length increases from 10,000 tokens to 1,000,000 tokens, the size of the score matrix expands by 10,000 times. The amount of computation grows quadratically.

    During inference, the memory occupied by the cache used to store historical keys and values—the KV cache—grows linearly with sequence length.

    These two growth curves determine the difficulty of deploying long-context models. Training costs are high, inference concurrency is limited, and the time to first token for a single request also increases.

    Therefore, approaches to reducing long-context costs can be grouped into two categories: reducing the number of token pairs involved in computation, or reducing the amount of data that must be read and written for each token pair. The methods discussed below follow these two directions.

    2. Three Common Cost-Reduction Approaches

    2.1 Sparse Attention

    Sparse attention lets each token attend only to a subset of positions, such as a local window, a fixed stride, or a candidate set obtained through content-based retrieval. These methods are intuitive to implement, but long-range dependencies may be lost. They therefore usually require supplementary mechanisms, such as periodically inserted global nodes.

    2.2 Hierarchical Compression

    Hierarchical compression first merges consecutive tokens into summary units, then performs attention over the summaries. It is similar to two-level reading. The first level preserves details for close reading of paragraphs relevant to the question. The second level compresses large sections into topic indexes for fast localization and global scanning.

    2.3 Cache and Precision Optimization

    Low-bit quantization of the KV cache, or sharing the KV cache across layers or attention heads, can directly reduce memory usage. These methods are independent of the previous two categories and can usually be combined with them. However, low-bit quantization may cause quality degradation on long-tail tasks, so regression testing and spot checks are needed before launch.

    The following table is a rough comparison of the three approaches. These are empirical judgments; specific conclusions need to be validated on your own tasks.

    Method Accuracy risk Applicable length Implementation complexity
    Sparse attention Long-range dependencies may be lost; supplementary mechanisms such as global nodes are needed Medium to long text; works well on locally relevant tasks Low to medium
    Hierarchical compression Summaries that are too coarse lose details; sensitive to the values of m and k Long text; benefits become more obvious as sequences grow longer Medium to high
    KV low-bit quantization and sharing Low bit width may cause quality degradation; long-tail tasks need spot checks Any length; often combined with other methods Low to medium

    3. Data Flow of Hierarchical Compressed Attention

    Suppose every m tokens are merged into one summary vector. If the original sequence length is n, the summary sequence length is approximately n/m. Performing full attention over the summary sequence reduces computation from O(n²) to O((n/m)²). On this basis, local fine reading of selected fine-grained blocks can balance coverage and precision.

    The pseudocode below only illustrates the data flow. Here, mean_vector and dot are assumed helper functions.

    1. def compress_blocks(kv, block_size):
    2. # kv: a key-value sequence of length n, where each element is a d-dimensional vector
    3. # Returns a summary sequence. The tail portion shorter than block_size also produces a summary.
    4. # The summary is averaged over the actual number of elements, without zero-padding, to avoid lowering the mean.
    5. summaries = []
    6. for start in range(0, len(kv), block_size):
    7. block = kv[start:start + block_size]
    8. summaries.append(mean_vector(block))
    9. return summaries
    10. def retrieve_top_blocks(query, summaries, k, query_pos, block_size):
    11. # Causal constraint: retrieve only blocks that are fully before the current position.
    12. # The block containing the current position can only be accessed through the local fine-reading window.
    13. visible = [i for i in range(len(summaries))