Redesigning State Caching for Hybrid Architectures to Improve Prefill Throughput
This article explains why hybrid architectures that combine full attention layers with linear layers make prefix caching difficult, and how to redesign state caching to improve prefill throughput. It covers content-addressed prefix identification, decoupled storage and joint scheduling for key-value caches and recurrent states, tiered caching with prefetching, and batching strategies for tail latency. It also outlines observability metrics and practical deployment suggestions for serving inference workloads.
1. The Problem: Hybrid Architectures Are Mainstream, but Cache Management Is the Bottleneck
Recent model designs increasingly adopt a hybrid stack of full attention layers and linear layers. Linear layers—such as state space models, linear attention, and gated recurrent structures—have inference costs that do not grow linearly with context length, which makes decoding faster and reduces cache usage. Keeping a small number of full attention layers preserves long-range precise retrieval and in-context lookup. Combining the two is a trade-off between decoding efficiency and context capability.
The problem is that this trade-off shifts complexity from computation to cache management. A pure attention model has only one form of prefix cache: key-value pairs organized by token. The slicing granularity aligns naturally, and a hit can be reused directly. A hybrid architecture, however, maintains two types of state at the same time:
- Key-value cache: grows with sequence length, can be appended per token, and can be sliced into blocks and evicted.
- Recurrent/linear state: a fixed-size matrix or vector per channel that encodes the entire history from the beginning to the current position and cannot be split by token.
As a result, the same prefix has completely different lifecycles, migration costs, and invalidation conditions in the two caches. This conflict is amplified in coding agent workloads: system prompts, repository context, and tool definitions form a very long shared prefix, while multi-turn tool calls keep appending new content. The prefix reuse rate should be extremely high, but hybrid state makes a “hit” no longer cheap.
2. Why Multi-Turn Long Contexts Break Prefix Caching
Each request in a typical agent trajectory consists of a “stable prefix plus a new segment.” Ideally, the stable prefix only needs to be computed once. In engineering practice, however, common causes of invalidation include:
- Granularity mismatch: the minimum reusable unit of linear state is much larger than a token block, so block-based management creates substantial internal fragmentation.
- Asymmetric swap-in and swap-out: a key-value cache can move only the missing tail blocks, but once linear state is evicted, swapping it back in requires replaying the entire history.
- Scheduling mismatch: request-level scheduling cannot express “partial prefix hits,” leading to repeated prefilling.
- Capacity contention: the two caches share GPU memory and host memory bandwidth. Without unified accounting, one type can overflow while the other sits idle.
The conclusion is straightforward: under a hybrid architecture, the cache must use a “unified prefix” as its logical unit and “tiered media” as its physical carrier, and must define migration semantics separately for the two types of state.
3. Core Ideas of the Redesign
3.1 Unified Prefix Identification and Content Addressing
Compute a content-bound identifier for each prefix (for example, a hash chain based on block content) so that different requests and different nodes can align on the same prefix. Illustration:
prefix_id = H( parent_prefix_id || hash(block_tokens) || model_variant )
Here, model_variant isolates states across different models and different parallel slicing configurations to avoid false hits.
3.2 Decoupled Storage and Joint Scheduling for the Two State Types
- Key-value cache: organized by block, supporting sparse swap-in.
- Linear state: organized by “checkpoints,” with a snapshot persisted every fixed number of tokens. On swap-in, replaying a small number of tokens from the nearest checkpoint is enough to restore it.
The scheduler maintains a unified block table that records each block’s location across media tiers and the availability of both state types. A hit requires both state types to be available simultaneously; otherwise, it degrades to partial recomputation.
3.3 Tiered Caching and Prefetching
GPU memory holds only hot prefixes; the warm tier resides in host memory; the cold tier falls back to external storage or a distributed cache. Because the next-turn prefix of an agent trajectory is highly predictable, the required prefix for the next turn can be prefetched asynchronously during the current decoding phase, hiding migration latency within decoding time.
3.4 Batching for Tail Latency
Prefill is a compute-intensive phase. Mixing it with decoding in the same batch raises the p90 of time to first token. Viable strategies include limiting the prefill budget per batch, pipelining long prefixes in chunks, and prioritizing short requests in the queue.
4. Observability and Validation
Optimizing the cache requires being able to measure it first. At a minimum, collect: prefix hit rate, hit share by media tier, number of replayed tokens, bytes swapped in and out, prefill queue time, and p50/p90/p99 of TTFT. When stress-testing with replayed real agent trajectories, fix the random seed and concurrency curve, and report “cold start” and “steady state” results separately to avoid overestimating gains from cold-start results.
5. Deployment Suggestions
If you use Baidu AI Cloud’s container platform to host inference services, you can make the model service stateless and deploy the cache layer independently, use object storage for cold-tier persistence, use a message queue for prefetch event distribution, and build the metrics system above with logging and monitoring alerts. The core principle is: separate cache from computation, make state migratable, and make hits explainable. The efficiency gains from a hybrid architecture ultimately depend on whether the cache system can manage the “two types of state” as first-class citizens.
