Optimizing State Caching for Hybrid Architecture Models: Boosting Prefill Throughput
Hybrid architecture large language models (LLMs) combine full attention and linear layers to balance long-context retrieval and decoding efficiency. However, managing prefix caching for multi-turn agent workloads becomes significantly more complex. This article explores the core challenges of hybrid state caching and proposes a redesigned caching system utilizing a unified block abstraction, periodic state snapshots, and hierarchical eviction to substantially boost prefill throughput.
Running Agent Workloads on Hybrid Architecture Models: Redesigning Hybrid State Caching to Boost Prefill Throughput
1. Background: Hybrid Architectures Are Becoming Mainstream
In recent years, linear attention and state space layers (such as Gated Linear Networks, Selective State Space Models, etc.) have transitioned from academic papers to become common components in open-source large models. Many next-generation models no longer rely on a pure full-attention structure but instead adopt a hybrid architecture that alternates between “full attention layers + linear layers.”
The motivation behind this design is straightforward: the KV Cache of a pure attention model grows linearly with the context length, making memory footprint and memory bandwidth bottlenecks during long-context inference. In contrast, linear attention and state space layers only maintain a fixed-size recurrent state, where the overhead per token during decoding is approximately constant, regardless of context length. Combining the two preserves the full attention layer’s ability for accurate long-range retrieval while enabling faster decoding speeds and a smaller cache size.
However, the flip side is that for multi-turn, ultra-long context workloads like coding agents, prefix cache management in hybrid models is much more complex than in pure Transformers. This is the core issue discussed in this article.
2. Why Agent Workloads Are So Demanding on Cache Systems
The typical working mode of a coding agent is to initiate a request with a long system prompt and code repository context. The model outputs a short action or tool call, the external environment executes it, appends the result to the end of the context, and initiates the next request. This loop can repeat dozens or even hundreds of times, causing the context to expand to hundreds of thousands of tokens.
This type of workload has three distinct characteristics:
- Highly Shared Prefix: The prefixes of two consecutive requests almost entirely overlap, with only a small amount of new tool return content appended at the tail. If the prefix cache can be reused, the prefill computation can be reduced by an order of magnitude.
- Incremental Growth: The context grows monotonically longer, meaning the cache must support incremental appending rather than full recomputation.
- Concurrent Multi-Session: The service side runs a large number of agent sessions simultaneously. The cache system’s capacity and eviction policies directly impact overall throughput.
For pure attention models, prefix caching is already a mature practice: the KV Cache is organized by blocks, and prefix hashing is used to match and reuse precomputed blocks. But hybrid architectures introduce new state types, making things tricky.
3. Three Core Difficulties of Hybrid State Caching
3.1 Linear Layer States Cannot Be Sliced and Reused at Arbitrary Positions
KV Cache is “position-by-position”: the cache at position i only depends on the first i tokens, so it can be truncated and reused at any block boundary. A linear layer, however, maintains a compressed state that rolls with the sequence—the state at position i is some lossy compression of all information from the first i tokens. To reuse the state at step i, a snapshot must have been saved exactly at that position; otherwise, it can only be recomputed from the beginning.
This means the prefix cache hit granularity for hybrid models is limited by “whether a linear layer state was saved at the corresponding position,” rather than being naturally block-aligned like a KV Cache.
3.2 Inconsistent Lifecycle and Capacity Characteristics Between the Two Cache Types
The KV Cache size of full attention layers grows linearly with length, but it can be chunked and evicted to host memory. The linear layer’s state is a fixed-size dense tensor; a single copy isn’t large, but each session and each save point requires a copy, which adds up as the number of sessions increases. How to uniformly schedule, evict, and determine cache hits for these two types of states is a new engineering challenge.
3.3 Different Parallelization Approaches for Linear Layers During Prefill
During prefill, linear layers typically use a chunk-wise parallel scan to leverage parallel computing power: a long sequence is divided into several chunks, computed in parallel within the chunk, and states are passed serially between chunks. If a prefix cache hit occurs in the middle of a chunk, the boundary condition of “partial intra-chunk hit” must be handled, ensuring numerical correctness without degrading into a token-by-token serial process.
4. Key Ideas for Redesigning Hybrid State Caching
To address the above difficulties, a reasonable hybrid state cache design can follow these principles.
4.1 Unified Block Cache Abstraction
Incorporate both state types into the same block-organized caching framework: each block records the token interval it covers, the full attention layer’s KV block, and a snapshot of the linear layer state at the block boundary. During hit determination, prefix hashes are matched level by level. When a hit occurs at a certain block, the KV and the linear state at the end of that block are simultaneously retrieved, and computation continues from the next block.
The schematic data structure can be organized as follows:
class HybridBlock:token_ids: list[int] # tokens covered by this blockkv_cache: Tensor # KV block of the full attention layerlinear_state: Tensor # snapshot of the linear layer state at the end of the blockprefix_hash: str # prefix hash up to this blockdef match_prefix(request_tokens, block_index):matched = 0while matched < len(request_tokens):h = hash_upto(request_tokens, matched + BLOCK_SIZE)block = block_index.get(h)if block is None:breakmatched += BLOCK_SIZEreturn matched # hit length (block-aligned)
4.2 Periodic State Snapshots and Hit Granularity Trade-offs
The more frequently linear layer state snapshots are saved, the finer the hit granularity, but the higher the memory overhead. The sparser the saves, the higher the cost of falling back to recompute on a miss. A common compromise in practice is to save a snapshot at fixed block boundaries (e.g., every 256 or 512 tokens), aligning it with the KV block boundaries to unify the hit determination logic. For agent workloads where the “tail increment is very short,” even if the hit point falls back to the nearest snapshot point, the extra computation is usually only a few hundred tokens, which is an acceptable cost.
4.3 Incremental Prefill and Tail Recomputation Pipeline
After hitting the prefix, the remaining tail executes a normal chunk-wise scan: the linear state from the cache is used as the initial state for the scan, while the KV is appended starting from the hit position. Engineering-wise, two points should be noted:
- Copy state instead of referencing: The same prefix may be hit by multiple sessions. The linear state must be copied before advancing to avoid cross-session contamination.
- Attention masks across block boundaries: When a new tail block performs full attention computation, it must correctly read the cached KV blocks to ensure the attention range covers the complete prefix.
4.4 Hierarchical Eviction and Capacity Management
Following the practices of general inference frameworks, the cache can be divided into a GPU memory layer and a host memory layer: the KV and state snapshots of hot sessions reside in GPU memory, while cold sessions are evicted based on a Least Recently Used (LRU) policy. Because the linear state is a small dense tensor, the unit cost of swapping it in and out is lower than that of large KV segments. Different priority weights can be assigned during scheduling to further maximize GPU memory utilization.
5. Expected Results and Validation Methods
With the above design, the prefill throughput for multi-turn agent workloads can be increased several times—essentially because each turn only needs to compute the newly added tail tokens, while the prefix is completely reused. During validation, it is recommended to focus on three types of metrics: prefix cache hit rate (calculated by tokens), the actual number of prefill tokens per request, and end-to-end Time-To-First-Token (TTFT) latency. At the same time, a correctness regression test is mandatory: under identical inputs, the outputs with and without caching enabled should be identical within the allowed numerical error tolerance, especially covering cases where the hit point is at the block boundary and inside the block.
6. Conclusion
Hybrid architectures have upgraded the KV Cache problem into a dual-state caching problem of “KV + recurrent state.” The core contradiction is that linear layer states cannot be sliced and reused arbitrarily, forcing the caching system to organize states using block boundary snapshots and making trade-offs between hit granularity, memory overhead, and recompute costs. By utilizing unified block abstraction, periodic snapshots, incremental prefill, and hierarchical eviction, hybrid models can enjoy the speed advantage during the decoding phase in multi-turn long-context agent scenarios while boosting prefill throughput to a level comparable to or even higher than pure attention prefix caching. When deploying such inference services in the cloud, general container platform capabilities and monitoring tools can be utilized to continuously observe cache hit rates and memory usage, supporting capacity planning and strategy optimization.
