Why KV Prefix Cache Fails Under Long Contexts: A Hybrid Attention Perspective
This article explains why KV prefix cache reuse can silently fail in long-context LLM inference when models use hybrid attention. It covers the gap between cache hit rate and correctness, five distinct cache objects with different lifecycles, the expanded responsibilities of the cache allocator, and a four-layer validation approach for safer cache reuse. Readers will learn how to avoid silent quality degradation caused by mismatched cache state.
1. Where the Problem Starts: Hit Rate Is Not Correctness
In large model inference serving, prefix cache is a standard technique for reducing time to first token and avoiding repeated computation. A common implementation splits token history into fixed-size pages and maps them to physical GPU memory blocks through a page table. When a new request shares the same prefix as a previous request, the existing blocks are reused directly, skipping redundant forward computation.
This mechanism has run reliably for pure full-attention models because of a strong implicit assumption: if the token sequence is identical, the corresponding key-value state is uniquely determined and can be reused as whole blocks. However, this assumption no longer holds when newer models adopt hybrid attention, where different layers use full-history compression, sliding windows, sparse routing, or other forms. An identical prefix only guarantees a consistent logical sequence; it does not guarantee that the physical cache state can be restored.
It is important to distinguish two layers: hit rate is a scheduling-layer metric, while correctness is an execution-layer metric. The two can be completely decoupled. A hit may be reported, but blocks may be missing when actually read, resulting in degraded generation quality rather than an explicit error. This is the hardest type of failure to diagnose.
2. Five Cache Objects with Different Lifecycles
Hybrid attention splits what used to be a single homogeneous cache into multiple forms. At the engineering level, at least five objects must be tracked explicitly.
- Compressed KV: A low-rank or aggregated representation that carries full-history semantics. It has the longest lifecycle, but depends on compression timing and block completeness.
- Uncompressed tail: The most recent tokens that have not yet been compressed. They must be kept at original precision, and their length fluctuates with the compression cycle.
- SWA window: Only tokens inside the window are retained. Blocks outside the window are meaningless for the current layer and become unusable once expired.
- Indexer state: The indices and selection results required by sparse routing. They are bound to the prefix but updated more frequently.
- On-disk prefix: History that has been swapped out to external storage due to insufficient GPU memory. It requires asynchronous migration back, and cannot be counted as a hit before migration completes.
These five objects have orthogonal restoration conditions, invalidation rules, and consistency requirements. Reducing them to a single boolean check of “whether a cache block is valid” is one of the most common design mistakes.
Illustrative pseudocode:
def can_reuse(block) -> bool:return (block.compressed_kv.completeand block.tail_len == expected_tail_lenand block.swa_window.validand block.indexer_state.version == block.versionand block.on_disk_prefix.resident)
3. Allocator Responsibilities: From Finding Free Blocks to Tracking Forms
A traditional allocation step only needs to pick free GPU memory blocks for new tokens, while hit detection is performed in parallel through prefix hashing. Once cache objects become heterogeneous, the allocator must record, for each token segment, which form it is currently in and which lifecycle stage it has reached. Otherwise, hashing may report a hit while the tail or window is missing at read time.
The recommended state machine has four constraints that must not be violated:
- Admission limits: Set quotas for swap-in, migration back, and concurrent requests to prevent state restoration from taking over GPU memory needed for decoding.
- Five-way routing: Different forms must use separate recycling and restoration paths; a unified LRU policy is not allowed.
- Temporal consistency: A version number or pointer check is required between hit detection and state reading to prevent the state from being modified after the check.
- Static topology compatibility: In static graph mode, shapes and address layouts are fixed, so dynamic migration back must reserve slots in advance.
4. Four-Layer Validation and Implementation Advice
Before actually continuing computation, validation should be performed in order: whole-block alignment, consistency of operator and runtime conventions, intersection of state across multiple cache groups, and byte-level hardware alignment. In practice, moving correctness assertions to the front of the hit path and adding separate monitoring for each of the five objects can significantly reduce the silent error rate. Baidu AI Cloud also emphasizes in its inference serving practice that cache reuse must be based on verifiable state consistency rather than a single hit rate metric.
