Hybrid Decoder Architecture: How KV Cache Shrinks to About One Quarter in Long-Context Workloads
This article explains why prefill often dominates decode in long-context LLM agents, how KV cache grows expensive, and how a causal-encoder plus full-decoder hybrid with cross-layer KV sharing and lower-precision storage can cut global cache to about a quarter of a previous setup—without claiming extra products or numbers.
Why “reading” can cost more than “writing”
In many modern LLM applications, coding agents (programming assistants, automated test agents, log-analysis tools, and similar systems) share a typical workload: each turn the model must read tens of thousands of tokens of source, test logs, and tool output, then emit only a few hundred tokens of next-step instructions. Over multiple loops, compute and GPU memory spent on “reading” easily exceed what is spent on “writing.”
Inference systems call this inverted load prefill-heavy, decode-light. A standard Transformer does not separate the two phases: both input and output run the full stack and keep a full KV cache. When the prompt is extremely long and the completion is short, most compute and storage sit in historical context—the main driver of inference cost.
Recent public model technical reports explore architecture-level optimizations for this pattern. One approach reuses layers so long-input encoding takes a “half path,” shares KV cache across layers, and greatly reduces global cache. This article unpacks the idea and its engineering implications.
Why KV cache is expensive
Attention and Q, K, V
The language model splits text into tokens. Each token becomes hidden vectors at every Transformer layer. Attention reads from history; the feed-forward network processes that information.
The three common tensors are Q (query), K (key), and V (value). Q is the query at the current position; K scores which past positions matter; V holds the values to be read. For one query and one ordinary attention head:
o = softmax(q · K^T / sqrt(c)) · V
Here q is the current query, K and V hold keys and values for t visible past positions, and c is the channel dimension. Softmax turns scores into weights. Production systems often transform K and V further to save memory.
Where KV cache comes from
When generating the next token, past K and V do not change and can be reused. The inference stack stores them as the KV cache. Size depends on layer count, number of heads and their dimensions, and sequence length. More layers and longer sequences mean a larger cache.
Even a single new token must read the full historical cache for attention. At hundreds of thousands of context tokens, KV read and store pressure both memory bandwidth and GPU capacity, which sets cost and concurrency.
Two-phase asymmetry
A request is typically split into:
- Prefill: process the entire existing input once, build state for later generation; compute-heavy but highly parallel.
- Decode: emit tokens one by one, each step reading history; bandwidth-bound and hard to parallelize.
Classic designs share one network and one full KV layout for both phases, with little room to specialize for long input, short output, and frequent continuation.
Core design: mixed causal encoder and decoder layers
Encode on the first half, decode on the full stack
The report’s idea (using the disclosed 40-layer example) splits the stack:
- First 20 layers as a causal encoder: on long input, mainly this half runs, producing a compressed intermediate state for context.
- Last 20 layers plus the full network for decode: new tokens still go through the complete model so generation quality and deep reasoning are not cut off in shallow layers.
Intuitively, very long prompts only pay for the first half of the network at prefill, so one-shot prefill compute drops. Decode still uses the full model, so generation capacity is not weakened—structurally separating the “read” and “write” paths.
Cross-layer shared KV cache
Storage matters as well. Traditionally each layer keeps its own KV cache, so cache grows with depth. This design shares one global KV cache across layers so neighboring or grouped layers do not each store historical keys and values. Structured retrieval results (for example from tools or document indexes) can be reused the same way, avoiding duplicate compute and storage.
Lower storage precision
On top of sharing, the report stores KV at lower precision. Attention can tolerate some quantization error; keeping K and V in a low-precision format further shrinks cache with little quality loss. Sharing attacks “store it many times”; quantization attacks “store it too heavily.”
Reading the results: milder sequence compression, much smaller cache
A notable point: compression along the sequence (context-length) axis is lighter than in a prior generation, yet global KV cache falls to about one quarter of that prior configuration. The savings come mainly from eliminating redundant per-layer caches and from fewer bytes per stored tensor.
The architectural takeaway: KV compression need not be a single-axis breakthrough. Sequence compression, cross-layer sharing, and precision can be traded together. Aggressive time-axis compression can hurt long-range recall; compressing along depth and precision often buys more space at a smaller quality cost.
Engineering takeaways
For inference services and agent platforms:
- Profile the load first. Measure real prefill/decode ratios. If input is much longer than output, optimize the prefill path and cache layout before chasing decode speed alone.
- Layer reuse. In custom or forked stacks, explore cross-layer cache sharing and split shallow “general encoding” from deep “generation reasoning.”
- Tiered precision. Use common quantization toolchains so hot layers keep higher KV precision and colder layers use lower precision.
- Platform ops. GPU containers, autoscaling, and monitoring can schedule instances by cache watermark so long-context jobs do not starve concurrency.
Summary
For long input and frequent continuation, next-generation LLM architecture work is moving from isolated algorithm tricks to jointly redesigning compute paths and storage. Half-path encoding plus full-path decode, cross-layer KV sharing, and low-precision storage together bring global cache to about a quarter of a previous setup. The method is: characterize the workload, choose compression axes, then combine them in engineering. While long-context cost still dominates inference, this line of work will keep evolving as a practical pillar of LLM deployment.
