Rebuilding State Caching for Hybrid-Architecture LLMs: Prefix Cache Management for Attention and Recurrent States
This article examines why prefix caching becomes difficult in hybrid LLMs that stack full-attention layers with linear layers, especially under multi-turn coding agent workloads. It breaks down the conflicts between attention caches and recurrent states across granularity, invalidation propagation, and storage tiering, then presents a scheduling approach based on unified addressing, checkpointing, layered storage media, and atomic commits. It also explains the sources of engineering gains, outlines a rollout sequence, and lists common pitfalls such as checkpoint interval tuning, cross-node version consistency, and rebuild-cost-aware eviction.
1. Background: Why Hybrid Architectures Complicate Prefix Caching
A growing number of large language models now stack full-attention layers together with linear layers. The design goal is clear: keep a small number of full-attention layers for precise long-range retrieval, and rely on a large number of linear layers for faster decoding and a smaller cache footprint. In architectural terms, this is a trade of a limited amount of expressive power for system efficiency.
The problems surface once the workload shifts from single-turn question answering to coding agents. Interactive coding tools typically involve multiple tool calls, very long system prompts, repeatedly appended code context, and extensive branch retries. These patterns make prefixes highly repetitive within the same session, which should be ideal for prefix cache reuse. However, in a hybrid architecture the cache is no longer a single key-value store. It consists of two coexisting forms—an attention cache and a recurrent state—with completely different lifecycles, addressing schemes, and eviction costs.
The difference can be understood this way: the attention cache is randomly addressable by position, so any prefix segment can be hit independently. The recurrent state of the linear layers evolves sequentially along a path, more like the result of a rolling accumulation. Once an intermediate state is discarded, it can only be recovered by recomputing from the beginning. This is the root cause of the increased difficulty of prefix cache management for hybrid models.
2. The Core Conflict: Inconsistent Semantics Between the Two State Types
Before rebuilding the cache, it helps to state the conflict precisely. There are usually three points of tension in engineering practice.
First, a granularity conflict. The attention cache can be split into blocks and reused independently; the smaller the block size, the higher the hit rate. The recurrent state is inherently sequence-level, and forcing fine-grained splitting produces a large number of unusable intermediate states.
Second, an invalidation propagation conflict. If part of the prefix is modified—for example, when a tool result is written back—the attention cache only needs to recompute the blocks after that segment, while the recurrent state must replay the entire evolution process, with the cost growing linearly.
Third, a capacity and medium conflict. The attention cache is large but can be swapped in and out block by block. The recurrent state is small but cannot be sharded; placing it on high-latency media directly slows down the critical path.
3. Rebuilding Approach: Scheduling Hybrid State as a First-Class Citizen
In response to these conflicts, the more mainstream rebuilding direction in the industry can be summarized in four points. The scheduling skeleton below is expressed as illustrative pseudocode:
# Illustrative: layered scheduling skeleton for hybrid state (not a real API)def prefill(request, cache):prefix = match_longest_prefix(request, cache) # 1. Longest prefix matchattn = cache.attn_blocks(prefix) # Attention blocks: reused via random addressingstate = cache.roll_state(prefix) # Recurrent state: restored from checkpointsif state is None: # 2. Checkpoint fallbackstate = replay_from_nearest_checkpoint(request)new_attn, new_state = model.forward(request.suffix, attn, state)cache.commit(prefix + request.suffix, new_attn, new_state) # 3. Atomic commitreturn new_state
First, unified addressing. Build one prefix identity scheme for both state types (content-hash-based block keys plus the session path), so the scheduler faces a single index space instead of two separate sets of logic.
Second, checkpointing. Take snapshots of the recurrent state at fixed intervals along long prefixes, turning the cost of being “unshardable” into “rollback at a fixed step size,” trading a small amount of storage for a bounded recomputation range.
Third, layered media. Keep the attention blocks under active inference and the most recent checkpoints in the hot tier, session-level recurrent states in the warm tier, and block data from historical traces in the cold tier. Swap-in and swap-out follow a “critical path first” policy rather than simple least-recently-used.
Fourth, atomic commits. In multi-turn scenarios there are many concurrent branches, so cache writes must be bound to the prefix version to prevent partially built states from being hit by later requests. This problem is especially fatal in distributed deployments.
4. Where the Engineering Gains Come From
Public experiments replaying real coding agent traces on GPU clusters show that the rebuilt hybrid state cache can raise prefill throughput to roughly three times that of a general-purpose inference framework, while p90 time-to-first-token drops by nearly 90%. The gains still hold after connecting an external memory cache on the order of hundreds of gigabytes. The mechanisms behind these numbers are not mysterious:
- Higher hit rate: fine-grained block keys let repeated system prompts and code context be reused repeatedly, substantially reducing the bytes that prefill actually needs to compute.
- A bounded recomputation range: checkpoints compress the worst-case replay length from “the entire session” to “one interval.”
- The critical path is not slowed down: the recurrent state is small and stays resident in the hot tier, avoiding slow media for high-frequency small objects.
- Better tail latency: p90 improves most noticeably because long-tail requests are precisely the workloads with very long prefixes and repeated branch retries.
5. Rollout Advice and Common Pitfalls
To validate a similar approach on your own inference service, proceed in this order: first measure the prefix repetition rate and branch depth of real workloads, then decide the block size and checkpoint interval; next compare hit rate and time-to-first-token using shadow traffic; only then introduce external caching and cross-node reuse.
There are three common pitfalls. First, too small a checkpoint interval inflates storage, while too large an interval eliminates the recomputation gains, so it must be tuned against the session length distribution. Second, cross-node reuse of the recurrent state requires version consistency; otherwise silent errors that answer the wrong question will appear. Third, cache eviction should not look at capacity alone; the “rebuild cost” should be included in the cost function.
Overall, the efficiency bottleneck of hybrid-architecture models has shifted from the decoding side to state management on the prefill side. Unified modeling, layered scheduling, rollback capability, and cross-node reuse of the attention cache and recurrent state are currently the most cost-effective optimization path in engineering. With general-purpose capabilities such as container platforms, object storage, and monitoring and alerting on platforms like Baidu AI Cloud, this kind of caching system is also easier to validate through canary releases and observe in production.
