State Cache Refactoring for Hybrid-Architecture LLMs: A Practical Guide

    This article explains why hybrid architectures that alternate full-attention layers with linear-state layers complicate prefix caching, and how to refactor state management to fix it. It covers modeling attention state and linear state as separate cache objects, keying and pooling them independently, building a capacity hierarchy across GPU memory, host memory, and distributed storage, and adapting prefill scheduling to turn repeated prefilling into incremental prefilling. It also details p90 time-to-first-token tuning, observability metrics, capacity planning formulas, and a three-step rollout path that delivers higher prefill throughput and lower first-token latency without changing the model architecture.

    1. Background: Why Hybrid Architectures Make Caching Harder

    Recent model designs increasingly adopt a hybrid architecture that alternates full-attention layers with linear-state layers. The motivation is straightforward: full-attention layers handle precise recall of historical information, while linear-state layers replace the ever-growing key-value cache with a fixed-size state, reducing memory footprint and improving compute efficiency during decoding. For decoding alone, the gains are clear.

    The picture changes under workloads such as AI coding assistants. These workloads are characterized by multi-turn conversations, extremely long contexts, and large repeated prefixes (the same code repository, the same system prompt, the same set of tool definitions), with only a small amount of new content appended each turn. This pattern of a highly overlapping prefix and a continuously appended suffix is exactly what prefix caching is best suited for.

    The conflict is that a pure-attention model has only one kind of cache object: the key-value cache, where appending, eviction, and reuse are relatively intuitive. A hybrid architecture, however, holds two kinds of state at once:

    • Attention state: grows with sequence length, can be split into blocks, and can be reused by prefix position.
    • Linear state: compressed from the entire history, fixed in size, and semantically dependent on sequential accumulation from the beginning.

    The two kinds of state differ completely in lifecycle, addressing scheme, and migration cost. Managing them with a single caching strategy causes reuse misalignment: either the linear state is recomputed repeatedly, or attention blocks are retained needlessly. In real trace-replay tests, the industry has observed that prefill in hybrid models becomes the bottleneck instead, with a particularly pronounced long tail in time to first token. A generalized approach proposed by one research team shows that after redesigning cache management for the two kinds of state, prefill throughput can reach roughly three times the baseline of a general-purpose inference framework, and p90 time to first token can drop by nearly 90% at best; the gains persist after attaching a CPU-side cache on the order of hundreds of gigabytes, and the approach extends to distributed storage and cross-node reuse.

    2. Core Insight: Treat State as Two Object Types, Governed Separately

    The first step of the refactor is not writing code but redefining the cache objects. Model state explicitly as two resource types:

    1. StateBlock (attention state block)
    2. key = hash(prefix_tokens, layer_range, block_index)
    3. size ∝ sequence length
    4. reuse = exact hit by prefix position
    5. LinearState (linear state snapshot)
    6. key = hash(prefix_tokens, layer_range, state_version)
    7. size = constant (independent of sequence length)
    8. reuse = whole-object hit only, position sensitive

    This yields three design principles:

    1. Separate pools. The two state types use independent allocators, eviction policies, and persistence channels so they do not compete for GPU memory.
    2. Separate keying. Attention blocks are addressed by position; linear state is addressed by prefix version. Cross-version concatenation is forbidden.
    3. Separate cost-based scheduling. Linear state has strong sequential dependencies and a high recomputation cost, so it should be reused first; attention blocks can be recomputed in parallel and serve as a secondary fallback.

    3. Cache Tiering: GPU Memory, Host Memory, and Distributed Storage

    Reusing hybrid state requires a capacity gradient rather than a single tier.

    • L1 GPU memory pool: holds only the attention blocks of the current request’s hot prefix and the latest linear state snapshot. Small capacity, lowest latency.
    • L2 host memory pool: absorbs session-level reuse at the hundreds-of-gigabytes scale, keeping the historical state of multi-turn conversations locally to avoid repeated prefilling.
    • L3 distributed/object storage: persists state by session or repository dimension, enabling cross-node and cross-instance reuse.

    One engineering caveat: linear state is small and high-value, so it should stay resident long term; attention blocks are large and need hot/cold tiering at block granularity, with prefetching that pulls the blocks likely to be hit in the next turn into GPU memory ahead of time.

    4. Prefill Scheduling: Making Repeated Compute Actually Disappear

    Tiered caching still needs scheduling support; otherwise the hit rate gets eaten by batching policy.

    1. # Illustrative: hybrid-state-aware prefill scheduling
    2. def schedule(requests, cache):
    3. hits, misses = [], []
    4. for r in requests:
    5. h = cache.lookup_linear(r.prefix) # whole-object hit first
    6. b = cache.lookup_blocks(r.prefix) # then block-level hits
    7. (hits if h else misses).append((r, h, b))
    8. # Skip prefill for the hit portion; compute only the missed incremental blocks
    9. for r, h, b in hits:
    10. r.prefill_tokens = r.total - covered(b)
    11. # Sort missed requests by descending prefix length to improve block reuse for later requests
    12. misses.sort(key=lambda x: -len(x[0].prefix))
    13. return hits + misses

    Three actions matter: skip attention computation for the already-hit prefix, reuse linear state to avoid accumulating from the beginning, and sort missed requests by prefix to increase block-level sharing. Only when the three are combined does repeated prefilling turn into incremental prefilling.

    5. Long-Tail Latency: p90 Is What Users Feel

    The pain in time to first token usually lies in the long tail, not the mean. Sources of the tail include cold starts for new sessions, full recomputation after a cache miss, and network jitter when pulling state across nodes. Actionable mitigations:

    • Warm up: keep high-frequency system prompts and tool definitions resident in L1/L2.
    • Prefetch: pull the blocks needed for the next turn ahead of time based on session turn patterns.
    • Degrade: when a state fetch times out, fall back to chunked parallel recomputation instead of waiting on a serial full recompute.
    • Isolate: move large state migrations to a separate thread pool so they do not block the main inference pipeline.

    6. Observability and Capacity Planning

    Before launch, establish three metric groups: hit rate (measured per tier), state migration bytes and duration, and the ratio of prefill tokens to actually generated tokens. For capacity planning, linear state can be estimated fairly accurately as concurrent sessions multiplied by single-snapshot size; attention blocks need headroom based on average prefix length multiplied by concurrency multiplied by per-token block size, plus a 20%–30% reserve for bursty long contexts.

    7. Implementation Recommendations

    To apply this in your own inference service, proceed in three steps: first split the state objects and introduce separate keying, then verify the hit-rate improvement; next add a host memory tier to absorb multi-turn sessions; finally extend to distributed storage for cross-node reuse. If you run on a container platform, you can combine object storage for state persistence with logging services and monitoring/alerting for hit-rate and long-tail observation, thereby gaining both prefill throughput and first-token latency benefits without changing the model architecture.