Redesigning State Caching for Hybrid Architectures: A Guide to Higher Prefill Throughput
This article explains why hybrid architectures that alternate full-attention layers with linear or state layers make prefix caching harder, and how a block-level joint state cache addresses the problem. It covers content addressing with range validation, asymmetric eviction, partial-hit stitching, tiered caching across device memory, host memory, and distributed storage, and cross-node reuse. It also lists the observability metrics to track and practical deployment recommendations for reducing prefill cost in long-context AI agent workloads.
1. Why Hybrid Architectures Dominate Long-Context Workloads
Recent model designs increasingly adopt a hybrid architecture that alternates full-attention layers with linear or state layers. The motivation is straightforward: full attention provides precise global information retrieval, while linear layers (in forms such as state space models and gated linear recurrences) replace the growing key-value cache with a fixed-size recurrent state, reducing memory usage and improving computational efficiency during decoding.
In workloads like AI coding assistants, however, the trade-off reverses. A single task often involves dozens of rounds of tool calls, code retrieval, file reads and writes, and result feedback, with context length expanding rapidly from a few thousand tokens to hundreds of thousands. At that point, the dominant cost is no longer decoding but prefilling: every new request must recompute the entire historical prefix, which stretches time to first token (TTFT) and consumes large amounts of GPU compute on redundant work.
2. Why Prefix Caching Is Harder for Hybrid Architectures
Prefix caching semantics are relatively simple for pure attention models: the cache stores key-value tensors arranged by position, and a hit lets the system skip attention computation for the corresponding positions. Hybrid architectures maintain two kinds of state at once, and their reuse rules differ completely:
- Attention state: strongly bound to position, growing linearly with sequence length; a hit requires the prefix to match token by token exactly.
- Linear layer state: a fixed-size recurrent state that is repeatedly overwritten, semantically equivalent to a “compressed summary” of the prefix. It does not grow with length, but once written, intermediate results cannot be retrieved from the cache “by position.”
This creates three engineering challenges:
- Granularity mismatch: the fixed-size state has the “block” as its smallest unit of reuse, while request prefixes often fork at arbitrary positions, making partial hits hard to exploit.
- Dual hit validation: both caches must be checked for consistency; otherwise state mismatches degrade output quality and are difficult to diagnose.
- Conflicting capacity and eviction policies: the two caches differ greatly in capacity elasticity, so a unified LRU policy inevitably sacrifices one for the other.
3. Redesign Approach: A Block-Level Joint State Cache
One proven direction is to abstract both kinds of state into “block-level state objects” and rebuild the cache hierarchy around them. A schematic data structure looks like this:
class StateBlock:block_id: str # Hash of prefix content, including position rangekv_shard: Tensor # Attention key-value shardlinear_state: Tensor # Fixed-size recurrent state snapshotseq_range: tuple # Covered token rangeref_count: int
Key design points include:
- Content addressing plus range validation: locate entries by “prefix hash + starting position” together, avoiding false hits across different branches.
- Asymmetric eviction: evict remote attention blocks first under memory pressure, while the small linear state can remain resident long term and act as a “summary index.”
- Partial-hit stitching: restore the fixed-size state from the most recent hit block, then perform incremental prefilling only on the unmatched tail, compressing redundant computation to the minimum.
- Tunable block size: blocks that are too small incur high metadata overhead, while blocks that are too large lower the hit rate, so the size must be calibrated against the fork-position distribution of the workload.
4. Tiered Caching and Cross-Node Reuse
When a single machine’s memory is insufficient, the state cache must extend to host memory and remote storage, forming a three-tier hierarchy:
- Device tier: hot blocks that skip prefilling directly;
- Host memory tier: capacity on the order of hundreds of GiB, holding warm blocks;
- Distributed storage tier: shared across nodes, so the same long prefix can be reused by multiple replica nodes.
Cross-node reuse requires state objects to be serializable and versioned, with support for reference counting and consistency validation. Two engineering points also matter: first, balancing transfer volume against benefit—fixed-size state is cheap to transfer while attention shards are large, so an admission threshold should be set; second, routing affinity—scheduling requests that share a prefix to the same node can significantly improve hit rates.
5. Sources of Gains and Observability Metrics
In real agent trajectory replays, this kind of design can achieve several times the prefill throughput of a native inference framework, with a marked drop in p90 TTFT, and the gains persist after attaching a large-capacity memory cache. The benefits come from three places: repeated prefixes are skipped, incremental prefill length is shortened, and prefix alignment among requests in a batch improves.
It is worth continuously tracking these metrics: prefix hit rate, average incremental prefill length, state cache residency rate, p50/p90/p99 TTFT, and compute cost per thousand tokens.
6. Deployment Recommendations
- Measure the prefix fork distribution of real workloads first, then decide the block size;
- Prioritize keeping the linear state resident and use it as an “index tier”;
- Design a namespace for cache keys that includes the model version and configuration fingerprint, preventing false hits caused by version drift;
- When building tiered caching on top of container platforms and object storage, cover hit rate and latency jitter with a monitoring and alerting system.
For teams using Baidu AI Cloud infrastructure, this pattern maps directly onto general-purpose capabilities: container platforms host inference replicas, object storage holds cold state, and monitoring and alerting ensure observability—bringing the prefill cost of long-context agent workloads into a reasonable range without changing the model architecture.
