Hybrid Architecture State Cache Optimization: 3x Prefill Throughput

    This article explains why hybrid architectures that combine full attention layers with linear or state layers have become a common choice for long-context workloads, and why they make KV cache management significantly harder. It covers the semantic differences between attention state and recurrent state, then walks through three design strategies: layered decoupling with independent addressing, lifecycle-aware eviction, and coordination with tiered storage. Engineering practices and realistic expectations for prefill throughput gains are also discussed.

    Hybrid Architecture State Cache Optimization: 3x Prefill Throughput

    1. Background: Why Hybrid Architectures Dominate Long-Context Workloads

    A clear trend in model architecture over the past year or two is the shift from pure attention stacks toward hybrid stacks that interleave full attention layers with linear or state layers. The reason is straightforward: the compute and memory cost of attention layers grows quadratically with context length, while linear attention, gated convolutions, and state space structures can maintain a fixed-size recurrent state during decoding, flattening the memory curve for long sequences.

    The benefits are especially pronounced for workloads such as intelligent coding tools and code assistants. A real agent session often involves dozens of tool calls: reading files, searching code, running tests, and rolling back changes. Each turn requires resending all prior history to the model, with contexts easily reaching tens of thousands or even over a hundred thousand tokens. In this scenario, decoding speed and cache footprint directly determine how many concurrent sessions a single machine can support.

    But hybrid architectures are not a free lunch. They turn a single-dimensional caching problem into a joint management problem for two semantically different kinds of state, and this is exactly where engineering practice tends to go wrong.

    2. The Core Conflict: Two Kinds of State with Different Semantics

    A pure attention model has only one form of cache: a set of key/value vectors per token, which can be sliced, discarded, and reused precisely by position. A hybrid architecture has at least two kinds of state:

    • Attention state: one-to-one with token positions, randomly addressable, and well suited to paged storage and on-demand loading.
    • Recurrent/linear state: a fixed-size tensor updated in place repeatedly. It encodes a compressed summary of “all history from the beginning to the current position” and cannot be sliced by token.

    The conflict follows directly. Suppose a session prefix has been reused for 8,000 tokens. The recurrent state at position 8,000 is only valid when “the preceding 8,000 tokens are exactly identical.” Once the user inserts a new system prompt midway, or the order of tool results changes, that state is invalidated entirely and must be recomputed from scratch. The attention portion, however, can still retain most of its hits.

    If a cache system manages both kinds of state as the same object, two typical failures appear. One is conservative over-invalidation, where the hit rate drops off a cliff. The other is aggressive reuse, which causes abnormal model output quality that is extremely difficult to diagnose.

    3. Design Approach One: Layered Decoupling and Independent Addressing

    The first key design is to fully decouple the two kinds of state at the storage and addressing layers, giving each its own metadata and lifecycle.

    Attention state is managed in paged blocks, sliced at a fixed token granularity, with each block carrying a prefix hash to form a traversable prefix tree. Recurrent state exists in the form of “checkpoints,” persisted only at specific positions, and explicitly records the prefix hash chain it depends on.

    1. Prefix tree node:
    2. block_id
    3. prefix_hash # uniqueness determined together with the parent node
    4. kv_page_refs[] # attention page references
    5. state_ref # recurrent state checkpoint at this position (nullable)
    6. token_count

    The direct benefit is that hit determination can be layered. Attention blocks are compared block by block, and however many hit, hit. The recurrent state is used only when the hash chain matches exactly; otherwise, the system falls back to the most recent valid checkpoint and recomputes from that point onward. In engineering terms, the write path should enforce that “state checkpoints are atomically bound to the hash chain” to avoid dangling references.

    4. Design Approach Two: Lifecycle-Aware Eviction

    Traditional cache eviction usually looks only at least recently used, but under a hybrid architecture, the “value” of a recurrent state depends heavily on its position in the tree and its likelihood of being reused.

    A more reasonable approach introduces layered scoring: checkpoints near the root get the highest retention priority, because they are the common prefix of many sessions; checkpoints on deep leaves with few branches can be evicted first, because rebuilding them costs only a short-sequence prefill. Attention blocks and state checkpoints should also be given different capacity quotas, so that fixed-size states do not fill up memory or VRAM and squeeze out the more splittable, higher-reuse attention blocks.

    A simplified scoring function can be written as:

    1. def score(node, now):
    2. depth_bonus = 1.0 / (1 + node.depth) # closer to the root is more worth keeping
    3. reuse = node.hit_count / (now - node.last_hit + 1)
    4. rebuild = node.token_count # rebuild cost
    5. return (depth_bonus * reuse) / rebuild

    It is worth emphasizing that the specific thresholds must be calibrated against real trace replays; there is no universally optimal parameter set.

    5. Design Approach Three: Coordinating with Tiered Storage

    When session scale exceeds single-machine VRAM, the cache must extend to host memory and even remote storage. The key here is not “whether it can be stored,” but “whether the swap-in and swap-out granularity matches the state semantics.”

    Attention blocks are naturally suited to large sequential transfers and can fully utilize bandwidth; recurrent states are small but latency-sensitive, and are best kept resident at a higher tier. The recommendation is therefore: the hot tier holds state checkpoints and high-frequency attention blocks, the warm tier holds paged attention blocks, and the cold tier uses object storage for long-cycle session archival. On Baidu AI Cloud, such tiering can map naturally to a combination of inference services on the container platform, disk/memory caching, and object storage, with hit rate and swap-in latency observed through the monitoring and alerting service.

    6. Engineering Implementation Notes

    1. Hit determination must be observable: track the attention hit rate and the state reuse rate separately, otherwise latency jitter cannot be explained.
    2. Prefix normalization: unify volatile fields such as system prompts, tool descriptions, and timestamps at the request entry point, to avoid breaking the hash chain over irrelevant differences.
    3. Asynchronous prefetching: predict the prefix likely to be hit in the next turn based on session history, and swap it in ahead of time.
    4. Consistency checks: sample and compare the output difference between “reused state” and “full recomputation” as a regression metric.
    5. Distributed reuse: share prefix tree metadata across nodes and pull state data on demand, avoiding metadata broadcast storms.

    7. Sources of Gains and Realistic Expectations

    The multiplicative improvement in prefill throughput essentially comes from three things stacking together: common prefixes are skipped entirely, recurrent states need no recomputation, and swap-in/swap-out overlaps with computation. The improvement in the long tail of time-to-first-token mainly comes from avoiding large-scale recomputation. It should be stated objectively that actual gains depend strongly on workload characteristics: the higher the proportion of shared prefixes across turns and the longer the sessions, the more pronounced the benefit; conversely, gains are limited in short-request, low-reuse scenarios. So before going live, be sure to run replay evaluations on your own real traces rather than copying someone else’s numbers.