Designing State Caches and Accelerating Prefill in Hybrid-Architecture Large Language Models
Hybrid architectures that combine full attention with linear attention or state space model layers introduce recurrent states alongside conventional KV caches. This article examines the cache-management challenges they create under long-context AI Agent workloads and presents an engineering approach based on checkpoint alignment, tiered storage, cache-aware scheduling, and pipelined persistence. In real programming-Agent trace replay, the approach delivered more than 3× higher prefill throughput and reduced p90 time to first token (TTFT) by nearly 90% compared with mainstream inference frameworks.
1. Background: Hybrid Architectures Are Becoming Mainstream
In recent years, linear attention and state space model layers have increasingly appeared in mainstream open-source large language models. Instead of relying entirely on a complete key-value (KV) cache, these layers maintain a fixed-size recurrent state to compress historical information.
A common design is to interleave full-attention and linear layers at a defined ratio, creating a hybrid architecture:
- Full-attention layers provide accurate long-range retrieval.
- Linear or state-space layers retain most sequence information with constant memory usage.
This design offers several clear benefits:
- During decoding, each new token only updates a fixed-size state instead of reading the entire historical KV cache, improving decoding speed.
- KV-cache memory consumption is significantly reduced, allowing more concurrent sessions on a single GPU.
- Memory pressure is substantially lower for long-context workloads, leaving more capacity for extended Agent trajectories.
2. Why Cache Management Is More Difficult for Agent Workloads
Typical workloads such as coding Agents and multi-turn tool calls often share extremely long prefixes, including system prompts, tool definitions, conversation history, and code context.
For pure Transformer architectures, prefix caching is relatively mature: KV blocks can be indexed by hash and reused when a match is found. Hybrid architectures challenge this assumption for three main reasons.
2.1 Recurrent States Cannot Be Arbitrarily Sliced
A linear layer’s state is the result of recursive computation. It can only be saved and restored at specific checkpoint positions; unlike KV data, it cannot be divided and reused at arbitrary block boundaries.
If checkpoint intervals do not align with prefix-cache boundaries, the system may encounter a situation where the KV cache hits but the recurrent state must still be recomputed. This creates partial reuse and undermines the benefits of prefix caching.
2.2 The Two Cache Types Have Different Lifecycles
KV caches can be managed with fine-grained paging. Recurrent states are coarser-grained: although they are smaller, they have strict alignment requirements.
Managing both types of data through the same allocator can lead to fragmentation and ineffective cache residency.
2.3 Multi-Turn Conversations Can Cause Write Amplification
Each conversation turn may add only a small amount of new content. However, a conservative state-snapshot policy can trigger repeated prefill work across turns, wasting compute resources.
In real programming-Agent trace replay, these issues can significantly erode the theoretical advantages of hybrid models. Prefill throughput becomes limited, while time to first token (TTFT) remains high.
3. Core Approach: Redesigning Hybrid State Caching
A practical way to address these issues is to treat KV prefix caching and recurrent-state caching as one integrated system rather than as two independent mechanisms.
The core design principles are as follows.
3.1 Align State Checkpoints with Prefix-Block Boundaries
When a sequence is divided into fixed-size prefix blocks, generate a recurrent-state snapshot at every block boundary.
This ensures that every prefix-cache hit also provides the corresponding recurrent state. A single cache hit can then enable complete reuse, eliminating recomputation caused by partial alignment.
3.2 Use Tiered Caching and Asynchronous Eviction
Keep frequently accessed states in GPU memory, and asynchronously move less frequently accessed states to host memory or a remote distributed cache layer.
Because recurrent states are relatively small, the cost of transferring them across nodes is much lower than recomputing an entire prefix. The engineering results indicate that even a remote CPU cache layer with hundreds of GiB of capacity can still provide significant benefits.
3.3 Make Scheduling Aware of Cache Affinity
When selecting a model replica, the scheduler should consider both prefix-cache and state-cache hit rates. Requests from the same session should preferably be routed to an instance that already holds the relevant state.
Combined with distributed state storage, this approach enables state migration and reuse across nodes.
3.4 Pipeline Prefill and Cache Persistence
During chunked prefill, asynchronously persist the KV data and recurrent state as soon as each chunk finishes computation.
This prevents storage writes from blocking the compute pipeline and allows cache construction to proceed in parallel with prefill execution.
4. Results and Engineering Validation
When replaying real programming-Agent traces on next-generation data-center GPUs, a system using the design above achieved more than 3× the prefill throughput of mainstream inference frameworks. Its p90 TTFT decreased by nearly 90% at most.
Notably, the performance benefits remained even after introducing a large external cache layer. This suggests that the primary source of improvement was a fundamental increase in cache reuse, rather than simply adding more hardware.
5. Implementation Recommendations
Teams planning to apply this approach to their own inference services can proceed in the following stages:
Quantify workload characteristics. Measure the prefix-sharing ratio across sessions, the average number of conversation turns, and context lengths. These metrics help estimate the potential value of hybrid-state reuse.
Analyze the model structure and checkpoint feasibility. Identify the model’s layer composition and determine the smallest practical alignment granularity for state snapshots.
Build tiered cache infrastructure. Use a three-level hierarchy consisting of GPU memory, host memory, and a remote cache layer. The remote layer can be implemented with general-purpose object storage or a distributed key-value store. General cloud capabilities such as object storage, container platforms, monitoring, and alerting—for example, those provided by Baidu AI Cloud—can support the elasticity and observability requirements of this type of cache infrastructure.
Run A/B tests with real trace replay. Focus on prefill throughput, p90 and p99 TTFT, and cache hit rate. These metrics provide a practical view of both performance and cache effectiveness.
6. Conclusion
Hybrid architectures introduce “state” as a new dimension in large language model inference caching, creating both additional complexity and new system-design opportunities.
Treating KV data and recurrent states as a unified resource—then aligning, tiering, and scheduling them together—can deliver substantial efficiency gains for the long-context workloads common in the Agent era.
State caching is becoming the next important battleground for inference-engineering systems.
