Refactoring State Caching for Hybrid Architecture LLMs: Tripling Prefill Throughput
This article explores the challenges of state management in hybrid architecture large language models (LLMs) that combine full attention and linear layers, particularly under agentic workloads. It details why traditional prefix caching fails for these models and introduces a new hybrid state caching approach. By implementing layered state representation, state-aware scheduling, and multi-tier distributed caching, engineering teams can significantly improve cache hit rates, reduce Time to First Token (TTFT), and achieve up to three times higher prefill throughput in multi-turn agent applications.
Background: Why Hybrid Architecture Models Are Becoming Mainstream
In recent years, several mainstream LLMs have adopted a hybrid architecture combining full attention layers and linear layers. In this design, some layers retain the standard self-attention mechanism, while others utilize linear attention or state space structures. This approach yields two direct benefits:
- Faster decoding: The computational complexity of linear layers scales linearly with sequence length rather than quadratically. This significantly reduces the computation required per generated token in long-context scenarios.
- Smaller KV cache: Linear layers maintain a fixed-size recurrent state instead of storing full Key and Value tensors for every historical token like attention layers. In memory-constrained deployments, hybrid architectures can support longer contexts or higher concurrency.
However, the flip side is that state management in hybrid models is far more complex than in pure attention Transformers. This complexity is highly sensitive to emerging workloads like intelligent coding agents.
Problem Analysis: Why Prefix Caching Fails Under Agent Workloads
Intelligent coding tools (such as code generation and repair agents) operate with distinct characteristics: multi-turn interactions, long contexts, and strong prefix reuse. A single programming task often involves dozens of conversation rounds, with each round appending tool call results and model outputs to an ever-growing common prefix.
For pure attention models, prefix caching is straightforward: as long as a new request’s prefix matches a cached prefix token-by-token, the pre-computed KV cache can be reused, skipping redundant prefill calculations. This match can occur at any token boundary.
Hybrid models, however, break this assumption. The recurrent state in linear layers has two inherent properties: sequential dependency and indivisibility.
- States are only valid at specific positions: A recurrent state is the cumulative result of processing up to a certain position. You cannot extract an intermediate state at an arbitrary token boundary unless the inference engine explicitly saved a snapshot there.
- Coarse state granularity: To reduce storage overhead, inference frameworks typically save linear layer states only at sparse checkpoints, such as every 256 or 1024 tokens. This means prefix reuse can only occur at checkpoint boundaries. Even if KV caches between checkpoints hit, the state must still be recalculated.
- Two-level cache coordination is difficult: The KV cache of attention layers and the state cache of linear layers differ entirely in lifecycle, size, and reuse granularity, making unified management highly challenging.
The result is that in traditional inference frameworks, the prefix cache hit rate for hybrid models drops drastically under agent workloads. Massive amounts of reusable prefixes are forced to undergo prefill recalculation, degrading both prefill throughput and Time to First Token (TTFT). In multi-turn long contexts, this waste compounds with each round—the 30th conversation turn might require recalculating nearly all content from the previous 29 turns.
Core Concept: Redesigning Hybrid State Caching
To address these issues, a new caching management approach (referred to here as the “hybrid state caching solution”) reconstructs the caching system for hybrid architecture models across three dimensions.
1. Layered State Representation
The solution explicitly divides the cache into two types of objects: KV blocks for attention layers and state snapshots for linear layers, establishing unified indexing and lifecycle management for both. KV blocks are stored in chunks of a fixed token count for fine-grained matching, while state snapshots are anchored to block boundaries. This ensures that any KV block boundary corresponds to a linear layer state that can be directly restored.
The key benefit here is that the minimum granularity for prefix reuse is reduced from the “checkpoint interval” to the “block size,” meaning cache hits are no longer restricted by sparse checkpoints.
2. State-Aware Scheduling
When a request arrives, the scheduler no longer solely searches for KV prefix matches. Instead, it performs a joint “KV + State” match: it finds the longest prefix that simultaneously possesses a valid state snapshot, restores the full model state from that position, and then only executes prefill on the incremental portion. The scheduler also evaluates the restore cost based on the storage location of the state snapshot (GPU memory, host memory, or remote cache) to make the optimal choice between “recalculating” and “transferring.”
3. Multi-Tier Caching and External Storage
GPU memory capacity is limited, restricting the number of state snapshots a single node can cache. The solution supports tiered offloading of state snapshots: hot data stays in GPU memory, warm data goes to host memory, and cold data is written to a distributed CPU cache layer. In large-scale experiments, after integrating an external CPU cache at the hundreds of GiB level (e.g., 512GiB), the state reuse benefits for long sessions remained significant. It also supports cross-node hybrid state reuse—session state generated on one machine can be directly utilized by subsequent requests on another machine. This is crucial for horizontally scaling inference clusters.
Performance Evaluation
In tests replaying real intelligent coding tool session traces on the latest generation of datacenter GPUs, the solution achieved the following results:
- Prefill throughput reached up to 3.14 times the baseline of mainstream open-source inference frameworks.
- p90 Time to First Token (TTFT) was reduced by up to 88.4%.
- After introducing large-capacity external CPU caching, the benefits remained stable without significant degradation from state swapping.
It should be noted that these data points come from experiments under specific hardware, workloads, and model configurations. Actual gains will vary based on session length distribution, turn density, and cache capacity. However, the trend is clear: improvements in state reuse granularity directly translate to reduced prefill computation.
Engineering Implementation Recommendations
Teams building or optimizing LLM inference services can draw insights from the following aspects:
- Evaluate Workload Characteristics: If your business primarily consists of short, single-turn Q&A, the benefits of prefix caching are limited. For workloads with strong prefix reuse, such as agents, multi-turn dialogues, and document continuation, the ROI of hybrid state management is very high.
- Optimize Snapshot Intervals: Denser snapshots offer finer reuse granularity but higher storage overhead. You can select block size and snapshot frequency based on typical incremental lengths (e.g., an agent adding a few hundred tokens per turn).
- Build a Multi-Tier Cache System: Combine GPU memory, host memory, and distributed cache, paired with LRU or session-affinity-based eviction policies. For cloud deployments, general cloud capabilities like Baidu AI Cloud’s object storage, distributed cache, and container platforms can be utilized to build an elastic state caching layer, while monitoring and alerting services can be used to observe cache hit rates and TTFT changes.
- Implement Session-Affinity Scheduling: Route requests from the same session to the node holding its state whenever possible, using cross-node state fetching as a fallback to reduce remote transfer overhead.
Conclusion
Hybrid architecture models have become an important direction for long-context inference due to their lower decoding costs and smaller cache footprints. However, their dual-state structure of “KV + recurrent state” poses new challenges to traditional prefix caching mechanisms. Through layered state representation, state-aware scheduling, and multi-tier distributed caching, it is possible to increase prefill throughput by approximately three times and significantly reduce TTFT under real agent workloads. As intelligent coding and agent applications proliferate, cache system design tailored for hybrid architectures will become a critical frontier in inference infrastructure competition.
