Optimizing State Caching in Hybrid Architectures: 3x Prefill Throughput Improvement

    This article explains why hybrid architectures that combine full attention with linear layers create new prefix caching challenges, especially for coding agents with long contexts and repeated common prefixes. It presents a decoupled caching strategy with separate attention KV block caching and linear state checkpoint caching, along with checkpoint intervals, prefix fingerprinting, rollback, scheduling, and tiered storage. The discussion covers practical recommendations and expected gains, including higher cache hit rates, reduced repeated prefill, and lower p90 time to first token (TTFT).

    1. The Problem: New Caching Challenges in Hybrid Architectures

    Mainstream large language models are evolving from pure attention to hybrid architectures that combine full attention with linear layers. The main benefit is clear: linear layers, such as state space models and linear attention, have decoding compute complexity that is insensitive to context length. This makes decoding faster and reduces cache usage.

    The problem becomes more complex in workloads such as coding agents. A single task in an AI coding tool often involves dozens of rounds of tool calls, code retrieval, file reads and writes, and result feedback. Context length can quickly grow to tens of thousands or even hundreds of thousands of tokens. Each request carries a large reusable common prefix, such as the system prompt, repository structure, and conversation history. This requires the inference service to support efficient prefix caching.

    Prefix caching for pure attention models is relatively simple: cache the Key/Value tensors and reuse them by token position alignment. Hybrid architectures, however, maintain two types of state:

    • Attention layer state: strongly bound to specific token positions, with precise hit and reuse at block granularity.
    • Linear layer state: a hidden state that evolves as the sequence progresses. It depends on the accumulated history of the entire prefix and is difficult to split by position.

    As a result, traditional prefix caching strategies experience lower hit rates and higher management overhead on hybrid models. The prefill stage performs substantial repeated computation, and time to first token (TTFT) shows noticeable jitter.

    2. Core Idea: Manage the Two State Types Separately

    The first principle is divide and conquer: do not put attention state and linear state into the same cache abstraction. Instead, design their lifecycles and reuse granularity separately.

    2.1 Layered Cache Abstraction

    The cache is divided into two layers:

    • The attention KV block cache handles fine-grained, position-aligned exact reuse.
    • The linear state snapshot cache handles coarse-grained approximate reuse aligned by checkpoints.

    When a request arrives, prefix matching is performed first. Matched attention blocks are loaded directly, while a matched linear checkpoint is used as the initial state for further processing.

    2.2 Checkpoint Mechanism

    Linear state cannot be truncated arbitrarily. Therefore, state snapshots need to be periodically written at several fixed positions in the sequence. The checkpoint interval requires a tradeoff: too small an interval causes severe write amplification, while too large an interval leaves many tokens to recompute after a hit. In practice, a fixed token step is commonly used, with adaptive adjustment based on the context length distribution of the workload.

    2.3 State Alignment and Rollback

    Because linear state is cumulative, restoring from a checkpoint requires an exact prefix match. A prefix fingerprint, such as a hash based on block content, is therefore needed to verify consistency. If verification fails, the system falls back to an earlier checkpoint to avoid contaminating subsequent generation with an incorrect state.

    The following is an illustrative cache lookup logic:

    1. def lookup_prefix(blocks, ckpts, req):
    2. # blocks: attention KV block cache, indexed by block hash
    3. # ckpts: linear state checkpoint cache, indexed by prefix fingerprint
    4. hit_blocks = match_blocks(blocks, req.token_ids) # longest prefix block match
    5. fp = fingerprint(req.token_ids[:len(hit_blocks)])
    6. state = ckpts.get(fp) # get the corresponding linear state snapshot
    7. if state is None: # fall back on a miss
    8. state, start = initial_state(), 0
    9. else:
    10. start = len(hit_blocks)
    11. return hit_blocks, state, start # continue prefill from start

    3. Key Engineering Techniques for Improving Prefill Throughput

    3.1 Improve Cache Hit Rate

    Coding agent trajectories are highly repetitive: the same repository context is carried repeatedly. Using content addressing rather than session addressing allows common prefixes shared across sessions and users to be reused, significantly improving the hit rate.

    3.2 Reduce Repeated Prefill

    After a checkpoint hit, only the unmatched tail tokens need to be prefilled. Linear layers in hybrid models are already inexpensive. The truly expensive part is repeated attention computation, which block-level reuse directly addresses.

    3.3 Coordinate Batching and Scheduling

    Incorporating cache hit length into scheduling decisions allows requests with longer hits to be prioritized, smoothing the TTFT tail. Chunked prefill can be used for requests with no hit to prevent long requests from blocking short ones.

    3.4 Tiered Storage and Capacity Expansion

    GPU memory stores hot blocks and recent checkpoints, while host memory or a distributed cache pool stores the full set of checkpoints. When a large CPU-side cache is introduced, cross-tier transfer may be required on a hit. The end-to-end benefit still holds because the saved recomputation cost is much higher than the transfer cost. In cross-node scenarios, consistency and version management of state snapshots are required to avoid reading stale checkpoints.

    4. Practical Recommendations and Expected Benefits

    • Enable this approach first on workloads with multiple rounds, long contexts, and a high proportion of common prefixes, where the benefits are most obvious.
    • Make the checkpoint step configurable and adjust it dynamically based on workload characteristics.
    • Implement prefix fingerprint verification to prevent state mismatch from degrading generation quality.
    • Monitoring should cover cache hit rate, checkpoint hit distribution, and p50/p90 TTFT, rather than average throughput alone.

    Overall, redesigning the hybrid state cache substantially eliminates repeated computation in the prefill stage. In real coding agent trajectory replays, prefill throughput can reach several times that of conventional approaches, while p90 TTFT drops significantly. The gains remain stable even with a large tiered cache. This approach provides direct reference value for building low-latency, high-concurrency AI coding and agent inference services.