Prefix Cache Consistency for Hybrid Attention LLMs: Why Hit Rate Is Not a Correctness Metric

    Prefix caching is a default optimization in long-context LLM serving, but teams moving to hybrid attention architectures often see good cache hit rates alongside intermittent output truncation, semantic drift, and repetition. This article explains why: a hash match only proves the token sequence is identical, not that the cached KV is in a resumable form. It covers the hidden assumption behind traditional prefix caching, what hybrid attention breaks, five cache states (compressed KV, uncompressed tail, SWA window, indexer state, on-disk prefix) and their recovery conditions, how the allocator must evolve into a state machine, four pre-hit validations, and the consistency checks and observability metrics to put in place before launch.

    1. Where the Problem Starts: Hit Rate Goes Up, Output Starts Breaking

    In long-context inference serving, prefix caching is almost always enabled by default. The benefit is straightforward: when multiple requests share the same system prompt or the same long document, the repeated token history does not need its KV recomputed, and time to first token drops significantly.

    But many teams run into a strange class of failures after switching to a hybrid attention architecture: the cache hit rate report looks great, yet production intermittently produces truncated output, semantic drift, and degenerate repetition. By the time the investigation ends, the problem usually turns out not to be the hit decision itself, but whether the state behind a hit can actually be recovered.

    The core conclusion up front: prefix cache hit rate is not a correctness metric. It only answers whether the key for a span of tokens matches. It does not answer whether the KV for that span is in a form that can be directly resumed.

    2. The Hidden Assumption of Traditional Prefix Caching

    The common approach in mainstream inference frameworks can be abstracted into two steps:

    1. Allocation: pick free GPU memory blocks for incoming tokens and write into them;
    2. Prefix match: hash the token sequence, and on a hit reuse the existing blocks and skip recomputation.

    This mechanism relies on an implicit premise: token history is homogeneous. The KV at every position exists in the same form and with the same lifetime, so “same hash means the physical block can be reused” is safe. Paged KV management, which splits history into fixed-size pages and maps them to physical blocks through a page table, makes that assumption efficient as well.

    An analogy: it is like fetching a book from a library by shelf number. If the number matches, you go straight to the shelf and take it, with no extra confirmation in between.

    3. What Hybrid Attention Breaks

    Hybrid attention mixes different mechanisms across different layers of the same model. Common combinations in the industry include:

    • Full-history compression: compress all history into a low-rank or low-resolution representation, saving memory at the cost of being irreversible;
    • Sliding window attention (SWA): keep the complete KV only for the most recent W tokens;
    • Sparse routing: an indexer selects a small number of historical positions to participate in computation.

    Two practical pressures are layered on top: context length climbing into the millions, which no longer fits on a single GPU and must be offloaded to host memory or disk; and the need to reuse state across requests and across sessions.

    The result is that the same “prefix-identical” token span sits in five different forms across layers and storage tiers. A hash hit can only prove the token sequence is identical; it cannot prove all five forms are present.

    4. Five Cache States and Their Recovery Conditions

    For engineering work, it is worth standardizing on five names, so that teams inventing their own terminology do not make troubleshooting harder:

    State Meaning Resumable directly after a hit Typical recovery action
    compressed KV full-history compressed representation No, information is lossy Recompute, or fall back to the uncompressed source
    uncompressed tail complete uncompressed KV at the tail Yes, but length must align Pad up to the page boundary
    SWA window complete KV inside the window Only if the window is complete Recompute the part that spilled outside the window
    indexer state index/scoring state for sparse routing No, missing routing means wrong results Rebuild the index
    on-disk prefix already offloaded to host/disk No, must be brought back Prefetch, verify, then bring back

    The key differences come down to three things: lifetime (when it is created, when it becomes invalid), recovery condition (whether it can be restored losslessly), and hit decision (what is used as the key). The compressed state is lossy, the window state rolls forward, the index state is derived, and the offloaded state carries transfer cost. Folding all of them into a single boolean “hit means reuse” decision is bound to produce errors.

    Illustrative decision logic:

    1. def can_resume(entry, incoming_len):
    2. # 1. Form completeness
    3. if not entry.has_compressed and entry.need_full_history:
    4. return False
    5. # 2. Tail alignment
    6. if entry.tail_len % PAGE_SIZE != 0:
    7. return False
    8. # 3. Window completeness
    9. if entry.swa_len < min(WINDOW, incoming_len):
    10. return False
    11. # 4. Index freshness
    12. if entry.indexer_version != entry.kv_version:
    13. return False
    14. # 5. Offloaded state brought back
    15. if entry.on_disk and not entry.prefetch_done:
    16. return False
    17. return True

    5. The Allocator Must Become a State Machine

    Once cache objects are heterogeneous, the allocator’s job expands from “find a free block” to “record the current form of each token span”. Four constraints are recommended:

    • Admission cap: limit how many compressed and offloaded states can be resident concurrently, so the hit rate does not look inflated while memory and bandwidth get overwhelmed;
    • Five-state routing: different states go through different reclaim queues with different priorities;
    • TOCTOU protection: state can change between match and read, so pin it with a version number or a snapshot of the state pointer, avoiding “it was readable when I checked, but already swapped out when I actually read”;
    • Static topology constraints: if graph-capture-style execution optimization is in use, dynamic memory pointers break the static address assumption, so fixed slots must be reserved.

    6. Four Validations Before the Hit

    1. Block alignment: round the hit length to page/block size, avoiding reuse of a partial page;
    2. Operator and kernel ABI consistency: different attention implementations use different data layouts, and mixing them is an error;
    3. State intersection across multiple cache groups: multiple cache groups (compressed group, window group, index group) must all be satisfied at the same time;
    4. Byte-level hardware alignment: dtype, alignment granularity, and device consistency all need to be verified.

    7. Implementation Recommendations

    Before going live, put four consistency checks and a set of observability metrics in place: hit rate per state, recovery failure rate, bring-back wait time, and the distribution of validation rejection reasons. Demote “hit rate” to a capacity metric, and promote “resumable rate” to a predictor of time to first token. On top of the container platform and monitoring/alerting capabilities offered by platforms such as Baidu AI Cloud, this per-state instrumentation can be turned into a standing dashboard with relatively little effort, helping teams keep both stability and cost under control in long-context scenarios.