Hybrid State Cache Refactoring to Boost Prefill Throughput for Agent Workloads
This article examines the cache bottlenecks introduced by hybrid attention-linear architectures under long-context and agent workloads, particularly during the prefill phase. It proposes a unified state cache design that treats KV caches and linear recursive states as a single, addressable, snapshotable resource, enabling prefix reuse, boundary snapshots, hierarchical offloading, and scheduling alignment. The article details the design principles, key implementation patterns, performance evaluation methods, and practical considerations for production deployment, with a focus on improving prefill throughput and reducing redundant computation.
Background: What Hybrid Architectures Solve—and What They Introduce
In long-context and agent scenarios, models that rely entirely on full attention layers face two compounding costs: during decoding, each generated token requires reading a growing KV cache, which places heavy pressure on GPU memory bandwidth; and long contexts cause the KV cache to expand rapidly, limiting concurrent sessions. To mitigate this, industry practice commonly adopts a hybrid architecture combining full attention layers with linear recurrent or state-space layers. Attention layers retain precise modeling of key historical information, while linear layers carry long-range information in fixed-size recurrent states, reducing cache reads and cache growth during decoding.
This architecture brings clear benefits on the decoding side, but agent workloads shift the bottleneck to the prefill phase. Agent sessions typically include long system prompts, tool definitions, multi-turn conversation history, tool results, and continuously appended context. After each tool call, the model often needs to reprocess newly added context. If cache management remains designed only around traditional KV caches, it ignores the saving, reuse, and migration of linear layer recurrent states, leading to repeated computation, state copying, memory fragmentation, and scheduling mismatches. The original proposal set a design goal of improving prefill throughput to roughly three times baseline; this figure should be understood as a target or reported value under specific workloads and configurations, not a universal promise. The reusable takeaway is the systematic refactoring of hybrid state caching.
Four Cache Bottlenecks Under Agent Workloads
1. Inconsistent cache granularity. Full attention layers cache per-layer KV, while linear layers cache recurrent or convolutional states. The two cache types have different lifecycles, shapes, update mechanisms, and reuse conditions. Managing them separately can lead to inconsistencies such as “KV hit but state miss” or “state invalid but KV still referenced.”
2. Incomplete prefix reuse. Agent requests often share system prompts and tool descriptions. Traditional prefix caching can reuse KV, but if linear states are not snapshotted along with the prefix, incremental prefill still needs to recompute states from scratch, weakening the benefit of prefix reuse.
3. Missing state snapshots at turn boundaries. After tool results are returned, context is appended at fixed boundaries. If hybrid state snapshots can be saved at turn boundaries, the next turn only needs to prefill the newly added segment rather than recomputing the full history. Without such snapshots, prefill computation grows linearly or worse with context length.
4. Prefill and decode competing for resources. Agent requests arrive in bursts; long prefills can block short decode requests, causing first-token latency jitter. In hybrid architectures, state caches occupy GPU memory; without hierarchical offloading and scheduling awareness, batch efficiency degrades.
Design Principles for Hybrid State Cache Refactoring
The goal is not simply “cache more,” but to unify KV caches and linear states into an addressable, snapshotable, hierarchical, and schedulable hybrid state cache.
Unified cache abstraction. Define a unified cache entry for each session or prefix, containing KV shards, linear layer states, model version, tokenizer version, context boundaries, and reference counts. The cache key should be derived from the token sequence hash, model version, and sampling-independent configuration, avoiding mixing states from different versions.
Prefix awareness and state snapshots. Save state snapshots at points such as the end of system prompts, turn boundaries, and tool result returns. When a matching prefix is found, restore both KV and linear states, and perform incremental prefill only for new tokens.
Hierarchical storage and asynchronous offloading. Hot states stay in accelerator memory, warm states in host memory, and cold states may be stored in general-purpose object storage. Offloading and prefetching run asynchronously to avoid blocking critical paths. In cloud deployments, general-purpose object storage, logging services, and monitoring/alerting can be used for persistence and observability, but cache consistency remains controlled by the inference side.
Chunked prefill and state merging. Split long contexts into chunks, save states at chunk boundaries, and support pipeline-parallel prefill. Chunk size requires a tradeoff among parallelism, snapshot overhead, and cache hit rate.
Scheduling cooperation. In batch processing, group requests by cache hit status: high-hit requests take incremental prefill, low-hit requests take full prefill. In mixed prefill-decode batches, limit the proportion of long prefills to protect P99 latency.
Key Implementation Pseudocode
function get_or_build_state(request):key = hash(request.prefix_tokens, model_version, tokenizer_version)entry = cache.lookup(key)if entry and entry.valid:state = cache.restore(entry) // KV + linear_statereturn incremental_prefill(state, request.new_tokens)state = full_prefill(request.prefix_tokens)cache.store_snapshot(key, state, boundary=request.boundary)return incremental_prefill(state, request.new_tokens)function incremental_prefill(state, new_tokens):for block in split_into_blocks(new_tokens):state = forward_prefill(state, block)if block.is_boundary:cache.store_snapshot(derive_key(state), state, boundary=block.end)return state
Implementation must handle reference counting and copy-on-write carefully: when multiple sessions share prefix states, new token additions only modify their own branches, avoiding in-place writes that corrupt shared snapshots. Eviction policies should account for recency, state size, recomputation cost, and hit history.
Sources of Performance Gains and Evaluation Methods
Prefill throughput improvements mainly come from four aspects: reducing repeated historical prefill, improving prefix and turn snapshot hit rates, lowering memory stalls via asynchronous offloading, and reducing mutual blocking between long and short requests via scheduling grouping. The three-times figure should be verified through stress testing and cannot be extrapolated directly. Recommended metrics include: prefill throughput (tokens/s), first-token latency and its P99, cache hit rate, snapshot restore time, GPU and host memory usage, offloading bandwidth, and the ratio of recomputed tokens. Stress tests should cover four scenarios: shared system prompts, multi-turn tool calls, mixed long/short concurrent requests, and cold cache startup.
Practical Considerations for Deployment
First, cache isolation and security. In multi-tenant environments, cache keys must include a tenant isolation dimension; sensitive contexts must not be reused across tenants. Second, version invalidation. When model weights, tokenizer, or inference configurations change, old states must be invalidated. Third, boundary selection. Too-frequent snapshots increase storage and write amplification; too-sparse snapshots reduce reuse benefits. Boundary placement should be tuned based on business turn distribution. Fourth, fallback mechanism. When cache anomalies occur, the system should fall back to full prefill, prioritizing correctness. Fifth, observability. Incorporate hit rates, recompute ratios, and latency percentiles into monitoring and alerting, and continuously adjust chunk size, eviction policy, and offloading thresholds.
Overall, the value of hybrid architectures extends beyond the decoding side. By refactoring hybrid state caching around the prefill characteristics of agent workloads, and managing KV and linear states as a unified resource, the architectural advantages can be translated into end-to-end throughput and user experience gains.
