Redesigning State Caching for Hybrid Architectures: How Prefill Throughput Improves
This article explains why hybrid architectures that alternate full-attention layers with linear or state-space layers make caching more complex, and why the bottleneck shifts to the prefill stage in AI Agent workloads. It covers separating attention state from linear-layer state, tiered storage, prefix fingerprinting with block-level reuse, dual-channel pipelined transfers, and cache-aware scheduling. It also provides capacity estimation formulas, metrics for measuring throughput gains, common pitfalls, and deployment guidance for general cloud environments.
1. Background: Why Hybrid Architectures Make Caching “Heavy”
Mainstream large model designs increasingly stack full-attention layers and linear or state-space layers in an alternating hybrid structure. The motivation is clear: full-attention layers handle precise long-range recall, while linear or state-space layers carry historical information in a fixed-size state. This reduces per-step computation during decoding and significantly shrinks the cache that must stay resident.
However, when these models are used in Agent scenarios, the problem quickly shifts to the prefill stage. Typical Agent workloads have three characteristics:
- Extremely long contexts: system prompts, tool descriptions, historical trajectories, and retrieved snippets stack on top of each other.
- Highly repetitive prefixes: every turn in the same session recomputes an almost identical prefix.
- High concurrency: many sessions are simultaneously in the “just submitted, waiting for the first token” state.
At this point, caching is no longer a memory-saving optimization but a bottleneck that determines throughput. Reusing a unified cache abstraction designed for pure attention models incurs three types of costs at once: memory is bound by the longest sequence, bandwidth is consumed by useless data movement, and scheduling is held back by the slowest layer. Redesigning the hybrid state cache means making structural changes targeting these three points, with gains typically showing up as order-of-magnitude improvements in prefill throughput.
2. The Nature of State in Hybrid Architectures: Two Types of State Cannot Be Mixed
Before redesigning the cache, you need to classify “state” clearly.
- Attention state (KV Cache): grows linearly with sequence length, stores a key-value pair for every token, and is accessed by random access at specific positions.
- Linear/state-layer state: its size is independent of sequence length. It is a fixed-size recurrent state, updated by sequential rolling overwrites, and accessed with whole-block reads and writes.
The two have completely different access patterns, lifecycles, and eviction costs. Putting them in the same cache pool inevitably means using a random-access interface to operate on sequentially overwritten data, causing extra copies and format conversions. The first principle of the redesign is state separation: build independent allocators, independent eviction policies, and independent transfer channels for the two types of state.
A schematic abstract interface can be expressed like this:
class StateCache(Protocol):def alloc(self, seq_id: int, length: int) -> Handle: ...def append(self, h: Handle, token_ids: list[int]) -> None: ...def commit(self, h: Handle) -> int: ... # returns the reusable prefix lengthdef evict(self, policy: str) -> list[int]: ...
The return value of commit is the key to the whole approach: it explicitly declares that “this prefix can be safely reused by subsequent requests” instead of leaving upper layers to guess.
3. Four Core Mechanisms of the Redesign
1. State Tiering and Tiered Storage
Classify state into three tiers by heat: the fastest tier inside the compute unit, the host-side memory tier, and an external storage tier (such as object storage or distributed caching, which are general cloud capabilities). Linear-layer state is small and has high reuse value, so it should stay resident in the fastest tier whenever possible. Attention state is large, so it migrates between tiers in blocks. Tiering decouples “capacity” from “bandwidth,” avoiding sacrificing hit speed for capacity.
2. Prefix Fingerprinting and Block-Level Reuse
Compute a content fingerprint for the prefix at a fixed block size (for example, a digest over the token sequence within a block). On a hit, skip prefill computation for that block. Two things need attention: blocks that are too small increase fingerprint management overhead, while blocks that are too large reduce the hit rate. You must also include the model version and configuration in the fingerprint to prevent incorrect reuse across versions.
key = hash(model_version | block_size | block_tokens)
3. Dual-Channel Pipelined Transfer
Design parallel transfer channels for the two types of state so that “computing block N” overlaps with “moving block N-1.” Because linear-layer state is read and written sequentially as whole blocks, it naturally aligns with the transfer granularity; attention state is pipelined block by block. Pipeline depth needs to match computation time; excessive depth amplifies peak memory usage.
4. Cache-Aware Scheduling
When selecting batches, the scheduler no longer looks only at sequence length but includes “prefix hit ratio, state residency location, and migration cost” in its scoring function. A practical scoring form:
score = α * hit_ratio - β * transfer_cost - γ * waiting_time
This prioritizes requests that are “almost fully hit,” maximizing effective compute utilization for prefill.
4. Estimating Capacity and Gains
Do not size capacity by intuition; use a reproducible estimation formula:
KV_bytes ≈ 2 * layers_attn * kv_heads * head_dim * bytes * tokensState_bytes ≈ layers_linear * state_dim * bytes # independent of length
This yields two engineering conclusions. First, in long-context scenarios, cache pressure comes mainly from attention state, so capacity expansion should target it first. Second, although linear-layer state is small, its size is independent of length, giving it the highest reuse value, so it is worth placing in the fastest storage tier.
For measuring throughput gains, fix three metrics: prefill tokens per second, first-token latency percentiles, and cache hit rate. Only comparisons under the same load-testing script and the same dataset make order-of-magnitude improvements credible.
5. Common Pitfalls
- Expanding capacity without changing the access pattern: bandwidth is saturated first, so the expansion has no effect.
- Ignoring the version dimension of fingerprints: after a model upgrade, the hit rate looks inflated and outputs become abnormal.
- Treating eviction as a local problem: cross-node migration costs are not accounted for, and tail latency worsens.
- Load-testing only short prompts: the long-prefix characteristics of Agent workloads are completely hidden.
6. Implementation Recommendations
In a general cloud environment, you can use a container platform to host inference services and elastic scaling, use object storage or distributed caching for state overflow, use a message queue to smooth request spikes, and use logging services with monitoring and alerting to close the loop on observing hit rate and percentile latency. General cloud product categories from Baidu AI Cloud can serve as the foundation for this type of architecture, but specific selections should still be based on load-testing data.
Summary: The focus of cache optimization in hybrid architectures is not “bigger” but “more correct.” Only when state separation, tiered reuse, dual-channel pipelining, and cache-aware scheduling work together can the effective compute of the prefill stage be truly unlocked.
