KV Cache Cost Reduction and Architecture Redesign for Million-Token Contexts
This article examines why long-context AI agents are increasingly constrained by KV Cache storage and data movement rather than raw compute, and how a causal encoder-decoder architecture combined with layered cache compression addresses the problem. It covers the three cost dimensions of long-context serving, a representative MoE specification with asymmetric activation, depth-wise splitting of the backbone into encoder and decoder stages, four stacked cache compression mechanisms, the system components needed to make persistent caches a first-class resource, and the practical capability boundaries of the approach.
1. The Bottleneck Has Shifted: Long-Running Agents Now Struggle to Store and Move, Not Compute
Early long-context optimization efforts focused almost entirely on the complexity of attention computation. Sparsification, local windows, and linearization did reduce floating-point work. But once agents enter real workflows — multi-turn tool calls, long document and codebase understanding, repeated backtracking reasoning — the real cost center moves: the global key-value cache (KV Cache) must stay resident for a long time, be reused across turns, and be shuttled repeatedly between layers. The longer the context, the sooner GPU memory capacity, memory bandwidth, and cross-node communication become the binding constraints.
So the standard for evaluating a long-context approach should not be “how much memory is saved per token,” but whether it can reduce three kinds of cost at the same time:
- Compute cost: repeated projection and attention during prefill;
- Storage cost: capacity consumed by resident cache;
- Transfer cost: cache swap-in and swap-out, plus data movement across GPUs and across nodes.
This means no effective solution can be a single trick; it has to be a coordinated design. The rest of this article follows the order: why reduce cost → how to change the architecture → how to compress the cache → how the system supports it → capabilities and boundaries.
2. Pin Down the Specification First: Every Mechanism Follows From the Constraints
Before discussing architecture, here is a typical specification (used to explain the design logic, not as a specific product’s metrics):
- The backbone is a Mixture-of-Experts (MoE) structure with parameter counts in the hundreds of billions; a separate conditional memory module carries long-term knowledge and is not counted in the backbone total;
- Maximum context length is around one million tokens;
- Input supports images and text; output is autoregressive text;
- Asymmetric activation: about 8 billion parameters activated per token during encoding, about 16 billion during decoding.
Asymmetric activation is the key to understanding the rest of the design. Encoding only needs to compress a long prompt into representations once, so it can take a “compress heavily, compute lightly” path. Decoding must emit a new token at every step and is latency-sensitive, so it depends more on cache reuse than on repeated computation.
3. Architecture Core: Split the Network by Depth into an Encoder and a Decoder
The most representative change is moving from a pure decoder-only structure to a Causal Encoder-Decoder. The 40-layer backbone is split in half along depth:
- The first 20 layers form the causal encoder: they read the full prompt only during prefill;
- The last 20 layers form the decoder: instead of revisiting the full prompt layer by layer, they project a global cache from the encoder’s final-layer hidden states, and at each layer rebuild a local window cache based only on the most recent tokens.
Illustrative expression (pseudocode):
# Prefill: the encoder consumes the long prompt in one passH = causal_encoder(prompt_tokens) # shape: [n, d]# The global cache and compression weights for decode layer l are both linear projections of HC_l = H @ W_kv[l] # global KV entriesZ_l = H @ W_z[l] # compression weights# During decoding, only the local window cache is rebuiltswa_l = build_window_kv(recent_tokens, window=n_win)
In terms of complexity, prefill drops from “every layer reads the full prompt” to “only the first half of the network reads the full prompt.” When prompt length is far greater than window length, prefill compute is roughly halved. More importantly, the back half of the network accesses long-range information uniformly through projections of a fixed-size global representation, so cache size is decoupled from prompt length.
4. How to Compress the Cache: Stack Four Mechanisms Instead of Optimizing One Point
A single technique can rarely hold down both capacity and bandwidth, so in practice four mechanisms are stacked:
- Context-distillation-style encoding: use encoder outputs in place of raw per-layer keys and values, for higher information density;
- Two-dimensional sparse attention: sparse selection along both the time dimension and the channel dimension, reducing ineffective reads and writes;
- Low-bit primary cache: the global cache is stored at roughly 4-bit precision, with per-entry scaling factors to control error;
- Bounded window replay: the local window cache has an upper bound; once exceeded, it is re-compressed and replayed, avoiding linear growth.
The combined effect, as an order of magnitude: the global cache can be compressed to a few hundred bytes per token, roughly one quarter of the previous generation, and the persistent cache to roughly one eighth. In engineering terms, pay particular attention to numerical stability under low-bit storage; the usual practice is to keep high-precision scaling factors and handle outlier channels separately.
5. How the System Supports It: Make the Cache a First-Class Citizen
Architecture and compression only matter once they land at the system layer. To reproduce similar capabilities on your own platform, these general components are worth attention:
- A container platform hosting elastic inference replicas, scheduled with session affinity;
- A distributed cache or in-memory database holding persistent cache across turns;
- Object storage for spilling and restoring cold cache for long sessions;
- A message queue decoupling tool calls from the main inference path;
- Logging services plus monitoring and alerting to track cache hit rate and swap-in/swap-out latency.
Capacity can be estimated with a rough model:
cache bytes ≈ token count × bytes per token × concurrent sessions × replica redundancy factor
Pushing “bytes per token” from thousands down to hundreds is equivalent to serving several times more concurrency on the same GPU memory — that is where the real cost reduction comes from.
6. Capabilities and Boundaries: A Realistic View of the Gains
Three boundaries need to be stated. First, one-shot encoder compression loses some fine-grained information, so precise retrieval tasks over very long documents need to be paired with external retrieval. Second, low-bit caches are sensitive to distribution drift and require continuous evaluation. Third, updating and governing the conditional memory needs a separate process to avoid stale knowledge. Overall, the value of this approach is that it reframes long context from a compute problem into a cache and bandwidth problem, providing a reusable engineering pattern for million-token agent applications.
