Optimizing State Caching for Hybrid Architecture LLMs: Improving Prefill Throughput
Hybrid models that alternate full attention layers with linear attention layers reduce decoding cost, but their recurrent states are more complex to cache than KV cache. This article explains why prefix caching for linear states is difficult in agent workloads. It outlines three design directions: treating states as first-class cache objects, optimizing chunked prefill, and extending storage across CPU memory and distributed storage. It also gives practical steps for auditing prefill cost and managing state cache.
1. Background: Why Hybrid Architectures Are Becoming the New Mainstream for LLM Inference
Over the past year, LLM inference has shifted noticeably toward a new architecture. More high-performance open-source and proprietary models no longer use a pure Transformer full-attention structure. Instead, they alternate full-attention layers with linear attention layers. The basic design works as follows:
- Key layers keep full attention: Some layers, for example one every few layers, use standard Softmax attention to model precise long-range dependencies.
- The remaining layers use linear attention or state-space mechanisms: These layers compress historical information into a fixed-size recurrent state. During decoding, the model does not need to revisit the full token history, which greatly reduces per-step compute and KV cache usage.
The benefits are direct: higher decoding throughput, a much smaller KV cache, and less memory pressure for long-context inference. This makes the design especially suitable for the most common workloads today: coding agents, multi-turn tool calling, and long-document analysis. These scenarios often involve contexts of hundreds of thousands of tokens and repeated multi-turn exchanges.
However, the trade-off is an engineering challenge that has been underestimated for a long time: prefix cache management for hybrid architectures is far more complex than for pure Transformers. This is the core problem this article examines.
2. Problem Analysis: Why Cache Management Is Harder in Hybrid Architectures
2.1 Prefix Caching Is Naturally Reusable in Pure Transformers
In a pure Transformer, the KV vector of each token depends only on its prefix. Therefore:
- Requests with the same prefix can share the KV cache.
- In multi-turn conversations, the KV from the previous turn can be reused directly. A new request only needs to prefill the incremental tokens.
- The cache lifetime is bounded by token position, which gives the cache a clear chunked structure.
This mechanism is already mature in mainstream inference frameworks, known as paged attention and automatic prefix caching.
2.2 Linear Layers Introduce Globally Coupled State
The problem with hybrid architectures lies in the linear attention layers. A linear layer maintains a recurrent state, usually a matrix with a fixed shape, which updates as the token sequence advances. This state has several properties that differ sharply from KV cache:
- The state is coupled across the entire sequence: The state after the Nth token is one whole determined jointly by the first N tokens. It cannot be split and shared at token granularity the way KV cache can.
- The number of states scales with the number of linear layers: The state of a single layer may be small, but linear layers make up most of the model’s layers, so the total state size cannot be ignored.
- Reuse is only possible at the end of a prefix: Two requests can reuse the state at the end of a prefix only when their prefixes match exactly. Once they diverge, the state must be recomputed from scratch or stored separately.
2.3 Agent Workloads Amplify the Conflict
A typical coding agent trajectory consists of a very long system prompt, project context, and tool call results appended over multiple turns. This means:
- Requests share very long prefixes, so the potential benefit of cache hits is large.
- Each tool call appends content at the tail, so the state at the end keeps changing.
- If cache management is poor, every turn requires re-prefilling the entire prefix. The recomputation cost of linear states can quickly erase the decoding speedup that hybrid architectures provide.
In other words, the savings hybrid architectures achieve on the decoding side may be paid back twice over on the prefill side. Most existing inference frameworks apply fine-grained prefix caching only to KV cache. For linear states, they generally either recompute per request or cache entire segments at coarse granularity. This leaves a clear performance gap.
3. Solution: Redesigning Hybrid State Caching
To address these problems, the FlashStates work from a research team proposes a systematic solution. Its core ideas can be summarized in three layers. The specific implementation belongs to that research work, but these design principles are useful for any team building its own inference service.
3.1 A Unified Cache View for States and KV
The first principle is: do not treat linear states as second-class citizens. In traditional implementations, KV cache has mature paging, indexing, and eviction mechanisms, while linear states are often just temporary tensors inside a request object. The improved approach elevates states to first-class cache objects on the same level as KV cache:
- Maintain checkpoint states for each sequence, meaning linear layer states persisted at certain token boundaries.
- During prefix matching, match KV blocks and state checkpoints together. When both hit, computation can resume directly.
- The granularity of state checkpoints can be tuned to balance memory overhead against recomputation cost. The denser the checkpoints, the shorter the prefix that must be recomputed after a divergence.
An illustrative cache hit logic is shown below:
# Pseudocode: prefix matching for hybrid architecturesdef match_prefix(request):kv_hit_len = kv_cache.match(request.token_ids) # Match KV blocksstate_hit = state_cache.find_checkpoint(prefix_len=kv_hit_len # Align checkpoint with the end of the KV hit)if state_hit:# Resume prefill directly from the checkpoint state, skipping the completed partresume_from = state_hit.token_posinitial_states = state_hit.linear_stateselse:resume_from, initial_states = 0, Nonereturn resume_from, initial_states
3.2 Optimizing the Prefill Computation
The second layer of optimization concerns how prefill itself is executed. Although linear attention is mathematically recurrent, it has a parallel chunked computation form. The sequence is split into blocks, and blocks are processed serially by passing accumulated states between them. Computation within each block is highly parallel. Combined with operator fusion and memory layouts tuned for hybrid architectures, hardware utilization during prefill can improve substantially. Research results show that under a real agent trajectory replay workload, these optimizations raise prefill throughput to more than 3x that of traditional approaches. They also reduce p90 time to first token (TTFT) by up to nearly 90%.
3.3 Tiered Storage and Cross-Node State Reuse
The third layer extends the storage architecture. For agent workloads with highly stable prefixes, moving state checkpoints down to CPU memory or even distributed storage can overcome the capacity limits of single-machine GPU memory:
- Hot tier: GPU memory, which stores the KV cache and latest states of active requests.
- Warm tier: A large CPU-side cache. A common industry practice is to attach a memory cache pool of hundreds of GiB. This tier stores state checkpoints from recent sessions.
- Cold tier: Distributed object storage, which enables cross-node reuse of shared prefixes, such as long system prompts and codebase context that the same team uses repeatedly.
Combined with quantized or low-rank compression of states, the cost of transferring and storing state checkpoints can be reduced further. Existing solutions have shown that the benefits remain significant when hundreds of GiB of CPU cache are integrated. They also support distributed storage and cross-node reuse of hybrid states. This means multiple replica inference clusters can share the computed results of the same expensive prefix.
4. Practical Recommendations for Engineering Teams
Based on the above, here are several actionable recommendations:
- Audit your prefill cost: Use agent trajectory replay to collect the TTFT distribution. If p90 is noticeably higher than the mean and the prefix hit rate is low, cache management is likely the bottleneck.
- **Set
