State Cache Optimization for Hybrid-Architecture LLMs: A Practical Guide
This article examines the caching challenges introduced by hybrid LLM architectures that combine full attention layers with linear layers. It explains the semantic differences between addressable KV cache and path-dependent recurrent states, then presents a three-tier cache design with unified addressing, prefill optimization strategies, and capacity planning guidance for long-context coding agent workloads. Readers will learn how to schedule, evict, and reuse both state types to improve throughput and time-to-first-token.
1. Why Hybrid Architectures Make Caching Harder
A number of recent models adopt a hybrid structure that mixes full attention layers with linear layers. The full attention layers keep a complete key-value cache, which gives them strong representational power. The linear layers, such as state space structures, replace the ever-growing key-value cache with a fixed-size recurrent state, which keeps memory usage during decoding at a constant level. The benefits are straightforward: faster decoding, a smaller cache, and lower memory consumption for long contexts.
The problem gets harder with workloads like coding agents. A typical agent reads several files, produces a patch, runs tests, and keeps editing based on the errors it sees. A single task can involve dozens of model calls, and the context grows from a few thousand tokens to hundreds of thousands. Every call shares an almost identical prefix, so ideally it should hit the prefix cache directly and only compute the new portion.
For pure attention models, prefix cache semantics are clear: a token sequence maps to a deterministic set of key-value tensors, which can be addressed, reused, and evicted by hashing blocks. Hybrid architectures add another kind of state. The recurrent state of a linear layer is strongly tied to the specific token position and the scan order, so it cannot be concatenated freely by content addressing the way key-value blocks can. The cache system now has to manage two kinds of objects with completely different lifecycles, addressing schemes, and migration costs, and the complexity rises sharply.
2. The Core Conflict: Two Kinds of State with Different Semantics
The states in a hybrid model can be roughly split into two categories:
- Addressable state: the key-value cache of the full attention layers, chunked by content hash, which naturally supports prefix sharing and cross-request reuse.
- Path-dependent state: the recurrent state of the linear layers, which is the accumulated result of scanning from the beginning to the current position. It has no content addressing semantics, and reuse requires the prefix to be exactly identical.
This creates three engineering difficulties. First, the matching condition is stricter: if a single token in the prefix differs, the path-dependent state must be recomputed. Second, the granularity does not match: key-value caches are suited to block-level swapping in and out, while recurrent states are usually migrated as a whole, which easily triggers frequent whole-layer transfers. Third, the cost is asymmetric: recomputing a prefix means scanning all tokens again, which is far more expensive than the partial recomputation in a pure attention model.
3. Design Approach: Tiered Caching with Unified Addressing
One workable optimization direction is to split the cache into three tiers and build a unified addressing and scheduling framework for both state types.
- Device tier: holds the hottest prefix blocks and the current recurrent state for the active request, aiming for the lowest access latency.
- Host memory tier: acts as a capacity pool that takes over evicted key-value blocks and state snapshots.
- Remote cache tier: serves cross-node and cross-request reuse, storing shareable common prefixes such as repository documentation, build scripts, and dependency manifests.
The key to unified addressing is the combination of “prefix fingerprint + block hash + state version”. Key-value blocks are located by content hash; recurrent states are located by “owning prefix fingerprint + version number”, which lets them participate in scheduling while keeping the semantics correct. A sketch:
class PrefixKey:block_hash: bytes # content hash of the key-value blockprefix_id: bytes # prefix fingerprint, identifies a consistent scan startstate_version: int # state version, incremented when the prefix changesdef lookup(block: PrefixKey):# check the device tier first, then the host tier, then the remote cachereturn cache.tiered_get(block)
4. Prefill Optimization: Bringing Down Time-to-First-Token
The prefill stage determines time-to-first-token. With a hybrid architecture, the portion that can be hit should reuse the cache directly, and only the uncached increment takes part in computation. At the same time, the scan of the linear layers can be pipelined with the chunked computation of the attention layers to reduce idle waiting. Three strategies work well in practice:
- Prefix-first scheduling: prioritize requests with high hit rates and long common prefixes to reduce repeated scanning.
- Chunked pipelining: submit computation block by block, overlapping state updates between blocks with attention computation.
- State snapshot reuse: for an exactly identical prefix, load the state snapshot directly and skip the rescan.
In replay tests on real agent traces, this kind of approach raised prefill throughput to more than twice the baseline on a single card, and p90 time-to-first-token dropped by more than eighty percent. The gains remained clear after adding a host memory cache on the order of hundreds of gigabytes, and the approach extends to distributed caching and cross-node state reuse.
5. Capacity Planning and Implementation Advice
When rolling this out, start with a workload profile: measure the distribution of common prefix lengths, the average number of rounds, and the context growth curve, then set the capacity ratio of each tier accordingly. As a rule of thumb, the device tier keeps the hot blocks of the current request, the host tier is sized at tens of times the device tier, and the remote tier is planned against the total volume of common prefixes. Eviction policies are also needed: evict key-value blocks by least recently used, and score state snapshots for eviction by “reconstruction cost × hit probability”.
In practice on Baidu AI Cloud, this can be combined with a container platform to host inference services, object storage to keep state snapshots, a message queue for request orchestration, and logging plus monitoring and alerting to observe hit rates and latency percentiles, forming a closed tuning loop.
6. Summary
Hybrid architectures trade state for cache, gaining decoding efficiency while shifting complexity into cache management. Tiering the two state types, addressing them uniformly, and driving scheduling by hit rate and reconstruction cost is the key path to stable throughput and latency gains in long-context coding agent scenarios.
