State Cache Refactoring for Hybrid-Architecture LLMs: Separating KV Cache from Recurrent State
This article explains why hybrid architectures that alternate full-attention layers with linear or state-space layers break traditional prefix caching, and how to refactor the cache layer around that problem. It breaks down prefill overhead into computation, state reconstruction, scheduling, and data movement; then presents a dual-track design that indexes KV blocks by content hash and recurrent states by sequence position plus content digest, aligns reuse to fixed checkpoints, continues computation incrementally from the latest checkpoint on partial hits, and tiers state across GPU memory, host memory, and a larger cache pool with different eviction policies. Engineering guidance covers capacity planning, split transfer queues, consistency on session rollback, observability metrics, and evaluation with real multi-turn traces. The expected gains and their limits are also discussed.
1. Why Hybrid Architectures Make Caching Harder
Over the past two years, a hybrid architecture that alternates full-attention layers with linear or state layers has become a mainstream model design choice. The motivation is straightforward: full-attention layers handle precise global information retrieval, while linear layers (including various linear attention variants, gated state-space models, and recurrent state-update structures) carry long-range history at a constant memory cost. Compared with a pure attention stack, the hybrid design keeps memory usage more stable during decoding, and the key-value cache no longer grows linearly with context length.
The trouble starts when the workload shifts from single-turn question answering to multi-turn, long-context agent scenarios such as AI coding assistants. A single agent task often involves dozens of tool calls, code retrieval steps, and file fragments fed back into the context, and every turn has to prefill all previous turns again. At that point the cache system must manage two kinds of state with fundamentally different properties:
- Key-value cache: strongly position-dependent, chunked by token, prefix-shareable, and incrementally appendable.
- Recurrent state: compressed from the entire history into a fixed-size tensor that is independent of length and cannot be sliced per token.
Every assumption behind traditional prefix caching (block hashing, per-block matching, longest common prefix matching) was built around the first kind of state. Applying it directly to the second kind produces three problems: inaccurate hit determination, mismatched reuse granularity, and unbalanced swap-in/swap-out cost. This is the root cause of the reduced gains during prefill in hybrid models.
2. Where Prefill Actually Loses Time
Optimization starts with attribution. Under a hybrid architecture, prefill cost splits into four parts:
- Computation: full-attention layers grow quadratically with context length and remain the dominant compute cost for long contexts.
- State reconstruction: the linear layers’ state can only be produced by advancing sequentially from the start of the sequence; you cannot “look at only the fragment you need” the way attention can.
- Scheduling: request arrivals are bursty, and whether the cache hits determines whether a request takes the “full recompute” path or a “lightweight continuation” path.
- Data movement: state migrates between GPU memory, host memory, and remote storage, and bandwidth often becomes the bottleneck before compute does.
The second item is unique to hybrid architectures. It means the cache cannot stop at block-level prefix matching; it must also answer a more fundamental question: given a history, can its corresponding fixed-size state be safely reused, and up to which position?
3. Core Design: Managing State Cache and KV Cache Separately
The approach is to explicitly decouple the two kinds of state, give each a management strategy that fits it, and orchestrate them together at the scheduling layer. Four design principles summarize the idea.
3.1 Dual-Track Indexing
Maintain a content-chunk-based hash index for the KV cache, and a composite index based on “sequence position + content digest” for the recurrent state. The former supports per-block longest-prefix hits; the latter exists as checkpoints, persisted only at fixed intervals, which avoids the redundancy of storing a fixed-size state at every position.
kv_index: block_hash -> kv_blocksstate_index:(seq_pos, digest) -> state_tensor
3.2 Checkpoint Alignment
Because a fixed-size state cannot be sliced, reuse must land on checkpoints. In practice, the checkpoint interval is usually set to an integer multiple of the block size, so that the “KV block hit length” and the “reusable state position” align naturally. This avoids the mismatch where KV blocks hit through 3.7 blocks but the state can only be reused up to block 3.
3.3 Continuation Strategy on Partial Hits
When only part of the prefix hits, the correct approach is to load the most recent checkpoint state and incrementally advance only the remaining, unmatched tokens. Pseudocode:
def prefill(request, cache):hit = cache.kv.match_prefix(request.tokens) # per-block matchingcp = cache.state.latest_checkpoint(hit.length) # align to a checkpointstate = cache.state.load(cp) if cp else init_state()kv = cache.kv.load(hit.blocks)tail = request.tokens[cp.end:] # only the tail needs advancingstate, kv_new = model.forward_incremental(tail, state, kv)cache.state.save_checkpoint(request.seq_id, state)return state, kv.concat(kv_new)
3.4 Tiering and Eviction
GPU memory holds only the hottest checkpoints and KV blocks; host memory serves as the second tier; a larger cache pool forms the third tier, used for state reuse across requests, across sessions, and even across nodes. For eviction, KV blocks suit per-block LRU, while state checkpoints should be weighted by their distance from the current longest common prefix, keeping checkpoints that sit on the backbone of a long conversation.
4. Implementation Notes
- Capacity planning: a fixed-size state is small, but the number of checkpoints grows linearly with concurrent sessions. Estimate it as “number of sessions × inverse of checkpoint interval × size of one state,” and reserve the bulk of the bandwidth budget for KV blocks.
- Transfer optimization: state tensors are contiguous, small, and frequent, which suits batched aggregation and pipelined prefetch; KV blocks are larger and suit sharded parallel transfer. The two traffic types should use separate queues so they do not block each other.
- Consistency: when a session is interrupted or rolled back, the corresponding KV blocks and state checkpoints must be invalidated together; otherwise a silent error appears in which state and history no longer match.
- Observability: collect at least five metrics: prefix hit rate, checkpoint hit rate, state load time, prefill queue time, and time-to-first-token percentiles.
- Validating results: replay real multi-turn traces rather than synthetic single-turn load tests, and watch p90/p99 time to first token; averages hide long-tail experience problems.
5. Sources of Gains and Reasonable Expectations
The gains from a separated state cache come from three places. First, turning a “full recompute” into “incremental tail advancement” directly cuts prefill computation. Second, making the fixed-size state reusable across requests lowers the marginal cost of long conversations. Third, tiering and prefetching overlap data movement with computation and reduce queueing. Under workloads with long contexts and multi-turn tool calls, this kind of optimization typically delivers a several-fold improvement in prefill throughput and a noticeable drop in tail latency; under short-context, low-reuse workloads the gains converge significantly. Always measure a baseline against your own traffic characteristics before shipping, and avoid applying these conclusions blindly.
6. Summary
The caching challenge in hybrid architectures is not fundamentally about “not enough cache”; it is about treating two kinds of state as one. Index the KV cache and the recurrent state separately, align reuse to checkpoints, move state in tiers, and drive evaluation with real traces, and you can keep the memory advantage of the hybrid architecture while pulling prefill throughput and latency back into a usable range. For teams building AI coding assistants, long-document analysis, or multi-turn tool-calling platforms, this approach is a starting point for refactoring the cache layer, and it can be combined with general cloud capabilities such as containerized deployment, object storage, message queues, and monitoring and alerting.
