Rebuilding State Caching for Hybrid-Architecture LLMs: A Block-Based Approach

    This article examines why hybrid LLM architectures—combining full attention layers with linear state layers—break traditional KV cache management, and how to rebuild caching around a unified block abstraction. It explains the semantic differences between concat-friendly KV caches and non-concat linear state caches, then walks through four core design changes: block alignment, prefix-tree indexing, sequential state replay instead of concatenation, and differentiated eviction. It also covers optimizations for coding agent workloads and a practical rollout path for validating the approach on your own inference service.

    In recent open-source LLM designs, linear attention and state space layers have moved from experimental components to standard building blocks. A typical pattern uses gated linear state updates in most layers while keeping full attention computation in a few layers, producing a hybrid structure of full attention plus linear layers. The motivation is clear: decoding no longer requires a key-value cache that grows linearly with context length, memory usage drops significantly, and long-sequence decoding throughput improves.

    The engineering reality, however, is that hybrid architectures do not eliminate the caching problem—they just reshape it. Full attention layers still keep a traditional key-value cache, while linear layers maintain a fixed-size recurrent state. The two cache types have completely different lifecycles, invalidation conditions, and migration costs, which means hit detection, reuse granularity, and eviction policies for prefix caching all have to be redesigned. For workloads such as AI coding tools—where the same repository context reappears constantly, tool calls happen across multiple turns, and a single session can easily reach hundreds of thousands of tokens—the quality of the caching strategy is amplified into a multiple-fold difference in throughput.

    2. The Fundamental Difference Between the Two Cache Types

    Before understanding why a rebuild is necessary, it helps to be precise about the semantics of each cache type.

    A traditional key-value cache is concat-friendly: it stores the key and value vectors for every historical token. As long as the prefix tokens of a new request match exactly, the cache can be attached in front directly, and the result is semantically equivalent to recomputation.

    A linear state cache is not concat-friendly: it maintains a fixed-size matrix state that is a compressed summary of the historical sequence. Its update is lossy and order-sensitive, so it cannot be split or appended at arbitrary token boundaries the way key-value caches can.

    This leads to three direct consequences:

    • Hit detection becomes more complex: an identical prefix does not mean the state is reusable—you also need the block boundaries to align.
    • Reuse granularity is limited: reuse can only happen at block granularity; any mismatch inside a block forces a full block recomputation.
    • Eviction is more costly: the state is small in size but expensive to recompute, so a simple size-based LRU policy is not reasonable.

    3. Core Design: A Unified Block-Based Cache Abstraction

    The widely accepted rebuild direction is to manage both cache types under a single “block” abstraction. It boils down to four points.

    1) Block alignment. Split into fixed token-count blocks. Each block stores both the full-attention key-values for that segment and a snapshot of the linear state at the end of that segment. Cross-request reuse is allowed only when block boundaries align.

    2) Prefix-tree indexing. Organize blocks with a prefix tree. Identical paths count as a hit; reuse goes as deep as the deepest matching node, and the remainder enters a pending-computation queue.

    3) State recomputation instead of concatenation. Unmatched blocks must be replayed sequentially, advancing block by block from the most recent hit state, so the order semantics of state updates are preserved.

    4) Differentiated eviction. Evict full-attention caches based on size and reuse frequency. Linear states are small but costly to recompute, so they should be retained longer, using a weighted score that combines reuse count and recomputation cost.

    A sketch of the hit and replay logic:

    1. def match_and_replay(prefix_tokens, block_size, cache):
    2. node, hit_len = cache.tree.walk(prefix_tokens, block_size)
    3. state = node.state_snapshot # linear state of the most recent hit block
    4. kv = node.kv_cache # corresponding full-attention key-values
    5. for start in range(hit_len, len(prefix_tokens), block_size):
    6. chunk = prefix_tokens[start:start + block_size]
    7. state, kv_chunk = model.forward_chunk(state, kv, chunk)
    8. kv.append(kv_chunk) # advance sequentially; concat is not allowed
    9. cache.tree.insert(start, chunk, state, kv_chunk)
    10. return state, kv

    4. Additional Optimizations for Agent Workloads

    Coding agent workloads have three characteristics worth optimizing for. First, the system prompt and repository context are highly repetitive, making them ideal for a highest-priority resident cache. Second, tool call results are appended continuously, which suits incremental block insertion rather than full-segment rebuilding. Third, multiple sessions share the same codebase, so common prefix blocks can be reused across sessions.

    Two engineering caveats remain: block size must trade off hit granularity against recomputation cost—too small raises indexing overhead, too large wastes work on mismatches; and state snapshots must control copy frequency so serialization overhead does not cancel out the gains.

    5. Implementation Recommendations

    To validate this kind of approach on your own inference service, a suggested path is: first use an offline dataset to measure prefix overlap and block hit rates, then load-test throughput and memory curves across different block sizes, and finally roll out gradually. At the infrastructure layer, you can deploy inference instances on a container platform, store cache snapshots in object storage, and use logging and monitoring with alerting to track hit rate, recomputed block count, and tail latency. Baidu AI Cloud offers corresponding product categories for these general capabilities and can serve as an infrastructure selection reference.

    Summary: Whether the benefits of hybrid architectures materialize depends on switching cache management from a “key-value concatenation” mindset to a “state block reuse” mindset. Get block alignment, prefix indexing, sequential recomputation, and differentiated eviction right, and prefill throughput in long-context agent scenarios has stable room to improve.