Reducing Long-Context Inference Costs: Prefill and KV Cache Optimization Strategies

    Long-context workloads, such as agentic software engineering and deep research, shift inference costs toward prefill computation and KV cache memory. This article explains where these costs come from and outlines optimization approaches for reducing prefill computation and KV cache overhead.

    1. Background: Long Context Makes Inference Cost a Bottleneck

    Agent applications are changing how models are used. A complex software engineering task can involve many tool calls, long terminal outputs, and entire code repositories. Deep research, GUI operation agents, and enterprise approval workflows also require the model to keep track of earlier interaction history. As input length grows from a few thousand tokens to hundreds of thousands of tokens, the structure of inference cost changes. Overheads that could once be ignored become the main bottleneck.

    Inference in traditional decoder-based architectures typically has two stages. The prefill stage processes the entire input at once and generates the initial cache. The decoding stage then generates output tokens one at a time. The two stages have different bottlenecks and call for different optimizations, so they should be analyzed separately.

    2. Where the Costs Come From

    2.1 Prefill Computation

    The prefill stage runs a forward pass over the entire prompt. Assume the model has L layers and the prompt has length N. In each layer, the attention component computes pairwise relevance across all N positions, so its computation scales with N². The feed-forward network scales with N. Overall compute therefore grows with both the number of layers and the input length. In long-input scenarios, prefill time often determines the time to first token (TTFT), which directly affects how responsive the system feels to users.

    2.2 KV Cache Memory

    To avoid recomputing key and value vectors for historical tokens during decoding, the system stores the K and V vectors of every layer. This is the KV cache. The cache grows linearly with context length and occupies high-bandwidth GPU memory for long periods. In multi-turn sessions, the cache may also be swapped out to host memory or local storage, or even migrated over the network. These transfers add read and write latency.

    The KV cache size per token can be estimated with the following formula:

    Bytes per token = 2 × number of layers × number of KV heads × head dimension × bytes per element

    The factor 2 accounts for the K and V tensors. The estimate below uses an assumed configuration. These parameters only demonstrate the calculation method. They do not represent the specifications of any particular model.

    1. Assumptions: 60 layers, 8 KV heads, head dimension 128, FP16 (2 bytes per element)
    2. Per token = 2 × 60 × 8 × 128 × 2 = 245,760 bytes ≈ 240 KB
    3. At 1 million tokens of input, the KV cache alone is about 246 GB

    This figure shows that even when model parameters fit in GPU memory, a long context alone can exhaust it. Reducing KV cache size is therefore one of the core tasks in long-context optimization.

    2.3 Redundancy Across Layers

    In a traditional architecture, each layer stores its own independent KV cache and separately computes the indices needed for sparse selection. Attention patterns in adjacent layers are often similar, so storing and computing them independently creates redundancy. This redundancy can be reduced by reusing results across layers.

    3. Reducing Prefill Computation

    3.1 Asymmetric Front and Back Segmentation

    Prefill and decoding have different computation patterns. Prefill must process the entire input, but the representations of some input tokens only need the first half of the network to meet downstream requirements. One approach splits the network into a front segment and a back segment. Most input tokens pass through only the front segment. Only the tokens that contribute to output generation pass through the full network.