Hybrid Architecture Models and Prefix State Caching for AI Agents

    This article explains why AI agents spend much of their time re-prefilling repeated history, and why hybrid models that combine full attention layers with linear recurrent layers need more than standard prefix caching. It covers unified checkpoints that keep KV caches and recurrent states consistent, the trade-offs between tiered storage and cross-node reuse, and how to evaluate and roll out these optimizations.

    1. Agent inference is shifting its bottleneck from generation to repeated prefill

    When agents run coding, retrieval, and data analysis tasks, they usually do not complete just one question-and-answer exchange. They repeatedly read the message history, process tool outputs, update plans, and then continue generating. As the task progresses, the context in each request may grow longer, yet two consecutive rounds often differ only by a newly appended segment at the end.

    If every round recomputes the entire history, the system repeatedly consumes a large amount of compute. Users see this as a long wait before the model starts answering, even though the final output is short.

    This wait mainly occurs in the prefill stage. The model must process the input tokens and build the internal cache needed for subsequent generation. The role of prefix caching is to store history that has already been computed, so a new request only processes the part that has not yet been computed.

    However, prefix caching is not simple text deduplication. Different layers of a model differ in what internal data can be reused, how that data is restored, and what consistency is required. When a model contains both full attention layers and linear recurrent layers, the cache system must evolve from storing historical keys and values to managing the complete execution state of a hybrid model.

    2. Why hybrid architectures need two kinds of cache

    1. Full attention layers store historical keys and values

    Full attention layers usually let the current token attend to the keys and values of historical tokens. As the context grows, the key-value (KV) cache generally grows with it.

    For these layers, prefix reuse is straightforward. If two requests share exactly the same prefix, the system can reuse the KV data at the corresponding positions and append new cache entries for the newly added tokens.

    Block-based management helps the system share common prefixes and reclaim part of the cache when capacity runs short. Block size, boundary alignment, and reference counting all affect how efficiently reuse actually works, however.

    2. Linear recurrent layers store compressed state

    Linear attention and state space structures typically summarize historical information through a recurrent state. The following general form illustrates the idea:

    State update: St = F(S{t-1}, x_t)

    Output computation: y_t = G(S_t, x_t)

    Here, x_t is the current input, and S_t is the internal state after processing up to the current position. The update function, state shape, and auxiliary data differ across specific models. This notation only illustrates the recurrence.

    Compared with historical keys and values that grow token by token, the main recurrent state of these structures usually does not grow linearly with sequence length. This helps control storage overhead for long contexts.

    Compression also brings an important limitation. Saving the state at the end of a sequence does not mean the state at any earlier position can be recovered. Recurrence usually cannot be simply reversed. To continue execution from an earlier position, the system must have a checkpoint at that position or recompute from an earlier checkpoint.

    3. Both cache types must correspond to the same prefix position

    Correct recovery for a hybrid model requires two conditions:

    • The full attention layers hold valid KV caches up to the target position.
    • The linear recurrent layers hold the complete state after processing the same position.

    If the KV cache covers a longer prefix while the recurrent state remains at an earlier position, the two cannot be combined directly into a usable execution context.

    A hybrid cache hit therefore depends on finding a prefix boundary that can be restored consistently, not on the number of cache blocks found.

    3. Why agent workloads expose this problem so easily

    1. Repeated appends create substantial reuse opportunities

    A typical agent repeatedly carries system instructions, task descriptions, conversation history, and tool results. Consecutive requests may share a long common prefix, so the reuse value is clear.

    Cache matching, however, must operate on the token sequence that actually enters the model, not on text that merely looks the same. Changes in message formatting, templates, whitespace, or tokenizer configuration can all affect prefix consistency.

    2. Branching and retries require retained history checkpoints

    An agent may try multiple approaches from the same history node, or retry after a tool execution fails. In these cases, keeping only the latest recurrent state is not enough, because several branches may need to continue from an older position.

    The state for a shared prefix should be saved as a read-only snapshot. When a branch continues computing, it should use copying or another safe isolation mechanism, so that one request does not modify state that another request is reusing.

    3. Long-tail requests amplify the cost of cache recovery

    On a cache miss, a long-context request must perform extensive prefill computation. Even on a hit, if the state sits in a slower storage tier, queuing, transfer, and deserialization can add waiting time.

    Average hit rate therefore cannot fully explain user experience. Teams must also measure recovery time for long-context requests and high-percentile time to first token.

    4. Unified checkpoints: turning scattered caches into recoverable state

    1. A checkpoint must cover all necessary information

    A recoverable hybrid checkpoint must describe at least:

    • The number of processed token positions.
    • The KV blocks for each full attention layer and their valid ranges.
    • The complete state of each linear recurrent layer.
    • The model weight version and execution configuration.
    • Data types, layouts, and compatibility information.
    • Prefix identifiers, access permissions, and lifecycle information.

    Some recurrent structures also depend on local windows, convolution buffers, or other auxiliary state. Saving only the main state tensors may not be enough for correct recovery.

    Checkpoints should be published only after all layer data is ready, so that no request reads state that has not been fully written.

    2. Checkpoint density involves trade-offs

    Denser checkpoints make it more likely that the system finds a checkpoint close to the longest common prefix of a new request, which reduces recomputation. They also increase state copying, index maintenance, and storage use.

    Sparse checkpoints create a different problem. The system may have saved a long KV prefix but can only continue from an earlier recurrent state, so a middle segment must still be re-executed.

    In practice, teams can combine fixed intervals with semantic boundaries. For example, they can place candidate checkpoints at a set token interval, at the end of complete messages, or at the end of tool results. The specific strategy should be validated against state size and the actual reuse distribution, not chosen based on context length alone.

    3. A hit may still require partial recomputation

    Suppose a new request shares the first 8,000 tokens with a historical request, but the latest consistent checkpoint is at token 7,000. The system must still process tokens 7,001 through 8,000 before it can continue with the new content.

    Cache evaluation should therefore focus on how much computation was actually skipped, not only on whether relevant cache was found.

    5. Tiered storage: trading capacity for hit rate, while accounting for transfer cost

    1. Each tier plays a different role

    Accelerator memory suits frequently accessed cache that will be used soon. Host memory can hold more historical prefixes. Distributed storage fits larger-scale saving and cross-node reuse.

    The farther a tier is, however, the more transfer bandwidth, restore queuing, metadata lookup, and failure retry the system must account for. Adding cache capacity does not necessarily reduce latency.

    2. Compare restoration against recomputation

    A simple decision rule applies:

    Remote reuse is more likely to be beneficial only when lookup time + transfer time + restore time is less than the avoidable recomputation time.

    For short prefixes, low-bandwidth links, or congested periods, local recomputation may be faster. For long prefixes with high computation cost, loading existing state