Reducing Long-Context Inference Costs: Encoder-Decoder Splitting and KV Cache Sharing
Long-context inference is increasingly dominated by repeated reads of history rather than by generation. This article examines the KV cache bandwidth bottleneck behind that shift, compares three cost-reduction paths—sequence compression, cross-layer sharing, and low-precision storage—and explains how a causal encoder-decoder split can combine them. It also discusses paged cache management, prefix cache hits, quantization calibration, operator fusion, observability, and evaluation trade-offs.
1. The Workload Has Changed: From Writing More to Reading More
Traditional conversational workloads have roughly symmetric compute: an input of a few hundred words and an output of a few hundred words. Agentic coding tools, code review agents, and long-document question answering are different. After an agent modifies a file, it often needs to read tens of thousands of tokens of source code, test logs, compiler errors, and retrieved snippets back into context, while producing only a few hundred tokens for the next action. This loop repeats, and the resources the model spends on reading can easily exceed what it spends on writing.
These workloads have three stable characteristics:
- Highly repeated prefixes: the same codebase or specification stays almost unchanged across turns, with new content appended only at the tail.
- Severe input/output asymmetry: input length is one to two orders of magnitude larger than output length.
- Frequent continuation: the session is split into many short turns, and each turn must “see” the full history again.
The conclusion is direct: the bottleneck in long-context inference is not generation, but how to repeatedly read history cheaply. This is exactly the problem addressed by a recently published class of inference architecture.
2. Why the Cache Is Large: Two Phases in One Request
A language model splits text into tokens. Each token enters the network and forms hidden vectors layer by layer: attention retrieves information from historical positions, and the feed-forward network continues processing. Three common attention notations are Q, K, and V—Q is the query emitted by the current position, K is used to judge which historical positions are relevant, and V carries the content actually read back. A single-query, single-head computation can be written as:
o = softmax(q · Kᵀ / √c) · Vq ∈ R^{1×c}, K,V ∈ R^{t×c}, o ∈ R^{1×c}
Here t is the visible history length and c is the number of channels. When generating the next token, the K and V of historical positions do not change, so the system stores them for reuse. This is the KV cache. If each layer stores its own copy, cache size grows linearly with “layers × sequence length × channels.”
A request therefore has two phases:
| Phase | Parallelism | Main bottleneck | Cache behavior |
|---|---|---|---|
| Prefill | Processes all input tokens at once | Compute | Writes all historical K/V |
| Decode | Processes one token per step | Memory bandwidth | Repeatedly reads all historical K/V |
In decode, every generated token requires moving the entire history cache, resulting in very low arithmetic intensity. This is the real cost structure of long-context workloads: the problem is not that compute cannot keep up, but that data movement cannot keep up.
A rough estimate (illustrative, not data from any specific product):
cache_bytes ≈ 2 × layers × t × c × bytes_per_elem × batch
As t grows from 4K to 128K, the cache grows 32×; doubling the number of layers doubles it again. To reduce cost, there are only three adjustable factors: the sequence dimension, the layer dimension, and precision.
3. Three Cost-Reduction Paths
1. Sequence-dimension compression. Store fewer channels per position (multi-query/grouped-query-style K/V sharing), compress and merge caches at adjacent positions, or skip caching entirely in some layers and rely on earlier layers for long-range retrieval. The trade-off is reduced long-range exact recall.
2. Cross-layer sharing. Adjacent layers’ K/V are highly similar in representation space, so multiple layers can reuse the same global cache, with only a few layers keeping local caches. This does not change sequence length, but directly divides by a layer factor.
3. Low-precision storage. Reduce the cache from half precision to a lower bit width, or quantize by channel/block while retaining a small number of high-precision “outlier channels.” This is a pure engineering approach with stable gains, but it requires error calibration.
4. Key Structure: Splitting the Network into a Causal Encoder and a Decoder
A recently published approach splits a 40-layer network into two parts: the first 20 layers form a causal encoder, and the last 20 layers form a decoder.
- When processing long input, the model mainly runs the first half and produces a compressed context representation.
- When generating a new token, the model runs the full network, but the history portion reads directly from a shared cache.
- Multiple layers share the same global KV cache, and some layers also reuse retrieval results.
Illustrative logic:
def forward(input_ids, cache):# Phase one: long input goes only through the causal encoderif is_prefill(input_ids):h = causal_encoder(input_ids) # first 20 layerscache.store_global(h) # shared by multiple layersreturn h# Phase two: decode runs the full network; history reads the shared cacheh = cache.load_global() # read once, reused by multiple layersfor layer in decoder_layers: # last 20 layersh = layer(h, kv=h) # only local layers write separate cachesreturn h
5. Why Sequence Compression Is Lighter While Cache Size Falls to One Quarter
This may seem contradictory, but it is the result of three factors multiplying:
- Sharing amortizes the layer factor: the global cache is stored once rather than once per layer, reducing the layer factor from 40 to nearly 1.
- Storage precision decreases: halving the bit width halves the size.
- Only some layers keep separate caches: local caches cover only short-range dependencies, so their length is much smaller than the full sequence.
Lighter sequence-dimension compression means long-range recall capability is better preserved. The large drop in total cache size comes mainly from cross-layer sharing and precision, not from sacrificing context length. This is the most notable part of the design—using the saved space to preserve capability rather than simply piling on length.
6. Engineering Implementation Notes
- Paged cache management: split the cache into fixed-size blocks allocated on demand to reduce fragmentation and simplify prefix reuse.
- Prefix cache hits: codebases, system prompts, and tool definitions remain unchanged across turns and should be reused across requests. Hit rate is the first lever for reducing long-context cost.
- Quantization calibration: keep high precision for outlier channels, calibrate with real business corpora, and evaluate long-range retrieval metrics separately.
- Operator fusion: decode is bandwidth-limited, so fusing attention operators and reducing intermediate tensor round trips is more effective than optimizing floating-point throughput.
- Observability: monitor cache hit rate, prefill/decode time share, and per-request cache peak to locate the real bottleneck.
7. Evaluation Methods and Limitations
Evaluating this type of architecture should not rely only on total throughput. Three groups of metrics are recommended: whether short-input baselines regress, retrieval and citation accuracy under long input, and consistency across multi-turn continuation. Limitations should also be stated clearly: cross-layer sharing weakens the independent modeling capability of some layers; ultra-low-precision caches are more error-prone when exact copying of long identifiers is required (such as hashes or UUIDs); and the encoder/decoder split increases implementation and scheduling complexity. The benefit depends heavily on the proportion of long input in the workload—if business inputs are very short, this design is almost meaningless.
Summary: The cost of long-context inference is essentially the cost of moving the cache. Sequence compression, cross-layer sharing, and low-precision storage can be stacked, and the value of the “causal encoder + decoder” split is that it spends compute in the first half and bandwidth in the second half, reducing cache size without sacrificing context length. In practice, raising prefix hit rate first and then considering architecture changes is usually the more cost-effective order.
