Refactoring State Caching for Agents in Hybrid Attention Architectures
This article examines why prefix caching is harder in hybrid architectures that combine full attention with linear state layers, and how to refactor the cache as a first-class system. It covers the semantic differences between KV and state objects, asymmetric fork and rollback costs, tiering strategies, prefix fingerprinting, prefill scheduling, and copy-on-write checkpoints, along with the performance gains reported for coding agent workloads.
1. Background: Agent Workloads Expose the Weak Spots of Hybrid Caching
Over the past year, a clear trend has emerged in mainstream LLM architectures: full attention is retained in a subset of layers, while the remaining layers adopt linear state update mechanisms such as linear attention, gated linear units, or state space models. The result is a hybrid structure of “full attention plus linear layers.” The motivation is straightforward. Full attention provides precise global retrieval, while linear layers carry long-sequence history at a constant state cost, reducing computation during decoding and significantly shrinking the cache footprint.
The picture changes with coding agent workloads. These workloads are characterized by many rounds of tool calls, very long system prompts, code context that is repeatedly appended, and frequent “roll back to a past version and branch again” behavior. Prefix cache hit rate therefore becomes the key performance factor, and cache management in hybrid architectures is far more complex than in pure attention models.
One research team replayed real agent coding traces on GPUs. The refactored hybrid state cache delivered prefill throughput of roughly three times that of a general-purpose inference framework at peak, and cut p90 time-to-first-token by nearly 90% at best. The gains held after attaching hundreds of gigabytes of host-side cache, and the design supports distributed storage and cross-node state reuse. This article strips away the specific product and abstracts the technical core.
2. Why Prefix Caching Is Harder in Hybrid Architectures
2.1 The two state types have different semantics
The attention cache accumulates key-value pairs token by token. It can be trimmed precisely and located at any position. The linear layer cache is a state tensor that is continuously overwritten. It encodes a summary of everything seen so far and cannot be indexed by token. On a prefix hit, the former can be sliced and reused directly, while the latter must be snapshotted and restored as a whole.
2.2 Forking and rollback costs are asymmetric
Agent scenarios frequently branch multiple paths from the same prefix. Attention caches can be shared read-only with reference counting. Once a linear state is overwritten by later tokens, it cannot be partially rolled back; it can only be restored to a checkpoint. This requires explicit state version management in the cache system.
2.3 Memory tier mismatch
Attention caches are large but splittable, which suits block-wise swapping between device memory and remote storage. Linear states are small but indivisible, which suits residency in device or high-speed memory. Managing both object types with one eviction policy creates a dilemma: small objects get moved repeatedly, or large objects crowd out the hot set.
3. Refactoring Approach: Treat the Cache as a First-Class Citizen
3.1 Unified abstraction with per-type policies
Define a common cache object interface that hides the underlying differences:
class CacheObject:key: str # prefix fingerprintkind: str # "kv" | "state"version: int # state version numberref_count: int # sharing count across forkstier: str # device / host / remote
Upper-layer scheduling works only against this interface. The lower layer dispatches on kind: kv objects use block indexing, state objects use snapshots and copy-on-write.
3.2 Prefix fingerprints and layered lookup
Compute an incremental hash over the request prefix to form a comparable fingerprint chain. On lookup, check the device hot zone first, then host memory, then remote distributed storage. For state objects, also validate the version to avoid reusing an object whose fingerprint matches but whose state has already been overwritten.
3.3 In-batch scheduling for prefill
Prefill is the throughput bottleneck. Practical measures include chunking long prefixes for parallel computation, skipping the parts that hit, batching the missed chunks across different requests, and ordering by state dependency rather than arrival order to reduce state rollbacks.
3.4 Copy-on-write and checkpoints
On a fork, use copy-on-write for state. Take a checkpoint every fixed number of tokens so rollback granularity drops from an entire segment to a fixed window, substantially reducing recomputation in the worst case.
4. Engineering Notes
- Capacity planning: keep linear states and the hottest attention blocks in device memory, warm data in host memory, and cold data plus cross-node sharing in remote storage.
- Consistency: add version numbers and checksums to cache objects, and verify before mounting them for cross-node reuse.
- Observability: track hit rate, swap-in and swap-out bandwidth, state rollback counts, and prefill queue time to locate bottlenecks.
- Integration with general cloud capabilities: container platforms can provide elastic replicas, object storage can serve as the cold tier, and monitoring and alerting services can power a hit-rate dashboard. General-purpose capabilities on platforms such as Baidu AI Cloud can host this kind of tiered cache deployment.
5. Where the Gains Come From
The throughput improvement is not a single optimization but three effects stacked together. First, a hit skips work and removes wasted prefill computation. Second, in-batch merging and dependency ordering raise compute utilization. Third, tiered storage keeps hot data resident and lowers transfer overhead. The latency improvement comes mainly from p90 tail requests no longer recomputing long prefixes because of state rollbacks.
6. Conclusion
Hybrid architectures are a reasonable trade-off between decoding efficiency and cache size, but they shift complexity to the cache management layer. For agent workloads, whoever governs attention blocks and linear states as two distinct object types will gain several-fold improvements in prefill throughput and time-to-first-token.
