Rebuilding State Caching for Hybrid LLMs: KV Cache and Linear State Co-Management

    This article examines why prefix caching becomes harder once hybrid architectures combine full attention layers with linear state layers. It breaks down the structural differences between position-based KV blocks and path-based recurrent states, explains how mismatched granularity, invalidation, and migration rules slow down prefill in multi-turn long-context workloads, and outlines four practical refactoring directions: unified accounting, state snapshotting, tiered cache coordination, and cache-aware scheduling. It also suggests evaluation metrics and explains where the reported throughput and latency gains come from.

    1. Background: Hybrid Architectures Make Cache Management Harder

    Recent open-source LLM designs increasingly introduce linear attention and state space layers—such as various gated delta rule update mechanisms and structured state space models—producing a hybrid structure where some layers perform full attention while others perform linear state recurrence. The motivation is straightforward: the cache of a full attention layer grows linearly with context length, whereas the inference state size of a linear layer is largely decoupled from sequence length. A hybrid structure can therefore preserve long-range modeling ability while significantly reducing memory usage during decoding and improving decoding speed.

    The engineering cost, however, is widely underestimated. In workloads like AI coding assistants, a single task often involves dozens of tool calls, code snippet reinjection, and long-file context, with many requests sharing large identical prefixes. Prefix caching in pure attention models uses “key-value tensors organized in blocks” as the basic unit, making hit detection simple. In hybrid models, however, the cache holds two semantically different kinds of objects at the same time: key-value blocks that grow with position, and fixed-size state tensors that evolve over time. Their lifecycles, invalidation conditions, and portability all differ, so traditional prefix caching strategies suffer from both lower hit rates and worse scheduling efficiency on hybrid architectures, with severe redundant computation during prefill.

    2. The Fundamental Difference Between the Two Cache Types

    To understand where the optimization room is, the difference has to be stated clearly first.

    The full attention layer cache is “position-based”: the key-value of the i-th token depends only on itself and the preceding context, and after block partitioning it can be reused independently, making it naturally suited to paged management and cross-request sharing.

    The linear state layer cache is “path-based”: the state is a lossy compression of the historical sequence, so the same state is only produced by the same prefix under the same input, but the state itself carries no position boundary information and cannot be concatenated block by block the way key-value blocks can.

    This produces three concrete conflicts:

    • Granularity conflict: key-value blocks use a fixed number of tokens as granularity, while states use an entire prefix as granularity, making them hard to account for in a single allocator.
    • Invalidation conflict: a single edit in the middle of a prefix only requires rolling back the pages after that block for key-value, but the state must be recomputed in full.
    • Migration conflict: key-value blocks can be transferred page by page across nodes, while a state must be packaged together with its corresponding prefix length and layer metadata, otherwise reuse produces incorrect semantics.

    3. Why Prefill Throughput Is Slowed Down

    In multi-turn long-context scenarios, actual prefill cost consists of three parts: redundant computation of the missed prefix, alignment padding at the hit boundary, and the transfer wait for cache swapping in and out. Under a hybrid architecture, all three get worse at once.

    If the scheduler matches by “most similar prefix,” it often obtains a longer nominal match length at the state layer but then falls back to a shorter boundary because of the page alignment requirement of key-value blocks, causing already-computed states to be discarded. More commonly, the request queue order does not match the cache residency order, so a long-prefix state is swapped out while waiting, and when the next request arrives it can only recurse from the beginning, forming a “the busier it gets, the more it recomputes” positive feedback loop.

    The decision logic can be abstracted schematically as:

    1. def resolve_prefix(req, kv_pages, state_index):
    2. kv_len = kv_pages.match(req.tokens) # page-aligned KV hit length
    3. st = state_index.lookup(req.tokens) # prefix length of the state hit
    4. if st is not None and st > kv_len:
    5. # when the state hit is longer, KV blocks in [kv_len, st) must be rebuilt
    6. kv_len = rebuild_kv(req, kv_len, st)
    7. return min(kv_len, st), st

    What really determines success in engineering is whether this “rebuild” step can be batched and overlapped with transfer, not the hit detection itself.

    4. Actionable Refactoring Directions

    The first is unified accounting. Put key-value pages and state snapshots into the same reference counting and eviction framework, using “prefix fingerprint + length + layer type” as a composite key, to avoid the duplicate residency and premature eviction caused by two cache systems operating independently.

    The second is state snapshotting. Take periodic checkpoints of linear layer states, allowing rollback and recomputation between checkpoints, trading a small amount of recomputation for a large improvement in hit rate; snapshots must carry metadata (corresponding prefix length, layer structure version), and on cross-node transfer they should be validated before being mounted.

    The third is tiered cache coordination. Keep the hottest prefixes in GPU memory, use host memory as a second tier holding a larger prefix pool, and let a remote storage service hold only cold prefixes. The key is prefetching: predict the required prefix from historical traces as soon as a request enters the queue, and start the transfer early so that transfer latency is hidden behind computation.

    The fourth is coupling scheduling with caching. Factor “prefix residency” into the queue scoring to avoid a long prefix that was just reused being swapped out immediately; apply affinity scheduling to consecutive turns of the same session so they land on the same node as much as possible, reducing cross-node state migration.

    5. Where the Gains Come From and How to Evaluate

    The order-of-magnitude reference given in public research is: replaying real coding assistant traces on a single high-end accelerator card, post-refactor prefill throughput can reach roughly three times that of a general-purpose inference framework, and p90 time-to-first-token drops by nearly 90%; the gains still hold after adding host memory caching on the order of hundreds of gigabytes. It should be stressed that such numbers depend heavily on trace redundancy and cache capacity and should not be treated as a general promise.

    For a self-built evaluation, three metrics are recommended to be fixed: prefix hit rate (distinguishing key-value from state), effective compute share of prefill (real computation / total time), and p90 time-to-first-token. Also record the number of state recomputations, which is the direct signal for judging whether the snapshot period is reasonable. If recomputations rise sharply with concurrency, the eviction policy is too aggressive.

    6. Summary

    The benefit of hybrid architectures is on the decoding side; the risk is on the prefill side. Governing the two cache types in a unified way, making states snapshotable and migratable, and making scheduling aware of residency status is the key path to reliably obtaining throughput and latency gains under multi-turn long-context workloads.