Managing Heterogeneous KV Cache in Million-Token Long-Context Inference

    This article explains why a high prefix cache hit rate does not guarantee correct generation in long-context LLM serving once hybrid attention is introduced. It breaks down the five state objects that must be tracked—compressed KV, uncompressed tail, sliding window attention (SWA) state, indexer state, and on-disk prefix—and shows how the allocator must evolve from finding free blocks to recording object shape and recovery conditions. It then presents four validation checks that make a prefix hit trustworthy, four consistency rules that must not be violated, and a practical rollout order with observability metrics for building a verifiable, recoverable, and observable long-context cache system.

    1. The Problem: A Prefix Hit Does Not Mean the State Is Usable

    In long-context inference serving, prefix caching has long been treated as a pure hit-rate optimization problem. Mature paged attention implementations split token history into fixed-size pages and use a page table to map logical pages to physical KV blocks, so requests with identical prefixes simply reuse existing blocks. This assumption has held for years on general-purpose dense models.

    Once a model adopts hybrid attention—where different layers use full-history compression, sliding windows, and sparse routing respectively—an implicit rule breaks: “same prefix implies the cache can be reused directly.” The hit rate can remain high while the generated output becomes wrong. A hit rate is therefore not a correctness metric; it only tells you that something was found, not that computation can continue from it.

    2. Why Heterogeneity Breaks the Original Assumption

    The traditional paged cache has a world model with only one type of object: the KV block. Hit determination is based on prefix hashing, and the allocation action only picks a free block to write into. After heterogeneity is introduced, the same span of token history exists in different forms across layers, with completely different lifetimes and recovery conditions. In engineering terms, this can be abstracted into five object types:

    • Compressed KV: the full history is stored in compressed form and can only be decoded when whole blocks are aligned;
    • Uncompressed tail: the most recent tokens are still kept at original precision, and if missing, near-end context is lost;
    • SWA window: valid only inside the window, and unrecoverable once the window has slid past;
    • Indexer state: the index/routing metadata that sparse routing depends on; if missing, attention takes the wrong branch;
    • On-disk prefix: once GPU capacity is exceeded, the prefix sinks to external storage, and recovery requires transfer and verification.

    An analogy: in a traditional model, finding a block is like getting a shelf number from a library and going straight to fetch the book. In a hybrid attention model, after getting the number you must confirm five things—whether the book is still on the shelf (compressed KV), whether the latest pages are bound (tail), whether the borrowing card has expired (sliding window), whether the index catalog is in sync (indexer state), and whether the book has been sent to a suburban warehouse and not yet returned (on-disk prefix). Only when all five are confirmed recoverable can you truly continue reading.

    3. Upgrading the Allocator’s Job: From “Finding a Free Block” to “Recording the Form”

    Once objects are heterogeneous, the allocator cannot merely find a free GPU memory block; it must also record the current form and recovery conditions for each span of tokens. Meanwhile, if prefix matching reports a hit based on hash alone, the actual read will be missing the tail, the window, or the routing, and generation will inevitably be wrong. Illustrative logic:

    1. # Pseudocode: recoverability of state must be validated after a hit
    2. hit = prefix_match(req.prefix_hash)
    3. if hit:
    4. states = cache_group.query(req.prefix_len) # state of the five object types
    5. if not recoverable(states, req.layer_plan):
    6. promote_or_recompute(req) # fill the tail / rebuild the window / sync the index / pull back cold storage
    7. attach(req, states)

    Here layer_plan describes the attention form of each layer and is the key input for determining which objects are needed; without it, any hit determination is incomplete.

    4. Four Checks That Make a Prefix Hit Trustworthy

    1. Whole-block alignment check: compressed KV must be complete at block granularity; a partial block is unusable;
    2. Operator and ABI consistency: the decoding operator must match the cache layout version, and cross-version reuse is treated as a miss;
    3. State intersection across multiple cache groups: state across cache groups (compressed group, sliding window group, index group) is intersected, and the result is downgraded if any one is unsatisfied;
    4. Byte-level hardware alignment: the start address, stride, and alignment boundaries must satisfy hardware constraints, avoiding a “logically hit, physically unreadable” situation.

    5. Four Consistency Rules That Must Not Be Violated

    • Admission cap: reserve budget for cold storage and recomputation so that long-context requests do not drain GPU memory and bandwidth;
    • Five-state routing: route to different recovery paths by object type, and never fall back to a single catch-all logic;
    • Time-of-check-to-time-of-use (TOCTOU) consistency and state pointer snapshots: state may change between matching and reading, so a consistent snapshot (such as an array of state pointers) is needed to fix the view;
    • Static graph topology: a static execution graph requires fixed addresses and shapes; swapping blocks dynamically must go through explicit copies or double buffering, and implicit in-graph replacement is not allowed.

    6. Implementation and Observability Recommendations

    The suggested rollout order is: first fill in the layer_plan metadata and the five-state query interface, then change hit determination to a dual condition of “hash + recoverability,” then add admission control and routing, and finally handle static graphs and cold storage transfer. For observability, beyond the hit rate, separately track the hits, downgrades, recomputations, and transfer latency of the five object types; only when these metrics are healthy at the same time is prefix reuse both fast and correct.

    On the cloud, you can combine a container platform to host the inference runtime, object storage to host the on-disk prefix, and logging and monitoring/alerting services to carry the five-state metrics, thereby building a verifiable, recoverable, and observable long-context cache system without binding to a specific implementation.