Layer-Shared KV Cache Architecture for Long-Context LLM Inference: A Technical Review
This article reviews a long-context inference architecture designed for read-heavy workloads, such as AI coding tools that read large amounts of source code, test logs, and tool output before producing a short next action. It covers the workload profile, how the network is split into a causal encoder and a decoder, how layer-shared global KV caching and lower-precision storage reduce the global KV cache to about one quarter of the previous scheme, the benefits and risks of this approach, and how to evaluate it in engineering practice.
When evaluating a long-context inference architecture, looking only at how aggressively it compresses the sequence dimension is not enough. It also matters how the architecture organizes the KV cache, how it schedules the prefill and decode stages, and whether these choices fit the target workload.
One publicly described approach targets workloads with long inputs, short outputs, and frequent context continuation. Consider an AI coding tool that edits a file and then reads back tens of thousands of characters of source code, test logs, and tool output, before producing only a few hundred characters of the next action. Over multiple rounds, the model spends more resources on reading than on writing.
For this kind of workload, the architecture splits the network into a causal encoder and a decoder. During long-input processing, it mainly runs the first half. When generating new tokens, it runs the full network. It also uses a multi-layer shared global KV cache, reuses retrieval results in some layers, and stores data at lower precision. As a result, the global KV cache falls to about one quarter of the previous generation’s scheme. This result is worth examining from three angles: principles, the limits of its benefits, and engineering evaluation.
1. Workload Profile: The Bottleneck Shifts from Compute to Cache Residency and Bandwidth
The KV cache of autoregressive inference can usually be approximated as:
C ≈ 2 × L × S × D × B × bytes
Here, L is the number of independent cache layers, S is the sequence length, D is the total Key and Value dimension per layer, B is the concurrent batch size, and bytes is the number of storage bytes per element.
With a traditional independent cache for each layer, the layer factor L cannot be compressed. Long contexts push S higher. Multi-round continuation keeps the cache resident for a long time. For read-heavy workloads, prefill requires a large amount of computation, but there are few decode steps. As a result, cache capacity, cache read and write bandwidth, and cache management overhead often become bottlenecks before raw generation compute does.
This type of workload has two more characteristics. First, context repetition is high. The same file, log segment, or batch of tool output may be referenced again and again across rounds. Second, outputs are very short. Each round produces only a few tokens, yet the model must locate, summarize, and decide the next action based on a large amount of input. Therefore, the optimization priority is not simply higher throughput for long generations. It is to reduce the peak cost of building the cache during long-input processing, increase cache reuse, and maintain quality over a small number of decode steps.
2. Architecture Breakdown: Half-Depth Encoding and Full-Depth Decoding
In the example configuration from public materials, a 40-layer network is split into 20 causal encoder layers and 20 decoder layers. When processing long input, the system mainly runs the first half. When generating new tokens, it still runs the full network. This stage split has several direct effects.
First, the token-level cache for the input mostly sits in the encoder layers. Long inputs no longer need a full KV copy in every layer, so cache write pressure and peak capacity during prefill both drop. Second, the generation stage runs the full network. This keeps the lower half’s role in generation quality, long-range dependencies, and the format constraints of tool calls. Third, the encoder output is passed to the decoder side as a context representation, and generated tokens gradually fill in the state the generation side needs. The design neither truncates the input nor relies on shallow encoding alone. It separates reading the context from writing the next step into different computation stages.
The following pseudocode illustrates this scheduling. It shows only logical relationships and does not represent real interfaces.
# Illustration: staged execution with layer-shared KVshared_kv = {} # group_id -> low-precision K/V cachedef prefill(tokens):h = embed(tokens)for layer in encoder_layers: # Long inputs mainly run through the first halfh = causal_encoder_layer(h)if layer.in_shared_group:shared_kv[layer.group] = write_lowp(h.kv)return hdef decode_step(token, state):h = embed(token)for layer in full_layers: # Generation runs the full networkkv = shared_kv.get(layer.group) or state.local_kvh = decode_layer(h, kv)return logits(h)
The key trade-off in this design is that the first half handles context encoding, and the second half handles generation adaptation. If the sharing granularity is too coarse or the low-precision error is too large, the representations across layers lose their ability to distinguish between inputs. Long-range localization and precise citation may then suffer. For this reason, evaluation should consider more than the cache reduction ratio. It should also measure accuracy on long-context tasks, the correctness of tool-call arguments, and consistency across rounds.
3. Where the Roughly Four-Fold Cache Reduction Comes From
The global KV cache falls to about one quarter mainly because of two multiplicative factors. Aggressive token eviction is not the cause.
The first factor is cross-layer sharing. Several layers share one global KV cache, and some layers also reuse retrieval results. Each group keeps only one copy of K and V, while the query side still applies per-layer transformations and adaptation. This reduces the independent cache layer factor and the number of cache copies. Sharing does not make all layers fully equivalent. Instead, layers are organized into groups that draw on a shared global cache pool, which cuts duplicate storage.
The second factor is lower storage precision. KV values are stored at low precision, supported by calibration methods such as group-wise scaling and keeping outliers at high precision. These methods significantly reduce the bytes per element. If sharing halves the number of independent cache layers and storage bit width also halves, the multiplicative factor is about one quarter. The actual ratio depends on the sharing granularity, the precision strategy, the head dimension, and the batch size.
The source material also notes that the design applies less compression along the sequence dimension. It does not free space by evicting tokens aggressively. Instead, it optimizes space through cross-layer sharing and storage precision. This helps preserve long-range information, but it also makes precision calibration and sharing error control more important.
A roughly four-fold reduction in the global KV cache does not mean a four-fold reduction in total end-to-end memory. Weights, activations, temporary buffers, and paging overhead still take up memory. The benefits mainly appear in the cache dimension for long-context, multi-round, and high-concurrency scenarios.
4. Benefit Boundaries and Risks: Suited to Read-Heavy Workloads, Not a Universal Solution
The advantages are clear for scenarios with long inputs, short outputs, repeated context, and frequent tool calls. AI-assisted programming, log root-cause analysis, code retrieval Q&A, and multi-round file editing can all benefit. Cache reuse and cross-layer sharing lower the cost of keeping long inputs resident, and half-depth encoding lowers the prefill peak.
The risks are equally clear. First, excessive layer sharing weakens the division of labor between layers, which can degrade performance on inputs that require deep reasoning. Second, low-precision storage may accumulate errors over long sequences. This matters most for tasks that depend on exact citation, numerical comparison, or long-distance dependencies. Third, half-depth encoding may not be enough for inputs that need full-layer context interaction. Although the generation stage runs the full network, the quality of the input-side representation still sets an upper bound. Fourth, reusing retrieval results requires the retrieved content to stay stable. If files change frequently, the cache hit rate will drop.
Therefore, this architecture is best described as an efficient cache organization scheme for specific workloads. It does not universally reduce the cost of long-context inference. Developers should run end-to-end evaluations against their own task distribution rather than relying only on cache capacity metrics.
5. Engineering Deployment and Evaluation Guidelines
For deployment,
