Redesigning Cache Management for Hybrid Architectures: The Key to Higher Prefill Throughput
This article explains why traditional prefix caching breaks down for hybrid models that combine full attention layers with linear layers, especially under multi-turn, long-context AI agent workloads. It covers the core differences between KV caches and recurrent states, why cache hit rates drop in agentic coding scenarios, and five practical redesign principles: unified chunk boundaries, state snapshots, tiered storage, asynchronous prefetching, and scheduling coordination. The article also outlines the engineering gains observed in trace replay tests and offers deployment guidance for measuring prefix reuse, monitoring cache performance, and planning capacity.
1. Background: Agent Workloads Push Cache Management to the Forefront
Over the past year, a number of new models have shifted toward hybrid architectures that combine full attention layers with linear layers. A small number of global attention layers are retained to reduce cache footprint and speed up decoding. However, when the workload changes from single-turn question answering to a multi-turn, long-context AI coding assistant, the bottleneck quickly moves from decoding to prefill. Every request must ingest repository context containing tens of thousands or even millions of tokens, which stretches time-to-first-token and consumes large amounts of compute on repeated work.
The automatic prefix caching mechanism in traditional inference frameworks was designed for pure attention models. Its cache unit is a homogeneous, addressable key-value block that can be reused in full segments. Hybrid architectures break this assumption: linear layers carry recurrent states that are neither addressable by position nor easily concatenated.
2. The Fundamental Difference Between the Two Cache Types
The key-value cache of full attention layers can be chunked by token position. Any prefix can be matched and resumed. Linear attention and state space layers maintain a fixed-size recurrent state, which is a lossy compression of “all history up to this point.”
This leads to three direct consequences:
- States cannot be concatenated: the states of two different prefixes cannot be added together to produce a merged state.
- States are not addressable: there is no concept of “the state at token 1000,” only “the state after reading the first 1000 tokens.”
- States are coupled to position: once history is inserted, rolled back, or rewritten, the state must be recomputed.
Schematically, the cache for a single request can be abstracted as two heterogeneous structures:
@dataclassclass HybridCache:kv_blocks: dict[int, Block] # Attention layers: addressed by block numberstates: dict[int, State] # Linear layers: fixed-size state stored per layerprefix_len: int # Token position consumed so far
3. Why Prefix Reuse Fails at Scale Under Agent Workloads
AI coding tools produce trajectories with distinct characteristics. System prompts and tool definitions remain stable, and the repository skeleton is relatively stable. However, each turn appends tool outputs, diffs, and retrieval snippets. History compaction and branch retries also occur frequently. As a result, the longest common prefix often stops at tens of thousands of tokens, while the following hundreds of thousands of tokens are fully recomputed.
The heterogeneous alignment problem is even more challenging. Even when attention layer blocks are matched, linear layer states may be unusable due to inconsistent chunk granularity or rollback points. The system ultimately has to fall back to prefilling from scratch. This is the root cause behind the declining cache hit rates that many teams observe on hybrid models.
4. Five Key Points for Redesigning the Cache Management System
First, unify chunk boundaries. Use the same splitting rules for attention blocks and state snapshot points to ensure that “a hit makes both segments available at the same time.”
Second, use state snapshots rather than state deltas. Persist fixed-size states at fixed intervals, together with position metadata, so that rollback and branching only need to revert to the most recent snapshot.
Third, adopt tiered storage. Keep hot states in GPU memory, use host memory as a secondary pool, and rely on remote distributed storage for large-scale prefixes. Admission and eviction should be based on prefix reuse probability and state size, rather than simple least recently used.
Fourth, implement asynchronous prefetching and pipeline overlap. Based on the file manifest and history fingerprints carried by the request, move candidate states to GPU memory in advance so that transfer and computation overlap.
Fifth, coordinate scheduling. Orchestrate requests that share long prefixes onto the same instance to reduce cross-node transfer. For cross-node scenarios, rely on consistent-hash-style state addressing.
5. Where the Engineering Gains Come From
In real trajectory replay tests, this type of redesign pushed prefill throughput on a single GPU into the range of several times that of a general-purpose inference framework, while p90 time-to-first-token dropped by more than 80%. After integrating a host memory cache on the order of hundreds of gigabytes, the gains remained and could scale to distributed storage and cross-node state reuse. The gains come from three overlapping effects: repeated computation is eliminated, GPU memory pressure is shifted to cheaper tiers, and queuing for long-tail requests is smoothed out.
6. Deployment Recommendations
Start by collecting prefix fingerprint statistics on your own inference pipeline to quantify the distribution of reusable prefix lengths. Then decide the snapshot interval and cache capacity. Bring cache hit rate, state transfer time, and p90 time-to-first-token into the same monitoring, alerting, and logging system. For capacity planning, use object storage or a distributed file system for cold states, and use a container platform to elastically scale prefill instances. If you use the relevant general-purpose capabilities from Baidu AI Cloud, you can build a validation environment with a combination of “compute instances + high-speed local disks + object storage + monitoring and alerting,” start with a small canary rollout, and gradually scale up.
