Optimizing State Caching in Hybrid Architectures: 3x Prefill Throughput for AI Agents
This article examines why hybrid architectures combining full attention with linear or state-space layers struggle under coding agent workloads, where prefill—not decoding—becomes the bottleneck. It explains a unified hybrid state caching design based on three ideas: unified addressing with separated storage, checkpointing with bounded incremental rollback, and upfront hit detection. Engineering practices covering memory tiering, transfer-compute overlap, quantization, eviction scoring, and observability are also covered, along with reported results of roughly 3x prefill throughput and near 90% lower p90 time-to-first-token, plus the workload characteristics that determine whether the gains materialize.
1. Why Hybrid Architectures Struggle with Agent Workloads
Over the past two years, mainstream LLM architectures have followed a clear evolutionary path: keep full attention in most network layers while interleaving linear attention, gated linear units, or state-space layers. The result is a hybrid structure of “full attention plus linear layers.” The motivation is straightforward—inference cost in linear layers grows roughly linearly with sequence length, and decoding only needs to maintain a fixed-size state at each step, which substantially reduces memory footprint and speeds up decoding.
The problems surface once the workload shifts from single-turn question answering to something like a coding agent. Coding agents are characterized by multi-turn tool calls, very long system prompts and code context, frequent branching and backtracking, and repeated reuse of the same prefix within a single session. In this setting the bottleneck is usually not decoding but prefill: every turn has to feed tens of thousands or even hundreds of thousands of tokens of history back into the model.
For a pure Transformer, prefix caching semantics are clear. The cache is organized in token blocks, a hit lets you skip computation for the corresponding block, and prefixes are strictly append-only. Hybrid architectures break that assumption for three reasons:
- Inconsistent state shapes. Attention layers cache key-value pairs that grow with length, while linear layers cache a fixed-size recurrent state. The two have completely different lifecycles, eviction policies, and serialization formats.
- Mismatched hit granularity. A linear state is a compressed global summary and cannot be sliced and reused at exact token offsets the way key-value blocks can.
- Expensive backtracking and forking. Agents often need to drop the last few turns and retry with a different tool argument. Attention caches can simply be truncated, but once a linear state has been contaminated by subsequent tokens it is hard to roll back.
2. Core Idea: Redefine “Caching” as Hybrid State Management
A representative piece of work on this problem (jointly proposed by university and cloud-side research teams) argues against reusing the cache abstraction from pure attention models on hybrid models. Instead, it designs a unified hybrid state caching layer. The key abstractions come down to three points.
First, unified addressing with separated storage. Logically, addressing still uses “prefix block plus block hash” for global lookup, keeping hit determination consistent across requests and sessions. Physically, states are pooled by type: the attention part is appended block by block, while the linear part is snapshotted periodically as checkpoints. Sketch:
class MixedStateEntry:block_hash: bytes # Unified addressing key covering the prefix token blockkv_blocks: list[Block] # Attention layers: incremental block-level hitslinear_ckpt: StateTensor # Linear layers: fixed-size snapshot with version numberversion: int # Used for version alignment during rollback and forking
Second, checkpoints plus incremental rollback. Snapshots of the linear layer state are taken at fixed intervals (for example every N blocks) and tagged with a version number. When an agent backtracks, the linear state only needs to be restored to the most recent checkpoint not beyond the target position, then a small number of tokens are replayed; the attention cache is truncated directly by block. This turns “not rollback-able” into “bounded replay.”
Third, upfront hit determination. Before actual scheduling, a lightweight metadata index (a prefix tree of block hashes) probes the reusable length, so state is loaded only for the portion confirmed to hit, avoiding wasted bandwidth on partially matching blocks.
3. Engineering Implementation Notes
- Memory tiering: keep hot checkpoints and recent blocks in GPU memory, warm data in host memory, and cold data in a remote cache or object storage, migrating adaptively based on hit frequency.
- Overlap transfer with computation: pipeline state loading asynchronously with decoding of the previous batch to hide loading latency.
- Quantization and compression: store linear states at lower precision and compress key-value blocks at block level, trading controllable accuracy loss for capacity.
- Eviction policy: LRU alone is not enough; use a weighted score combining prefix reuse frequency and recomputation cost—the more expensive a state is to recompute, the more it should be kept.
- Observability: track attention hit rate, linear checkpoint hit rate, number of replayed tokens, and load wait time separately; otherwise the source of the gains cannot be identified.
4. Results and Applicability Boundaries
In experiments replaying real coding tool-call traces on a single high-end GPU, this kind of redesign achieved roughly 3x the prefill throughput of a conventional inference framework, with p90 time-to-first-token dropping by nearly 90%. The gains held after adding host memory caching on the order of hundreds of GiB, and extended further to distributed caching and cross-node state reuse.
The benefits depend heavily on workload characteristics: agent scenarios with long prefixes, high reuse, and frequent forking gain the most, while short requests and low-reuse batch offline jobs see limited improvement. For production adoption, it is advisable to first measure hit rate and recomputation cost offline, then decide whether the checkpoint overhead is worth it.
5. Deployment in the Cloud
To reproduce similar capabilities in a cloud environment, you can compose general-purpose building blocks: a container platform to host inference and caching services, object storage for cold state persistence, a message queue to decouple state loading tasks, and logging plus monitoring and alerting to track hit rates and latency percentiles. Baidu AI Cloud offers corresponding capabilities across these general components, which can be assembled as needed without relying on any specific proprietary interface.
