KV Cache Cost Optimization for Million-Token Context LLM Inference: From Architecture to Cache Co-Design
Long-context LLM inference is increasingly constrained by global KV cache storage, reuse, and movement, not only by attention compute. This article presents a full-stack approach built around asymmetric activation, a causal encoder-decoder (CED) split, and four KV compression techniques: Engram conditional memory, CSA-like sparse attention selection, FP4 main KV cache, and SWA bounded replay. It also covers the system-side requirements—paged KV management, tiered storage, Prefill/Decode disaggregation, and quantization-aware training—and discusses capability boundaries and fallback strategies for precision-sensitive workloads.
The Real Bottleneck: Long-Horizon Agents Have Shifted the Constraint
For years, the industry widely viewed the core challenge of long context as the quadratic growth of attention computation with sequence length, which led to broad adoption of various sparse attention methods. But when context truly reaches the scale of hundreds of thousands or even one million tokens, and is repeatedly used by agents through multi-turn dialogue and frequent tool calls, a new bottleneck emerges: the long-term retention, cross-turn reuse, and frequent movement of global KV cache.
Computation can be sparsified and compressed, but cache does not disappear on its own. Every doubling of context increases memory footprint, memory bandwidth consumption, and inter-node data movement in a roughly linear fashion. In high-concurrency inference services, KV cache often consumes far more storage resources than the model weights themselves, and schedulers spend much of their time swapping cache in and out rather than performing actual matrix operations.
Therefore, evaluating whether a long-context solution is advanced cannot rely only on “how much computation it sparsifies.” It must also determine whether the solution simultaneously reduces three types of cost:
- Compute cost: how many parameters are activated per token during Prefill and Decode;
- Storage cost: how many bytes of KV data must be persisted per token;
- Transfer cost: the bandwidth overhead of moving KV data among HBM, host memory, and across nodes.
A truly effective solution must co-design all three rather than rely on a single-point trick.
Specifications First: The Design Philosophy of Asymmetric Activation
Before discussing mechanisms, it is useful to clarify the typical specification profile of this class of solution, since all subsequent optimizations revolve around it:
- The backbone uses a mixture-of-experts (MoE) architecture with total parameter scale in the hundreds of billions;
- A separate “conditional memory” module is added and does not consume the backbone activation budget;
- The context window supports up to the 1M-token scale;
- It supports image and text input and text autoregressive output;
- Asymmetric activation: the number of activated parameters per token in the Prefill stage is significantly smaller than in the Decode stage (for example, about 8B versus 16B).
Asymmetric activation is an easily overlooked but critical design decision. Intuitively, encoding and decoding should be symmetric, but their workload characteristics are completely different: Prefill is batch-parallel computation, with the bottleneck in compute utilization; Decode is serial token-by-token generation, with the bottleneck in memory bandwidth and latency. Making the encoding side “thinner” and the decoding side “stronger” can significantly reduce the encoding cost of long prompts while keeping total throughput unchanged, while also preserving generation quality.
Architecture Core: Causal Encoder-Decoder (CED)
3.1 From Decoder-only to a Two-Segment Split
Traditional large models use a pure Decoder architecture, where every layer and every token must fully read historical KV. The CED (Causal Encoder-Decoder) approach splits dozens of Transformer layers in depth into two halves:
- The first half serves as a causal encoder: it runs only during Prefill, reads the full prompt sequence, and produces a condensed global representation;
- The second half serves as the decoder: it no longer rereads the full prompt layer by layer. Instead, it directly projects the encoder’s final hidden state into the global KV entries of the current layer, while rebuilding local sliding-window attention (SWA) KV only from the most recent tokens.
In formula form, for the l-th decoder layer:
C^l = H_mid · W^l_KV # global KV entriesZ^l = H_mid · W^l_Z # compression weights
Here, H_mid is the output of the encoder’s last layer. The global view of each decoder layer comes entirely from layer-specific linear projections of this shared representation, rather than from re-running attention over all historical tokens.
3.2 Complexity Benefits
Let the prompt length be N, the sliding window size be n_win, and the number of layers be L. The Prefill complexity of a traditional Decoder-only model is approximately O(N·L); after the CED split, it becomes:
O(N·L/2 + n_win·L/2) ≈ O(N·L/2) (when N ≫ n_win)
In other words, Prefill computation is nearly halved, and the advantage becomes more pronounced as context grows longer. More importantly, the decoder no longer maintains full global KV for every layer. Instead, it projects highly compressed global entries from a shared representation—opening the door to subsequent cache compression.
Four-Part Cache Compression: Reducing Per-Token Global KV to the Sub-KB Level
CED solves the question of “where global KV comes from.” The next question is “how global KV should be stored.” A typical collaborative compression scheme is completed by stacking four components:
4.1 Conditional Memory (Engram): Moving Static Knowledge Out of KV
A large amount of long-context content—documents, codebases, and historical conversations—is read-only during inference. The conditional memory module stores this reusable knowledge in an independently parameterized form and retrieves and activates it on demand during inference, rather than spreading all of it into the KV cache. It is not counted in the backbone activation budget, effectively attaching a “read-only memory” to the model.
4.2 Sparse Attention Enhancement (CSA-like Mechanism): Reducing Entries That Need Caching
On top of sparse attention, further entry selection is performed: only tokens with long-term value for subsequent generation enter the global KV, while the rest remain only in the sliding window and are discarded after use. This changes the “how much to store” problem from passive retention to active filtering.
4.3 Low-Precision Main KV (FP4 Quantization): Compressing the Size of Each Entry
The retained global KV is stored in 4-bit floating point. KV cache generally tolerates quantization better than weights do. With group scaling factors, FP4 can compress the per-entry size to one quarter of FP16 with almost no loss in generation quality. From an engineering perspective, attention must use fused dequantization and attention kernels to avoid “cheap storage but expensive compute.”
4.4 Bounded Replay (SWA Bounded Replay): Limiting Local Cache Growth
In multi-turn dialogue, sliding-window KV accumulates across turns. The bounded replay mechanism sets a hard cap for local KV: old local KV beyond the window is either compressed into the global representation or directly evicted, ensuring that Decode-stage cache occupancy does not grow without bound with session length.
4.5 Stacked Effect
With the four components stacked, global KV can be compressed to roughly hundreds of bytes to the sub-KB level per token. Compared with unoptimized schemes of the same generation, global KV is reduced to about one quarter, and the total KV that must be persisted is reduced to about one eighth. This means that under the same memory budget, the system can support several times more concurrent sessions or context length.
System-Side Support: Algorithms Must Be Designed Together with the Inference System
Compression algorithms can deliver benefits only with system cooperation. Several key engineering areas include:
- Paged KV management: borrowing from virtual memory ideas, allocate KV in blocks and use reference counting to support shared prefix cache across requests and avoid repeated Prefill;
- Tiered storage scheduling: hot KV resides in HBM, warm KV in host memory, and cold KV on disk or in object storage, with the scheduler prefetching ahead based on access prediction;
- Prefill/Decode disaggregated deployment: encoding instances and decoding instances scale independently, and together with the asymmetric activation architecture, each workload runs on the optimal hardware mix;
- Quantization-aware training: FP4 KV must introduce quantization noise during training or post-training; otherwise, production accuracy will degrade noticeably.
When deployed on cloud, container platforms can be used for elastic inference orchestration, and monitoring and alerting systems can track core metrics such as cache hit rate, TTFT (time to first token), and cost per token, continuously calibrating how aggressively to apply the compression strategy.
Capability Boundaries and Selection Guidance
The applicability boundary of this approach is also clear: CED’s global representation is a compressed, lossy summary, so there is theoretical information loss for tasks such as “verbatim quotation of very long source text” (for example, exactly retrieving a particular token position); FP4 KV may also be insensitive to subtle numerical differences in extreme long-range dependencies. Therefore, practical systems should retain fallback paths: requests with high precision requirements can fall back to a higher-precision cache tier, or full recomputation can be enabled for critical segments.
Overall, the future of long-context inference does not belong to any single technique. It belongs to full-stack co-design across “architecture split + external memory + cache compression + system scheduling.” Understanding which cost category each layer of design addresses is more valuable than memorizing any set of parameter numbers.
