Optimizing Prefix Caching for Hybrid Architecture Models: A Practice in Boosting Prefill Throughput

    Hybrid architecture large language models (LLMs) combining full attention and linear layers offer faster decoding and lower memory usage but introduce significant complexities in prefix cache management. This article explores the challenges of misaligned state granularity between attention and linear layers, proposes a state-alignment-centric cache redesign strategy involving unified state abstraction, alignment-aware chunking, tiered storage, and cache-affinity scheduling, and highlights real-world performance gains, including up to 3.14x prefill throughput improvement and an 88% reduction in p90 Time To First Token (TTFT).

    1. Why Hybrid Architectures Are Mainstream and Harder to Cache

    In recent years, many next-generation large models have moved away from pure Transformer full-attention structures toward hybrid architectures that combine “full attention layers + linear layers (linear attention/state space layers).” The motivation is clear: linear layers have a decoding complexity independent of sequence length and do not require maintaining a complete KV cache for historical tokens, resulting in faster decoding speeds and smaller memory footprints.

    However, this comes at the cost of significantly increased cache management complexity. The prefix cache for a pure Transformer model only needs to manage one structure: the Key/Value tensors corresponding to each token, which can be chunked, stored, and reused by matching block hashes. In contrast, the state of each layer in a hybrid architecture is not uniform:

    • Attention layers: Retain token-by-token KV blocks that can be aligned by prefix, naturally suited for block-level reuse.
    • Linear layers: The state is a “compressed recurrent state,” usually defined only at specific sequence positions (such as chunk boundaries). It cannot be truncated and reused at arbitrary token boundaries like KV blocks.

    This means that when two requests share a prefix, but the prefix length does not fall on the valid boundary of the linear layer’s state, the linear layer state cannot be directly reused. It must either fall back to recomputation or be forced to truncate the reusable length to an aligned position. In typical workloads like programming Agents—characterized by multi-turn conversations, ultra-long system prompts, and repeatedly occurring environment snapshots and file contents—the prefix hit rate is extremely high. The quality of the state reuse strategy directly determines the overhead of the prefill phase.

    2. The Core Problem: Mismatched Alignment Granularity of Two State Types

    The caching challenge of hybrid architectures can be abstracted as a “dual-state alignment” problem:

    1. KV cache can be segmented and reused according to fixed-size blocks (e.g., 16 or 64 tokens), offering a fine granularity.
    2. Linear state usually advances in larger blocks (e.g., a state update corresponds to hundreds of tokens) and can only save state snapshots at block boundaries, resulting in a coarse granularity.

    Therefore, the result of a prefix match must simultaneously satisfy two constraints: hitting as many tokens as possible at the KV level, and the hit point at the linear state level must fall on a state snapshot boundary. If the scheduler only performs greedy matching based on KV blocks, the reused prefix might exceed the range supported by the linear state, causing the linear layer to recompute part of the prefix from scratch. Although this recomputation is linear in complexity, it remains costly under ultra-long contexts.

    In addition, there are three practical engineering-level issues:

    • Unbalanced state sizes: Attention layer KV grows linearly with context, while linear layer state size is fixed. Once mixed, the memory budget, swap-in/swap-out strategies, and the object granularity of remote caching all require separate designs.
    • Multi-level cache coordination: State migration between GPU memory, host memory, and remote distributed caches must ensure that “KV blocks” and “linear state snapshots” are semantically synchronized from the same source. Otherwise, obscure errors may occur where KV is reused but the wrong state is applied.
    • Request scheduling coupling: The prefix hit rate is affected by request placement. Scheduling requests that share long prefixes to instances holding the corresponding states (cache affinity scheduling) is equally, if not more, important for hybrid architectures because the cost of state recomputation is higher.

    3. Design Approach: Redesigning Caches Around State Alignment

    To address the above issues, an effective solution is to redesign the caching layer around “hybrid states.” The core ideas can be summarized in four points:

    1. Unified State Abstraction. Treat the “KV block set + linear state snapshot” corresponding to each prefix as an inseparable cache unit, providing unified lookup, insertion, and invalidation interfaces. During matching, align primarily using the valid boundaries of the linear state, and truncate the KV blocks to the same position, fundamentally preventing the misalignment of the two state types.

    2. Alignment-Aware Chunking and Matching Strategy. Instead of greedy reuse at the fine granularity of KV blocks, plan chunking using linear state snapshot boundaries as anchors. For ultra-long common prefixes (such as system prompts and tool definitions), pre-set snapshots within them according to state boundaries, enabling subsequent requests to likely hit at the boundaries and maximizing the end-to-end reusable length.

    3. Tiered Storage and Asynchronous Swap-Out. Hot states reside in GPU memory; warm data is swapped to host memory; cold data is written to a distributed remote cache. Because the linear state size is much smaller than the KV of the same length, prioritizing the swap-out of KV and retaining the linear state often yields better overall hit benefits. Swap-in/swap-out overlaps with the computation pipeline to avoid synchronous copy blocking the prefill.

    4. Cross-Node State Reuse and Cache Affinity Scheduling. Serialize the hybrid state and store it in shared storage. Coordinate with the routing layer to record the “prefix fingerprint -> node” mapping, ensuring that multiple turns of requests for the same task land on nodes where the state is ready. On a miss, the state can also be pulled from the remote cache rather than recomputing the entire sequence.

    The illustrative matching logic is as follows:

    1. def lookup(request_tokens, cache):
    2. # 1. Calculate prefix fingerprint and find the longest hit cache unit
    3. hit = cache.longest_prefix_match(hash_blocks(request_tokens))
    4. if hit is None:
    5. return None
    6. # 2. Align the reusable length to the linear state snapshot boundary
    7. reusable_len = align_to_state_boundary(hit.length)
    8. kv_blocks = hit.kv.truncate(reusable_len)
    9. linear_state = hit.linear_state_at(reusable_len)
    10. return PrefixState(kv_blocks=kv_blocks,
    11. linear_state=linear_state,
    12. length=reusable_len)

    4. Real-World Performance Gains and Applicable Boundaries

    In evaluations using real programming Agent trajectory replays (based on next-generation high-bandwidth datacenter GPUs), this solution achieved a prefill throughput of up to approximately 3.14 times the mainstream open-source inference engine baseline, and the p90 Time To First Token (TTFT) decreased by up to 88%. Even after integrating hundreds of GiB of host memory cache and swapping a large amount of state out of GPU memory, the gains remained significant, indicating that the bottleneck mitigation primarily comes from state reuse itself rather than relying solely on GPU memory capacity.

    It is worth noting that the magnitude of the gains is strongly correlated with the workload pattern: Agent workloads with high prefix sharing rates, many turns, and long contexts benefit the most. For single-turn short requests with almost no shared prefix, the advantage of hybrid caching is mainly reflected in the low decoding overhead of the linear layer itself. For engineering implementation, it is recommended to first profile the prefix sharing rate of the traffic before deciding whether to introduce a full multi-level hybrid state cache.

    5. Conclusion and Practice Recommendations

    Hybrid architectures trade more complex caching semantics for lower decoding costs. To unlock their full potential in Agent workloads, the keys are: unifying dual-state management anchored by linear state boundaries, performing alignment-aware chunking and matching, building a GPU-memory-remote multi-level state cache, and combining it with cache affinity scheduling. For teams building their own inference platforms, these capabilities can be evolved as an independent module within the inference engine’s caching layer. For teams building inference services using cloud container platforms and object storage, the architecture for remote state caching and distributed reuse can also be directly mapped onto general storage and scheduling components.