Optimizing State Caching in Hybrid Architectures: 3x Prefill Throughput

    This article explains why prefix caching becomes more complex in hybrid model architectures that combine full attention and linear layers. It breaks down three core conflicts—state heterogeneity, lifecycle mismatch, and asymmetric storage costs—and presents a unified hybrid state caching design based on blocks, dual indexing, differentiated eviction, and version consistency. It also covers prefill-stage optimizations, tiered caching, and distributed reuse, along with a practical rollout path. In real agent trajectory replays, this approach improved prefill throughput to about 3x the baseline and reduced p90 time-to-first-token by up to 88%.

    1. Background: Why Hybrid Architectures Complicate Prefix Caching

    Mainstream large model architectures are shifting from pure attention to hybrid forms that combine full attention with linear layers. The core benefit is clear: linear layers (such as state space models and linear attention) have compute and memory costs during decoding that do not grow linearly with context length, making decoding faster and caches smaller. Retaining a small number of full attention layers preserves long-range dependency handling and precise retrieval.

    However, agent workloads such as AI coding tools behave very differently from single-turn question answering:

    • Multi-turn interaction: user instructions, tool call results, and code snippets are repeatedly appended within the same session;
    • Long context: repository-level code, logs, and error stacks can easily reach tens or even hundreds of thousands of tokens;
    • High prefix reuse: system prompts, project descriptions, and conversation history repeat heavily across requests.

    Under this workload, prefix caching in a pure Transformer is relatively straightforward: on a hit, the same KV tensors are reused. In hybrid architectures, each layer has a different state shape—attention layers cache KV that grows with length, while linear layers cache a fixed-size recurrent state. The two state types differ in size, lifecycle, eviction cost, and transfer cost. Applying a single caching strategy leads to problems where cached data cannot be used or reused data produces incorrect results, wasting substantial compute on repeated work during prefill.

    2. Three Core Conflicts in Hybrid State Caching

    2.1 State Heterogeneity: One Policy Cannot Fit All Layers

    Attention layer cache size is proportional to sequence length, while linear layer cache size is independent of length. Managing both with a unified capacity quota and eviction policy inevitably causes one resource type to be over-evicted or held too long.

    2.2 Lifecycle Mismatch: A Prefix Hit Does Not Mean the State Is Usable

    A prefix cache hit requires the prefix tokens to match exactly. However, a linear state is a compressed representation of the prefix and is more sensitive to small differences. Once chunk boundaries, truncation points, or how tool outputs are concatenated change, the state must be recomputed, and recomputation is far more expensive than ordinary KV reuse.

    2.3 Asymmetric Cost of Tiered Storage

    GPU memory is expensive, CPU memory is cheaper, and remote storage is cheaper still but has higher latency. Attention KV is large and suitable for offloading; linear states are small but costly to recompute, making them better suited to faster tiers. Without differentiated placement, both hit rate and latency degrade.

    3. Design Approach: Schedule Two State Types as Two Resource Classes

    The goal is not to optimize one cache in isolation, but to establish a unified hybrid state caching abstraction: use the block as the smallest unit, carrying both the attention KV and the linear state for that block, with metadata attached to each state type (length, chunk boundaries, compute version, source node).

    Key design points include:

    • Chunk alignment: split the prefix at fixed token boundaries to ensure hits align across requests;
    • Dual indexing: index separately by prefix hash and state fingerprint to avoid cases where the prefix hits but the state is invalid;
    • Differentiated eviction: prioritize offloading large KV to lower-cost tiers and keep small states resident;
    • Version consistency: invalidate state versions when model weights or chunking strategy change, preventing stale reuse.

    Illustrative pseudocode:

    1. class HybridStateBlock:
    2. prefix_hash: str # Fingerprint of the prefix content
    3. kv_cache: Tensor # Attention layer cache, grows with length
    4. linear_state: Tensor # Linear layer cache, fixed size
    5. version: str # Model and chunking strategy version
    6. def lookup(block):
    7. hit = index.get(block.prefix_hash)
    8. return hit if hit and hit.version == block.version else None

    4. Key Prefill-Stage Optimizations

    Prefill is the main source of time-to-first-token latency. In hybrid architectures, the optimization focus is threefold:

    1. Skip on hit: blocks that hit skip forward computation entirely, and only the missed tail undergoes incremental prefill;
    2. Chunked pipelining: split long prefixes into chunks and compute while loading to hide CPU-to-GPU transfer latency;
    3. State-first loading: fixed-size linear states are small, so prefetch them to GPU memory first to reduce waiting on the critical path.

    Engineering-wise, batching and memory fragmentation also matter: hybrid cache blocks vary in size and can cause fragmentation, so they should be managed in separate pools by state type.

    5. Tiered Caching and Distributed Reuse

    Adding a large CPU memory cache still yields significant gains, confirming that tiering is the right direction. Practical recommendations:

    • Keep hot data (system prompts, project context) resident in GPU memory;
    • Place warm data (earlier turns) in CPU memory;
    • Offload cold data to remote storage with asynchronous prefetching;
    • In multi-node deployments, use content fingerprints for a global index to enable cross-node state reuse and avoid recomputing the same prefix on multiple nodes.

    6. Results and Implementation Recommendations

    In real agent trajectory replays, this approach raised prefill throughput to about 3x the baseline and reduced p90 time-to-first-token by up to 88%. A three-step rollout is recommended: first unify chunking and metadata, then implement dual indexing and differentiated eviction, and finally integrate tiered storage and cross-node reuse. For teams building coding agents on hybrid architecture models, this method significantly reduces compute waste in long-context scenarios and is a cost-effective path to better interactive experiences.