KV Cache and State Caching Optimization for Hybrid-Architecture LLMs

    Hybrid architectures that interleave full-attention Transformer layers with linear attention layers are becoming the mainstream choice for long-context models, but they shift complexity from the model itself into the inference system's cache management. This article explains why prefix caching breaks down under hybrid architectures, and presents a unified "KV + linear state" caching design—including first-class state snapshots, efficient recovery, and reuse strategies for multi-turn agent workloads—along with measured gains in prefill throughput and TTFT, plus practical engineering guidance.

    1. Background: Why Hybrid Architectures Dominate Long-Context Models

    In recent years, a clear architectural trend has emerged in the mainstream LLM community: interleaving traditional full-attention Transformer layers with linear attention layers (or similar state-space mechanisms) to form a “full attention + linear layers” hybrid architecture. A typical design inserts several linear attention layers after every few full-attention layers, at ratios such as 1:3 or 1:7.

    The benefits of this hybrid design are straightforward:

    1. Faster decoding. The computational complexity of linear layers is independent of sequence length. During decoding, they rely on a fixed-size recurrent state rather than a KV cache that grows linearly with context, dramatically reducing memory traffic.
    2. Smaller caches. Since full-attention layers make up only a minority, the KV cache shrinks severalfold, making long-context inference feasible in terms of memory and cost.
    3. Better length extrapolation. Linear mechanisms naturally generalize well to longer sequences, while the small number of full-attention layers preserves precise retrieval capability.

    For these reasons, many new models targeting long-context and agent scenarios have adopted this approach. However, the architectural gains are not free—they shift complexity from inside the model to the cache management layer of the inference system.

    2. The Problem: Hybrid Architectures Complicate Prefix Cache Management

    In workloads like intelligent coding assistants and coding agents, a single conversation often spans dozens of turns, and each turn’s input shares a large prefix with previous turns (system prompts, code repository context, message history, etc.). To save prefill computation, inference systems typically use prefix caching: if a new request’s prefix matches an existing one, the cached KV is reused directly, and only the incremental portion is prefilled.

    For pure Transformer models, this is relatively simple: the KV cache is organized into blocks, and a matching prefix means the corresponding blocks’ KV can be reused directly—you only need to guarantee block-boundary alignment.

    Hybrid architectures introduce two new problems:

    1. Full-attention layers and linear layers have different state shapes. Full-attention layers cache per-token K/V tensors; linear layers cache a compressed recurrent state matrix. The two differ completely in size, organization, and alignment requirements, so a prefix cache manager designed around “KV blocks” cannot be applied directly.
    2. Linear states cannot be truncated by block. A linear layer’s recurrent state is the result of sequentially scanning and accumulating the prefix—it is path-dependent: S_t = f(S_t-1, x_t). If the prefix cache hit boundary does not coincide with a linear state snapshot point, there is no way to resume from the middle—the system must either roll back to a snapshot point and re-scan, or recompute the entire segment. In real multi-turn workloads, the linear state often becomes the prefill bottleneck, producing the awkward situation where “the KV hit but the state missed, so the entire prefix is recomputed anyway.”

    Multi-turn agent workloads amplify both issues: contexts are long (often tens to hundreds of thousands of tokens), making recomputation expensive; and requests arrive in quick succession with rapidly growing prefixes, making overall latency extremely sensitive to cache reuse rates.

    3. Core Idea: Redesigning Hybrid State Caching

    To address these problems, recent research in the industry proposes unified management of “KV + linear state.” The core design can be summarized in three points:

    3.1 Bring Linear State into a Unified Cache Abstraction

    Instead of treating linear-layer state as a temporary internal tensor, promote it to a first-class cache object on par with KV. The logical unit maintained by the cache manager is no longer just a “KV block,” but a composite structure of “block + corresponding linear state snapshot.” A conceptual sketch:

    1. # Conceptual sketch: unified cache entry
    2. @dataclass
    3. class HybridCacheBlock:
    4. block_id: int
    5. # KV for full-attention layers (only some layers have this)
    6. kv: Optional[KVTensor]
    7. # Linear-layer state snapshots at this block boundary
    8. linear_states: List[StateMatrix]
    9. # Reference count and version info
    10. ref_count: int
    11. version: int

    With this design, no matter which block boundary prefill resumes from, both KV and linear state are available together, achieving consistent “all-hit or all-recompute” semantics.

    3.2 Efficient State Snapshotting and Recovery

    Although each linear state is fixed in size, snapshots across all layers still add up. The key optimizations are:

    • On-demand snapshotting: save state only at block boundaries (the alignment points where prefix cache hits may occur), rather than at every token;
    • Single-pass scan on recovery: if there is an increment after the hit point, simply continue scanning the incremental part from the nearest snapshot—no need to replay the entire prefix;
    • Decoupled storage for state and KV: state snapshots can reside in CPU memory or a distributed cache, swapped in asynchronously when needed, avoiding pressure on scarce GPU memory.

    3.3 Reuse Strategies for Multi-Turn Workloads

    In agent scenarios, prefixes grow append-only: turn N’s input is essentially turn N-1’s input plus new messages. The caching strategy can be optimized for this pattern:

    • After each turn completes, immediately persist the state at the trailing block boundary as the recovery point for the next turn;
    • For forking scenarios (multiple branches derived from the same prefix), use copy-on-write to prevent concurrent modification of state;
    • Support cross-node reuse: through a distributed cache layer, different inference instances can share state for the same prefix, so a cluster only needs to fully prefill a hot prefix once.

    4. Results and Engineering Notes

    In replay tests of real multi-turn code-editing trajectories, this unified state caching approach delivers significant gains: prefill throughput improves severalfold compared to mature general-purpose inference frameworks, and p90 time-to-first-token (TTFT) drops by more than 80%. The gains come mainly from two sources:

    1. Eliminating redundant prefill caused by state misses. In multi-turn workloads, this redundant computation often accounts for a substantial share of total prefill;
    2. Reducing memory pressure and paging overhead. Linear states are small; once brought under unified management, cache hit rates improve and overall system throughput rises accordingly.

    Several practical engineering recommendations for implementation:

    • Choose block size with both full-attention and linear layers in mind. Blocks that are too large lead to coarse incremental prefill granularity and low reuse; blocks that are too small inflate snapshot overhead. Tune based on the prefix growth patterns of your typical workload.
    • For TTFT-critical scenarios, keep state snapshots resident in CPU cache, trading bandwidth for latency; for throughput-first scenarios, limit the snapshot hierarchy.
    • Monitor KV hit rate and state hit rate separately. Divergence between the two is the most common hidden performance pitfall in hybrid-architecture systems.

    5. Conclusion

    Hybrid architectures are an important direction for long-context inference, but they impose new requirements on the inference system’s cache management: the KV of full-attention layers and the state of linear layers must be managed uniformly and consistently. Promoting linear state to a first-class cache object—supporting snapshot recovery and cross-node reuse—is the key step for hybrid models to truly deliver on their promise of “faster decoding, smaller caches” in agent-style multi-turn workloads. For teams building long-context inference services, this design approach is well worth evaluating and adapting within their own inference stacks.