A New Approach to Faster Long-Context Inference in Hybrid Models: Rethinking State Cache Design
Hybrid models that combine full attention with linear attention or state-space layers reduce decoding complexity and KV cache usage, but they introduce new challenges for long-running, multi-turn AI agent workloads. This article explains why KV caches and model states must be managed separately, and examines a dual-track caching architecture, hierarchical state storage, cache-aware scheduling, state serialization, invalidation policies, and performance evaluation practices. In a representative agent trace replay test, the approach achieved 3.14× higher prefill throughput and reduced p90 time to first token by 88.4% compared with a traditional framework.
Background: Hybrid Models Are Mainstream, but Inference Is Getting Harder
Hybrid architectures that combine full attention layers with linear attention or state-space layers (SSMs) have become a major direction in large language model development. The basic idea is to replace some traditional attention components with linear attention or SSM layers. This reduces the computational complexity of decoding from quadratic to linear and significantly lowers GPU memory consumption for the KV cache. As a result, models can generate long sequences and handle multi-turn conversations with lower memory usage and faster autoregressive decoding.
However, these advantages introduce new bottlenecks for complex, multi-turn AI agent workloads. Agent tasks often involve long contexts, repeated chain-of-thought reasoning, tool calls, and the insertion of tool results. Their request patterns typically include high concurrency, multiple rounds of interaction, and dynamically growing contexts.
Prefix caching is an important technique for improving efficiency in these workloads, but it becomes substantially more complicated for hybrid models. The inference system must manage not only conventional KV caches, but also the dynamic states maintained by linear-attention and state-space layers. Because these states do not share the same lifecycle as KV caches, designing and optimizing the inference system becomes considerably more difficult.
Problem Breakdown: Why Is Caching So Difficult for Hybrid Models?
In a pure Transformer architecture, the KV cache for each sequence depends on the attention weights and input data. The computation proceeds token by token within a bounded structure. In a hybrid architecture, however, each sequence also carries a state vector or matrix that evolves as new tokens are processed.
State computation generally follows one of two patterns:
- Input-independent state: determined only by model parameters.
- Input-dependent state: updated dynamically during inference according to the current token.
If KV caches and model states are serialized together and stored in a global cache without distinguishing between them, cache reuse can easily introduce logical errors. For example, the first part of a request context may correspond to state A, while the later part corresponds to state B. If the cache slices the overall KV data but cannot align the corresponding states precisely, the dependency chain becomes inconsistent. This can cause the model to lose long-range information and may result in unstable output or even inference failures.
Therefore, state caching must be separated from KV cache management, with a strictly state-aware caching strategy.
Solution Overview: Dual-Track Caching and Global Scheduling
An open-source technology team in the industry has proposed a system-level approach that redesigns both the cache data plane and the management plane. Its central principle is to manage KV caches and model states independently, while using a global scheduler to maximize reuse.
The design can be understood from three perspectives.
1. Decoupled Cache Granularity: Local Traversal and Global Lookup
The first key idea is to divide traditionally unified cache data according to its computational properties, creating a local-awareness layer and a global-awareness layer.
- Local-awareness layer: This generally includes shallow model layers or input embedding layers. Because these layers have fixed parameters and do not involve state dependencies, conventional strategies such as longest-prefix matching remain applicable. The system builds fine-grained indexes for the KV data in this layer.
- Global-awareness layer: This refers to deeper network components that involve state updates or global attention interactions. For these layers, the system maintains an explicit state checkpoint for each sequence. The cache uses the checkpoint, rather than only the KV cache, to determine whether data can be reused or must be invalidated.
This separation allows the cache system to determine more accurately how much of a new request can reuse conventional KV data and how much state computation must be performed again.
2. Hierarchical State Storage
Because states change frequently while KV data can occupy a much larger volume, the proposed design uses a multilevel, heterogeneous cache pool to balance access performance and storage capacity.
Level 1: Hot Cache in GPU Memory
This level stores the latest states and frequently accessed KV data. Since hybrid-model states are updated frequently, the cache must provide high write bandwidth. The data is typically represented using GPU memory blocks, such as CUDA Graphs or compiled buffers, and input-dependent states are prioritized for zero-copy access.
Level 2: CPU or Persistent Memory Cache
When GPU memory becomes insufficient, KV data and state sequences that are no longer involved in high-frequency computation can be serialized and compressed in host memory. This level also performs deduplication by merging highly repetitive context segments shared by different sequences.
Level 3: Distributed Shared Storage Pool
This level enables cache sharing across nodes and racks. Even when a workload is scheduled on another physical machine, previously serialized states can be loaded through a distributed file system or object storage, as long as the workload identity remains unchanged.
This capability is particularly useful for rollback operations, such as correcting an incorrect decision during an agent task. Instead of recomputing the entire context, the system can retrieve a recent checkpoint and resume from that point.
3. Cache-Aware Scheduling and Population
In a distributed inference architecture, the scheduler no longer focuses only on balancing computational load. It evolves into a cache-hit-driven scheduler.
A simplified implementation is shown below:
def schedule_request(request, global_cache_metadata):# 1. Extract request context features and compare them with each node's cachebest_node = Nonebest_hit_ratio = 0for node in available_nodes:hit_ratio = compute_cache_hit_ratio(request.context_hash, node.global_cache_metadata)if hit_ratio > best_hit_ratio:best_hit_ratio = hit_ratiobest_node = node# 2. Fall back to ordinary load balancing when the hit ratio is too lowif best_hit_ratio < HIGH_HIT_THRESHOLD:best_node = select_by_rr(available_nodes)# 3. Dispatch the request together with cache-hit metadatadispatch_to_node(best_node, request, best_hit_ratio)
The value of this scheduling policy is that it prioritizes the highest global cache hit rate over the shortest queue on any individual node. By routing requests with identical or similar contexts to the same node, the system increases the likelihood of reusing states at the physical infrastructure level.
Engineering Practice: Integration Steps and Performance Trade-Offs
Teams planning to integrate this approach into their inference stack should focus on several areas.
Step 1: Register and Serialize State Operators
This step requires changes to the model execution code. Operators that update the state of linear-attention layers should be marked explicitly as state-related. The implementation must also define custom serialization logic to convert state tensors into efficient byte streams.
Because a state is typically a multidimensional vector or matrix rather than a key-value pair, a dedicated binary format combined with lossless compression is recommended.
Step 2: Configure Fine-Grained Invalidation Policies
In a hybrid architecture, model states are more sensitive to context changes than KV caches. A repeated segment of historical text in a KV cache may be reused directly, while the corresponding state may still need to be recomputed.
The system therefore needs a configurable state tolerance parameter. For example, if a new request reaches a checkpoint at position 5 while the cached state corresponds to position 3, the system must decide whether to recompute the state or truncate the context to align with the cached checkpoint.
Step 3: Benchmark Throughput and Cache Consistency
Performance evaluation should not focus only on the reduction in time to the first token. It should also measure the cache consistency ratio: the proportion of requests that avoid recomputation when migrated across nodes.
In a representative agent trajectory replay test, the approach achieved up to 3.14× the prefill throughput of a traditional framework, while reducing p90 time to first token by 88.4%. These gains primarily came from converting frequent computation-heavy I/O into less expensive memory and network I/O.
Conclusion: Building More State-Aware Inference Systems
State caching designs such as FlashStates highlight an important shift in hybrid-model inference optimization: systems are moving from coarse-grained GPU memory management toward fine-grained state management.
The main lesson is that KV caches are no longer the only performance consideration for agent and multi-turn workloads. Efficient state scheduling and reuse have become equally important targets for system design and cost optimization.
For developers, understanding this architecture and learning to treat model state as a separate abstraction from the traditional KV cache is becoming an important capability for building high-performance LLM applications. Managing this critical variable effectively is one of the most important keys to optimizing long-context inference in next-generation systems.
