Cache Redesign for Hybrid Architecture Models in Agent Workloads

    Hybrid architecture models combine full attention layers and linear layers, but conventional prefix caching often underperforms in multi-turn, long-context agent workloads. This article explains how to separate KV cache and linear-layer state, define independent invalidation rules, validate reuse in the scheduler, use two-tier storage and cross-node reuse, and verify the approach with reproducible tests. It also covers engineering considerations such as quantized-state error control, eviction, and data lifecycle.

    1. Why Caching for Hybrid Models Is Difficult

    Hybrid architecture models place full attention layers and linear layers in the same network. Linear layers make compute grow approximately linearly with sequence length and reduce the KV cache footprint, which makes them attractive for long-context scenarios. However, in multi-turn, long-context workloads such as coding agents, directly reusing the prefix-cache logic of traditional inference engines often fails to deliver the expected results.

    How should cache management work for hybrid models? Store KV cache and non-KV state separately, apply independent invalidation rules to each, and have the scheduler verify that a state is still valid before reuse.

    How can cache hit rates be improved for multi-turn long-context agent workloads? First, reduce whole-chain invalidation caused by prefix changes. For example, split reusable history segments into separate blocks and record a state version for each segment. Then apply tiered caching to hot states to reduce recomputation.

    1.1 Difference Between KV Cache and State Cache

    A hybrid model has two types of data that need caching. The first is the KV cache produced by full attention layers. It is arranged by token and can be matched by block. The second is the recurrent state of linear layers, which records cumulative information compressed from the input sequence. This state varies with model structure and batch size. In some configurations it is smaller than the KV cache, but it can still reach GB scale.

    Because linear-layer state is obtained step by step through recurrence, the state at position t depends on all previous positions. This means that if any position in the prefix changes, all states after that position must be recomputed.

    1.2 Three Concrete Problems in Agent Workloads

    Prefix shift invalidates the whole chain. Suppose the prefix of the first-round request is “system prompt, user request, result of tool call A.” In the second round, the agent changes a segment of code in the result of tool call A. At this point, the prefix hash no longer matches after the modified position, and both KV blocks and linear-layer states must be recomputed from the modification point. Agent tasks often involve repeated edits and rollbacks, so this situation is common.

    State cache is often ignored. Many inference engines cache only KV blocks and recompute linear-layer state from scratch every time. In long contexts, the amount of this recomputation grows with sequence length.

    Memory bandwidth may offset cache gains. After the KV cache becomes smaller, the bottleneck often shifts from GPU memory capacity to GPU memory bandwidth. If a cache hit requires moving an entire state segment, the transfer time may offset the compute time saved. Cache granularity should therefore be as fine as possible, moving only the blocks that are actually needed.

    2. Design

    The core of the design is to split the cache into two chains and have the scheduler check both for availability at the same time. The following four aspects explain the approach.

    2.1 Separate Storage Structures

    KV blocks and state blocks use different data structures and index fields.

    Type Main fields Invalidation granularity
    KV block block_id, layer_id, head_group, token range, prefix hash Invalidated by token block; all blocks after the modification position become invalid
    State block session_id, branch_id, step_id, layer_id, state_type, dtype, compression method, checksum, model version Invalidated by “session branch + step number”; all steps after the modification position in the same branch become invalid

    KV blocks can remain valid at token granularity. State blocks are invalidated as a whole by branch and step number, which gives coarser compute granularity but simpler metadata. With the two separated, a single modification does not force full invalidation on both chains at the same time.

    2.2 State-Aware Scheduler Matching

    After the scheduler receives a request, it checks whether reuse is possible in the following order:

    1. Use the prefix hash to look up candidate cache segments in the index.
    2. Check whether the candidate segment’s model version, quantization configuration, positional encoding parameters, and context window start match the current request. These parameters affect computation results, so reuse is not allowed if they do not match.
    3. Check whether the checksum of the state block matches the recorded value to prevent data corruption.
    4. Only after all three checks pass are the KV block and state block reused. If any check fails, recompute from the most recent valid position.

    One point needs clarification. After code order is swapped, the surface text is different, but the recurrent state result is usually different as well, so the two cannot be treated as directly equivalent. Equivalent reuse is allowed only when state updates satisfy provable conditions such as commutativity or mergeability and a checksum proves that the results are identical. Without such proof, this design performs exact matching only.

    2.3 Two-Tier Hierarchical Storage

    L1 (GPU memory) stores only recently accessed high-frequency state blocks. Capacity is determined by the GPU memory budget and can be estimated using “the number of state blocks required for a single batch of prefill” as a baseline. The eviction policy can use LRU combined with access-frequency weighting.

    L2 (CPU memory) stores the complete state of all historical sessions. The scheduler initiates an asynchronous copy when a request enters the queue, prefetching the required state from L2 to L1. The prefetch trigger can be set to “estimated queue wait time greater than the duration of a single copy.”

    Example parameters are as follows. Actual values need to be replaced with measured results from your own environment:

    • L1 capacity: reserve a fixed proportion of the GPU memory budget and adjust it based on the measured hit rate.
    • Prefetch trigger: start when queue time exceeds the duration of a single copy.
    • Effective PCIe bandwidth: measure it on the target machine with the vendor’s bandwidth test tool; do not use the nominal value.

    2.4 Cross-Node Reuse

    Cross-node reuse requires unified state naming and consistent write rules.

    Global ID format:

    tenant_id / model_id@model_version_hash / session_id / branch_id / step_number / layer_id / state_type

    • The step number is assigned by the scheduler according to request order within a branch and increases monotonically within each branch. Physical timestamps are not used, avoiding conflicts caused by clock skew across multiple nodes.
    • A new branch ID is assigned when a new branch is created. Parallel attempts in the same session therefore do not overwrite each other.
    • Tenant ID and model version hash are written into the ID to avoid prefix hash collisions between different tenants or different models.

    Write rules: State writes use copy-on-write. Existing blocks are not modified; new blocks are generated. Branch head pointer updates use compare-and-set. If two nodes update the same branch at the same time, the later committer sees a version mismatch and creates a new branch instead of overwriting existing data.

    Read rules: Check the local cache first. On a miss, access the CPU memory of the remote node through RDMA. If the remote node also misses, fall back to recomputation.

    Security requirements: The cache may contain user code and business data. The metadata service must check the requester’s tenant identity and block cross-tenant reads. RDMA access requires access control, and compliance requirements should determine whether cache data at rest is encrypted.

    3. Test Conclusions and Reproduction Method

    The original replay tests used real coding-agent traces and observed changes in prefill throughput and time to first token (TTFT). The results showed that in multi-turn replay, state-aware caching clearly improved both metrics. However, the exact multiples and percentages depend on the following conditions: the baseline engine version, GPU model and count, batch size, input and output length, and the definition used for cache hit rate. When these conditions are incomplete, the original numbers cannot be directly generalized to other environments.

    To verify the approach in your own environment, use the following method:

    1. Fix the baseline engine version and record all configuration parameters.
    2. Use the same batch of real traces and ensure that the input is identical for each replay.
    3. Warm up before each experiment, run it multiple times, and take the median and p90 values.
    4. Record KV hit rate, state hit rate, prefill throughput, p50 and p90 TTFT, and the share of cross-node fetches separately.
    5. Compare results with the design enabled and disabled, and confirm that output quality is consistent between the two.

    4. Engineering Implementation Considerations

    1. Integrate as a scheduling policy. The design does not need to replace the core of an existing inference engine. The scheduler decides when to use full-attention computation and which reuse path to take, while the cache manager provides available states. This division of responsibilities makes it easier to iterate incrementally on existing frameworks.

    2. Error control for quantized states. States can be stored in low-precision formats to save GPU memory, but an error upper bound must be determined first. The specific method is to compare output logit deviation and task success rate against a high-precision baseline. State blocks whose deviation exceeds the threshold are no longer reused and fall back to recomputation. The compression method must be written into metadata for later troubleshooting. Thresholds should be determined based on baseline tests of your own model.

    3. Eviction policy. When an agent task ends or a branch has not been accessed for a long time, first demote the state to L2 and set a time to live (TTL). Delete the state only when L2 also exceeds capacity and the state has not been accessed for a long time. Direct deletion would cause all subsequent similar requests to recompute.

    4. Data lifecycle and compliance. Cached data may contain sensitive code and user content. Define retention periods, support batch deletion by tenant or session, and avoid outputting the state content itself in logs.

    5. Conclusion

    Hybrid architecture models have clear compute and memory advantages in long-context scenarios, but their caching needs dedicated design. Separating KV and state, managing invalidation by branch and step number, using exact matching instead of unverifiable equivalence checks, and combining hierarchical storage with cross-node reuse form an approach that can be implemented incrementally.

    Cache management is therefore not only a storage problem. It is also affected by model structure, scheduling policy, and hardware topology, and all three need to be optimized together. Before launch, complete baseline comparison and quality validation on your own workload, then decide which parts to adopt.