Running Agents on Hybrid Architecture Models: Restructuring State Caching to Boost Prefill Efficiency

    In multi-round programming agent scenarios, processing long historical contexts during the prefill phase often creates latency bottlenecks. While prefix caching is a core optimization strategy, hybrid architecture models—combining full attention layers with linear attention or state space layers—introduce unique challenges due to their varying state structures. This article explores the characteristics of programming agent workloads, the difficulties of prefix caching in hybrid models, and a restructured caching approach using block-based KV caches and checkpoint state caches. It also covers hierarchical storage, cross-node reuse, performance evaluation, and a practical checklist for implementation.

    In multi-round programming agent scenarios, each request carries a lengthy historical context, including system instructions, code snippets, tool call results, and previous reasoning records. Recomputing these contents from scratch every time makes the prefill phase a latency bottleneck. Prefix caching avoids redundant computation by reusing pre-computed intermediate results, making it a core optimization for such workloads.

    1. Why Hybrid Architectures Have Become Mainstream

    The KV cache of full attention layers grows linearly with context length, causing significant memory footprint and bandwidth pressure in long sequences. Linear attention or state space model (SSM) layers use a fixed-size recurrent state instead of token-by-token KV, resulting in fast decoding and a smaller cache size. Interleaving these two types of layers in a certain ratio preserves the precise retrieval capability of full attention while compressing the cache size. This design has been adopted in multiple models as a common solution balancing efficiency and effectiveness.

    2. Characteristics of Programming Agent Workloads

    • Multi-round Incremental: Each round adds only a few new tokens, making the historical prefix highly repetitive.
    • Long Context: Codebase files, logs, and tool outputs can scale the context to a massive size.
    • Branching and Rollback: Agents may return to a historical node to retry, requiring the cache to hit prefixes at intermediate positions, not just at the end.
    • Concurrency and Burst: Multiple sessions run simultaneously, requiring dynamic scheduling of cache capacity between GPU memory and host memory.

    3. Challenges of Prefix Caching in Hybrid Models

    The KV cache of a pure Transformer has a favorable property: the KV of the first n tokens depends only on the first n tokens. Therefore, the KV can be split into token blocks, allowing any prefix’s KV to be saved independently and concatenated with other blocks.

    The state of linear layers is different. It is a recurrent compression of the entire prefix, lacking a token-by-token indexable structure within the state. To reuse the state of a specific prefix, a snapshot must be saved exactly at that position. If the snapshot position does not align with the requested prefix boundary, the state cannot be used directly.

    This introduces three engineering problems:

    1. Snapshot Granularity: Saving a snapshot for every token is too costly, while too large an interval reduces the hit rate, requiring a trade-off.
    2. Consistency: The KV of the full attention part and the state of the linear part must correspond to the same prefix position; otherwise, the output will be incorrect.
    3. Eviction Policy: The size and recomputation cost of the two types of caches differ significantly, making a unified eviction strategy often ineffective.

    4. Redesigning the Cache Structure

    The core idea is to split the cache into two categories and use a unified prefix position as the index key.

    • Block-based KV Cache: Stores the KV of full attention layers in fixed token blocks, supporting concatenation after partial hits.
    • Checkpoint State Cache: Saves the state of linear layers at block boundaries or strategically selected positions, allowing hits only at checkpoints.

    During a query, the system finds the longest prefix position that simultaneously has both types of caches, and continues computation from that position. During a write, after a request finishes, a checkpoint is written at the end position, and checkpoints are also written at high-reuse boundaries like system instructions.

    Schematic code (illustrating logic, not a specific API):

    1. def lookup_prefix(tokens, kv_store, state_store, block_size):
    2. # Search backward from the longest candidate prefix, requiring both KV blocks and a state checkpoint to exist
    3. for end in range(len(tokens), 0, -block_size):
    4. key = hash_prefix(tokens[:end])
    5. if key in state_store and kv_store.has_blocks(key, end):
    6. return end, state_store.get(key), kv_store.get_blocks(key, end)
    7. return 0, None, None

    This logic guarantees a key constraint: for any hit prefix, its KV and state must come from the exact same position.

    5. Hierarchical Storage and Cross-Node Reuse

    GPU memory is limited, so cold data can be offloaded to host memory or distributed storage. The design can focus on the following points:

    • Hierarchical Placement: Hot data stays in GPU memory, warm data in host memory, and cold data in distributed storage.
    • Prefetching: Proactively loads cache blocks back into GPU memory based on the scheduling queue to hide data transfer latency.
    • Cross-Node Reuse: A unified metadata service records the location of cache blocks, allowing different inference nodes to reuse the state of the same prefix. Model versions and key configurations must be verified to prevent incorrect reuse.

    In public experiments, the system integrated a 512GiB scale CPU-side cache and still showed significant gains, indicating that hierarchical storage remains valuable even under a large working set.

    6. How to Interpret Performance Data

    A research team replayed real programming agent session traces on a new generation of datacenter GPUs, reporting that prefill throughput reached approximately 3.14 times that of the baseline inference framework, and p90 time-to-first-token (TTFT) latency was reduced by up to 88.4%. When reading such data, note the following three points:

    1. Workload Determines Gain: The higher the prefix repetition rate, the more obvious the caching benefit. You need to verify this using your own session logs.
    2. Baseline Alignment: Ensure both comparison parties use the same model, hardware, and batching configuration.
    3. Tail Latency Separately: The p90 metric reflects the experience of long-tail users and better illustrates the impact of cache misses than the average.

    7. Implementation Checklist

    • First, use real session logs to calculate the prefix repetition rate and branching ratio before deciding on checkpoint positions and intervals.
    • Design a separate cache key for hybrid models, incorporating the model version and key configuration into the key-value pair to avoid incorrect reuse.
    • Sample and compare the output of cache hits with full computation to verify the correctness of the caching path.
    • Establish monitoring metrics for cache hit rate, eviction count, and load latency, and set alert thresholds.
    • Conduct stress tests in both single-node and cross-node scenarios to confirm consistency and fault recovery behavior.