Optimizing Cache Management for Hybrid-Architecture Models Under Agent Workloads
Hybrid architectures that combine full-attention layers with linear attention or state-space layers reduce decoding memory use and improve token generation speed, but they also make prefix caching more difficult. This article explains how to manage KV Cache and recurrent state as a unified hybrid state, using aligned checkpoints, tiered storage, lazy materialization, and cache-aware scheduling. In replay tests with real coding-agent traces, the approach delivered up to 3.14× higher prefill throughput and reduced p90 time to first token by as much as 88.4%.
Introduction: The Cache Challenge Behind the Rise of Hybrid Architectures
Over the past two years, more large language models have adopted hybrid architectures that combine full-attention layers with linear layers, such as linear attention or state-space layers. The goal is straightforward: full-attention layers preserve the ability to model long-range dependencies at important positions, while linear layers process most of the sequence with a constant-size state. This significantly reduces GPU memory consumption during decoding and accelerates token-by-token generation.
Compared with pure Transformer models, hybrid architectures use a smaller KV Cache and can decode faster. Under the same hardware constraints, they can support longer contexts and higher concurrency.
Architectural benefits, however, do not automatically simplify system design. When these models are used for real workloads such as coding agents and conversational agents, a new challenge emerges: prefix-cache management becomes much more complex.
In an agent workflow, each interaction appends a small amount of new content to the end of the historical context. In theory, most of the prefix should be reusable, allowing the system to skip repeated computation. In a hybrid architecture, however, the model state includes more than the KV Cache. It also includes the recurrent state of the linear layers. These two types of state have very different lifecycles, granularities, and storage costs.
Applying cache designs developed for pure Transformer models can therefore prevent substantial portions of the prefix from being reused. As a result, compute is wasted during the prefill phase.
This article examines a redesign of state caching for hybrid-architecture models. The approach achieved more than three times the original prefill throughput in replay tests using real agent traces, reduced p90 time to first token (TTFT) by nearly 90%, and worked together with distributed CPU-side caching.
1. Why Prefix Caching Is More Difficult for Hybrid Architectures
1.1 Pure Transformers Have a Straightforward Cache Model
For a pure Transformer model, the implementation path for prefix caching is relatively clear. The system manages the KV Cache in token blocks and indexes those blocks using content hashes. When a block is found, it can be reused directly.
KV Cache also has a natural token-level partitioning property. The KV values for the first N tokens are a prefix subset of the KV values for the first N+K tokens. Therefore, almost any position can serve as a reuse boundary.
1.2 Linear-Layer State Is Compressed and Cannot Be Arbitrarily Split
Linear layers, including linear-attention and state-space layers, work differently. They compress the entire prefix into a fixed-size recurrent state. This state is a lossy summary of the history: it preserves the state after processing the first N tokens, but it cannot be used to reconstruct the state at arbitrary intermediate positions. Two states also cannot simply be concatenated into a valid new state.
This creates two direct consequences:
- Reuse granularity is constrained: a cached state can be reused only when it exactly matches the prefix boundary of the current request. Being off by even one token may prevent reuse.
- State storage requires a trade-off: denser checkpointing improves the hit rate, but each checkpoint consumes a meaningful amount of storage. Sparse checkpoints reduce storage usage but can lead to much more recomputation.
1.3 Agent Workloads Amplify the Problem
Coding agents typically have long system prompts, extensive tool definitions, and continuously growing multi-turn contexts. Each new user message or tool result is usually relatively short. This means:
- Prefix overlap is extremely high, so cache hit rates should ideally be excellent.
- The prefix boundary moves forward incrementally with every conversation turn. If saved state checkpoints do not align with those boundaries, the system must fall back to an earlier checkpoint and recompute the intervening segment.
- With long contexts, even a relatively short recomputation directly increases prefill time and therefore affects time to first token (TTFT).
The core systems challenge for hybrid architectures under agent workloads can be summarized as follows:
How can linear-layer state and KV Cache be managed together within a limited storage budget to maximize prefix reuse?
2. Design Approach: Redesigning Hybrid-State Caching
The central idea of the proposed approach is to manage the KV Cache and linear-layer state as a unified hybrid state, rather than as two isolated caching mechanisms. The main design principles are described below.
2.1 State Checkpoints at Aligned Boundaries
Because linear-layer state cannot be split arbitrarily, the system must choose checkpoint locations strategically. Practical choices include:
- Prioritize request boundaries: In agent workloads, conversation turns and tool-call boundaries are natural reuse points. Persisting or retaining state at these locations can maximize the hit rate.
- Add evenly spaced checkpoints with a sliding-window strategy: For very long prefixes, checkpoints can be added at fixed intervals. This places an upper bound on the distance between any prefix boundary and the nearest checkpoint, limiting worst-case recomputation.
- Align with KV block boundaries: Placing state checkpoints at KV Cache block boundaries allows both layers of caching to use a common indexing scheme. This helps avoid situations where the KV Cache hits but the corresponding recurrent state does not.
2.2 Tiered Storage Across GPU, CPU, and Distributed Caches
Although hybrid state is smaller than a pure KV Cache, GPU memory is still insufficient for storing all historical states in long-context, high-concurrency scenarios. A tiered design can distribute state across multiple storage levels:
- Keep hot state on the GPU: State for actively running sessions remains in GPU memory, enabling reuse without transfer overhead.
- Offload warm state to CPU memory: Large CPU memory pools, potentially hundreds of GiB, can store state for recent sessions. When a state is needed, transferring it back to the GPU over a high-speed interconnect is often much cheaper than recomputing the prefix.
- Place cold state in distributed storage: A distributed cache shared across nodes allows sessions for the same user or project to reuse historical state even when they are scheduled on different machines. This is particularly important for inference services deployed with multiple replicas.
Experiments show that the approach continues to provide significant benefits after adding a large CPU-side cache. This indicates that the gains do not come merely from having more storage. They primarily come from improving the cache-management strategy and its hit rate.
2.3 On-Demand Materialization and Lazy Computation
Another important idea is to distinguish between a logical prefix and its physical state.
The system can first maintain a lightweight logical index that records which prefixes are available and which checkpoints correspond to them. Only after a request arrives and the reuse path is determined does it materialize the required state.
This avoids computing and storing a complete state for every possible prefix. Instead, the available storage budget is used only for states that are likely to be reused.
2.4 Coordinating with the Scheduler
Cache hits should not be treated as an isolated concern. If the scheduler can observe cache state while queuing and batching requests, it can make better decisions based on which requests share prefixes and which states are already resident in GPU memory.
For example, the scheduler can:
- Prioritize requests that can reuse local GPU state, reducing state-transfer overhead.
- Batch requests with shared prefixes together to amortize prefill costs.
- Under GPU memory pressure, evict states that are relatively cheap to recompute, such as states located close to an existing checkpoint.
3. Performance Results and Interpretation
In replay tests based on real coding-agent interaction traces and conducted on a new-generation data-center GPU, the approach delivered clear performance improvements:
- Peak prefill throughput reached approximately 3.14× that of the baseline inference framework.
- p90 time to first token decreased by up to approximately 88.4%.
- The benefits remained stable after adding a CPU cache with capacity in the hundreds of GiB.
- The design supported distributed storage for cross-node hybrid-state reuse.
These figures should be interpreted in context. The maximum benefit depends on the degree of prefix overlap and the regularity of turn boundaries. Coding-agent traces have an ideal pattern for this optimization: long shared prefixes followed by short increments.
For workloads with little prefix overlap, such as completely independent single-turn requests, the gains will naturally converge. However, agent workloads are among the fastest-growing and most cost-sensitive use cases for inference services. The practical value of improving hybrid-state caching in this environment is therefore substantial.
4. Engineering Recommendations
Teams building or selecting an inference service can evaluate and apply similar ideas from several angles:
- Quantify workload characteristics first: Measure inter-request prefix overlap, the length of each turn’s incremental content, and the distribution of context lengths. If prefix overlap exceeds 50%, hybrid-state cache optimization is very likely to provide meaningful returns.
- Align checkpoint policies with business and workflow boundaries: Conversation turns, tool calls, and file-edit operations are valuable checkpoint locations. They are often more effective than placing checkpoints mechanically at fixed token intervals.
- Calculate state-transfer costs carefully: The time required to move state from CPU memory or remote storage back to the GPU must be significantly lower than the time required to recompute the corresponding prefix segment. High-speed interconnects and sufficient CPU memory are important prerequisites.
- Design caching and scheduling together: An isolated cache module cannot deliver the full benefit. The scheduler must understand the cache topology and state locations.
- Use general cloud infrastructure where appropriate: In cloud deployments, object storage, distributed caching, and container platforms can provide the foundation for tiered state storage and cross-node sharing. For example, on Baidu AI Cloud, teams can combine container and storage services to build elastic inference clusters that manage hot, warm, and cold states across different tiers, reducing the overall cost of long-context agent services.
Conclusion
Hybrid architectures reduce the state required for decoding and improve generation speed, but they also make prefix reuse more difficult. A redesigned hybrid-state cache demonstrates that substantial improvements are possible when the system explicitly accounts for the fact that linear-layer state cannot be arbitrarily split.
With carefully placed checkpoints, tiered storage, on-demand materialization, and cache-aware scheduling, prefill throughput can increase by several times while time to first token drops significantly.
As agent applications move into production, system-level optimizations of this kind may become as important as the model architecture itself. They deserve close attention from developers of inference engines and large-scale AI serving platforms.
