Optimizing State Cache Management for Hybrid Architecture LLM Agent Services

    Hybrid architectures combining full attention layers with linear attention or State Space Model (SSM) layers are increasingly popular for Large Language Model (LLM) inference due to their memory efficiency and faster decoding speeds. However, they introduce significant complexities in prefix cache management for Agent workloads, which typically involve multi-turn, long-context interactions. This article explores the challenges of managing heterogeneous caches—linearly growing KV caches and fixed-size recurrent states—and presents a systematic solution. By elevating recurrent states to first-class cache citizens, introducing state checkpoints, utilizing a hybrid prefix tree, and extending to heterogeneous distributed storage, the optimized inference engine achieved up to 3x prefill throughput and reduced p90 Time-To-First-Token (TTFT) by nearly 90%.

    I. Background: Why Hybrid Architecture is the New Choice for Agent Inference

    In recent years, the design of attention mechanisms in large models has undergone structural changes. While full attention layers offer strong expressive power, their KV Cache grows linearly with context length, creating increasing memory pressure and bandwidth bottlenecks for long-context inference. To address this, the industry has begun integrating layers with fixed-size recurrent states, such as linear attention and State Space Models (SSMs), alternating them with full attention layers to form a hybrid architecture.

    This hybrid architecture has been widely adopted in the open-source community, offering several core benefits:

    • Smaller cache footprint: Linear and SSM layers only need to maintain a fixed-size recurrent state rather than a KV sequence that grows with length, drastically compressing KV Cache usage.
    • Faster decoding speed: During decoding, each step only requires updating the state, significantly reducing memory access compared to full attention layers and alleviating memory bandwidth bottlenecks.
    • Longer effective context: Within the same memory budget, longer conversations can be served.

    For typical workloads like intelligent coding assistants and code Agents—where user interactions are characterized by multi-turn, long-context, and high-frequency continuations—the decoding benefits of hybrid architectures are particularly compelling.

    II. Problem: Complexity of Prefix Cache Management in Hybrid Architectures

    However, these benefits are not without cost. In Agent scenarios, inference services typically rely on prefix caching to reuse historical context: each new turn of conversation only processes incremental content, while previously processed system prompts, historical messages, and file contents hit the cache directly, skipping most of the prefill computation.

    For pure Transformers, the target of prefix caching is the K/V tensors of each layer, which have a uniform structure and granularity, making management relatively straightforward. However, hybrid architectures introduce two fundamentally different cache objects:

    1. Full attention layers’ KV Cache: Grows linearly with the number of tokens and naturally supports prefix reuse of any length.
    2. Linear/SSM layers’ recurrent state: The entire prefix is compressed into a fixed-size state tensor, which is “indivisible”—it cannot be reused at token granularity like KV caches.

    This introduces several engineering challenges:

    • Inconsistent reuse granularity: Within a single request, attention layers can reuse up to the Nth token, while the effective reuse point for state layers depends on the checkpoint where the state was refreshed. Aligning the two is difficult.
    • Sequential dependency of state updates: The state of linear layers is calculated recursively by time step. To rebuild the state at an intermediate position, computation must be replayed from the nearest checkpoint, which is computationally expensive.
    • Complex cache invalidation and recalculation: Once a user edits intermediate content (common in Agent scenarios), invalidation handling for the state layer is much trickier than for the KV layer.
    • Unfriendly to heterogeneous storage: KV Cache already has mature CPU-side caching and distributed storage practices, whereas lifecycle management and cross-node migration for state tensors still lack unified solutions.

    These issues result in massive redundant computations during the prefill phase when replaying real Agent trajectories. The Time-To-First-Token (TTFT) degrades noticeably, and the decoding benefits at the architecture level are offset by the inefficiency of cache management.

    III. Solution: Redesigning KV and State Cache Management

    To address these issues, researchers proposed a system-level cache management reconstruction. The core concepts can be summarized as follows:

    1. Promoting Recurrent State to a First-Class Cache Citizen

    Traditional inference frameworks treat state layers as internal variables that are “updated along the way,” limiting cache management only to KV. The new approach explicitly incorporates the state tensors of each linear/SSM layer into the prefix cache system, using unified block management, reference counting, and eviction policies alongside the KV Cache. This means states can be indexed, reused, preempted, and swapped out just like KV.

    2. State Checkpoint Mechanism

    To solve the sequential dependency of state recurrence, the solution introduces state checkpoints: during prefill, snapshots of intermediate results for state layers are taken at fixed intervals. When a request is preempted, resumed, or a prefix fork occurs, the system can replay from the nearest checkpoint instead of scanning the entire context from scratch. The checkpoint interval is a tunable parameter, requiring a trade-off between snapshot storage overhead and replay computation cost:

    1. # Pseudocode for state checkpointing
    2. for chunk in chunks:
    3. state = linear_layer.recurrent(chunk, state)
    4. if chunk.idx % CHECKPOINT_INTERVAL == 0:
    5. state_cache.save(prefix_id, chunk.idx, state.clone())

    3. Hybrid Prefix Tree and Aligned Reuse

    At the cache lookup level, the solution maintains a unified prefix tree where nodes record the mapping of both KV blocks and state checkpoints. Upon a hit, alignment is based on the “shorter valid prefix” of the two, and only the missed tail segment undergoes unified incremental prefill, ensuring that only the minimal necessary content is recalculated per request.

    4. Heterogeneous and Distributed Cache Extension

    The solution further extends state caching to CPU memory and remote storage, adopting a tiered architecture consistent with KV caching: hot data resides in GPU memory, warm data in CPU-side cache, and cold data sinks to object storage-like infrastructure, with support for cross-node state reuse. This allows long-session Agents to still benefit from cache hits during instance restarts or request drifting.

    IV. Results and Validation

    When replaying real coding Agent workflows on the latest flagship GPUs, the optimized inference engine achieved up to a 3x increase in prefill throughput and a reduction of nearly 90% in p90 TTFT compared to mainstream open-source inference frameworks. Even after integrating hundreds of GiB of CPU cache and enabling distributed storage, the gains remained significant, demonstrating that the solution is not only suitable for single-node memory optimization but also scales to cluster-level multi-turn session reuse scenarios.

    V. Insights and Practical Recommendations

    For teams needing to deploy hybrid architecture models in production to serve Agent-type workloads, this work provides several transferable insights:

    1. Cache system design must cover all state forms: Any intermediate computational results that evolve with context and can be reused should be integrated into a unified cache lifecycle management system, rather than being scattered across the implementation details of individual layers.
    2. Design checkpoints for recurrent computations: For any computation with sequential dependencies, the checkpoint + replay pattern is a universal approach that balances correctness and flexibility.
    3. Tiered and distributed caching is mandatory for long sessions: GPU memory alone cannot support the session scale of Agent scenarios; a three-tier architecture of GPU—CPU—remote storage should be prioritized in planning.
    4. Benchmark with real trajectories: The prefix structure and branching frequency of Agent workloads differ significantly from synthetic tests. Cache strategies should be evaluated using real or near-real workloads.

    Overall, hybrid architectures solve the problem of “single-step computation efficiency,” while cache management reconstruction solves the problem of “cross-request reuse efficiency.” Only when the two complement each other can linear attention and state space layers truly realize their theoretical advantages in multi-turn Agent scenarios.