State Caching for Hybrid Architecture LLMs: The Key to Faster Prefill

    This article explains why hybrid LLM architectures, which interleave full attention layers with linear attention or state-space layers, complicate prefix caching. It describes how treating linear states as first-class cache entries, checkpointing at chunk boundaries, and scheduling prefill around cache reuse can reduce the cost of re-prefilling long prefixes in multi-turn agent workloads. It also covers checkpoint granularity, eviction, tail-latency monitoring, numerical consistency, and benchmarking.

    1. Why Hybrid Architectures Are Becoming Common

    Over the past two years, a number of large models have moved away from pure Transformer designs with global attention. Instead, they stack full attention layers and linear attention layers (or state-space-style layers) in alternation. This design has three main advantages.

    First, decoding is faster. A linear layer maintains only a fixed-size state during inference. It does not need to read and write a KV cache that grows with the context. As a result, memory bandwidth pressure in the decoding stage is reduced.

    Second, the cache is smaller. The size of the linear state depends on the model’s hidden dimension, not on sequence length. The longer the context, the more this advantage matters.

    Third, retrieval ability does not drop noticeably. The small number of retained full attention layers handle precise long-range retrieval, which offsets the weakness of pure linear attention on “needle in a haystack” tasks.

    This type of architecture is now common in models built for agents and long-context workloads.

    2. Saved Bandwidth Shifts the Burden to Cache Management

    Hybrid architectures save bandwidth and memory, but they make cache management in the inference system more complex. A coding agent session often includes dozens of tool calls and code edits. Most of the prefix in each request is identical to the previous one, so prefix caching can avoid a large amount of repeated computation.

    Applying prefix caching to hybrid models raises three problems.

    1. The cache structures differ. Full attention layers store KV blocks, while linear layers store accumulated states. The two differ in lifecycle, granularity, and update method, so the system needs a unified abstraction to manage both.

    2. States are hard to replay partially. KV cache can grow in chunks, with the KV of new tokens appended after the existing KV. A linear state, however, is a continuously accumulated variable. After a prefix hit, computation must resume from the hit position, so the state must be saved and restored precisely. Small deviations in numerical precision or computation order can make later outputs differ from the result of recomputing the whole sequence.

    3. Recomputing long prefixes is expensive. Agent contexts often reach tens of thousands of tokens or more. If the uncached part of a prefix must still be prefilled from the beginning after a cache hit, time to first token (TTFT) gets worse as the session grows. The better approach reuses the cached portion, including the linear state, and computes only the new tokens.

    3. Treating State as a First-Class Citizen

    One class of new caching approaches places linear states on the same footing as KV. Both types go into the same prefix cache. The approach has four parts.

    1. A unified chunking abstraction. KV blocks from full attention layers and states from linear layers are aligned to the same token boundaries and organized into a prefix tree. Each tree node stores both types of data, and prefix matching treats them equally.

    2. Checkpoints at block boundaries. The system saves a snapshot of the linear state at each block boundary. After a cache hit, it restores the state from the nearest checkpoint and recomputes only the tokens after that point. This avoids accumulating the state from the beginning. It also avoids saving a snapshot for every token, because that would add heavy storage and write overhead.

    3. Cache-aware prefill scheduling. Before prefill, the system matches the prefix and splits the request into a reuse segment and an incremental segment. The reuse segment only loads KV and state. Compute units process only the incremental segment.

    4. Tiered storage. Hot data stays in GPU memory, warm data in CPU memory, and cold data in remote storage. In multi-machine deployments, a cross-node state reuse mechanism is also needed. This lets a session find its cached prefix no matter which instance it is routed to.

    The general request flow is shown below. The pseudocode omits error handling and concurrency control. It only illustrates the data flow.

    1. def prefill(request, init_state):
    2. node, matched_len = prefix_tree.match(request.tokens)
    3. # match only returns nodes with checkpoints, so matched_len is always
    4. # an integer multiple of CHUNK_SIZE
    5. if node is not None:
    6. restore_kv(node)
    7. state = restore_linear_state(node.checkpoint)
    8. start = matched_len
    9. else:
    10. state = init_state()
    11. start = 0
    12. # Incremental segment: compute only the new tokens
    13. tail = request.tokens[start:]
    14. for chunk in split(tail, CHUNK_SIZE):
    15. update_kv(chunk)
    16. state = update_linear_state(state, chunk)
    17. prefix_tree.insert(chunk, state) # Save a checkpoint at the block boundary
    18. return decode(request)

    If the prefix tree can only match a position without a checkpoint, the system should fall back to the nearest checkpoint. It then includes the remaining tokens in the incremental segment and recomputes them.

    4. Practical Recommendations

    Choose the architecture based on the workload. Multi-turn agent scenarios with high prefix repetition benefit most. Single-turn long-document tasks have little reusable prefix, so the gains are much smaller. Before choosing this approach, measure the prefix repetition rate of real requests, then decide whether the cache redesign is worth the effort.

    Balance checkpoint granularity. Smaller blocks allow finer recovery, but each block needs a state snapshot, which increases write and storage overhead. Larger blocks mean fewer snapshots, but reuse may require recomputing a longer incremental segment. The best granularity depends on the model dimensions, the efficiency of the block compute kernels, and the memory budget, so measure it on your own hardware. A few hundred to a few thousand tokens is a common starting point, but do not treat it as a universal default.

    Design eviction around rebuildability. Linear states must be recomputed from the token sequence before they can be restored. Unlike KV, they cannot be fetched directly from a corresponding cache. Cold data can therefore keep only metadata, such as the token range and checkpoint information, and be recomputed when needed. This saves storage but increases the computation required on a hit.

    Monitor tail latency. Interactive agent experiences are often shaped by p90 and p99 TTFT, and averages can hide these problems. Set capacity planning goals around tail latency.

    Verify numerical consistency separately. Restoring linear state from a snapshot depends on deterministic computation. Different precisions, parallel partitioning schemes, and operator implementations can all cause small differences between the restored result and full recomputation. Before going live, compare the output of the restore path with the output of the full recomputation path.

    5. Reproducibility Checklist

    When evaluating this approach in your own environment, record the following information. Without these details, performance numbers are hard to compare.

    • Model architecture: ratio of full attention to linear layers, hidden dimension, state dimension
    • Workload characteristics: typical context length, turns per session, prefix repetition rate
    • Batch size and concurrency
    • Hardware: GPU model, GPU memory capacity, CPU memory capacity, and interconnect bandwidth
    • Software: inference framework and version, operator library version, precision settings
    • Metrics: prefill throughput, TTFT at p50, p90, and p99, and cache hit rate
    • Baseline: the baseline framework under the same workload, and whether prefix caching was enabled

    6. Common Questions

    What happens if only KV is cached and the linear state is not? After a hit, the linear layers still have to be computed from the beginning. Only part of the prefix caching savings remains, so the TTFT improvement is limited.

    What problems come from setting checkpoints too densely? Snapshot write and storage overhead can consume the gains. Snapshots also occupy GPU memory, which reduces the space available to other requests.

    Is it enough to look only at average latency? No. Averages hide queuing and long-tail request problems. The stutters that agent users notice usually show up at p90 and p99.

    Do all models suit this approach? No. The model must use a hybrid architecture, and the workload must have significant prefix repetition. Pure Transformer models can use KV-only prefix caching.

    7. Summary

    Hybrid architectures give the bandwidth and memory gains to the model and leave the complexity of cache management to the inference system. Managing linear state and KV together, checkpointing at block boundaries, and recomputing when needed is a practical path for making hybrid architectures work well in long-context agent workloads. Whether to adopt it should depend on the prefix repetition rate and tail latency measured on your own workload.