LLM Input Cache Hits: How Prefix Caching Works and How to Use It

    Input cache hits let inference services reuse KV state for matching prompt prefixes instead of recomputing the full forward pass. This article explains what is cached, how hits are scored and billed, what breaks prefix reuse, how that affects cost and first-token latency, and practical ways to raise hit rate and measure it.

    Input cache hits occur when an inference service sees that a new request’s prefix matches intermediate results already computed and stored. The service reuses those results instead of running a full forward pass from the first token. For developers and enterprise users, that affects two things at once: input-side cost, and first-token latency on long context. Understanding hit conditions, keeping a stable prefix, and separating “cheaper” from “correct” matters more over time than any single price change.

    What is actually cached

    During self-attention on the input, every token produces keys and values. Those KV tensors are tightly coupled to later generation and are the most expensive part of a long prompt. Input caches typically store prefix-aligned KV state, not a copy of the raw text for string lookup. Text that looks almost identical can still miss if tokenization, token order, or system instructions differ in even one place.

    A request is often split, for billing and scheduling, into two kinds of input:

    • Cache miss tokens: recomputed and charged at the full input rate.
    • Cache hit tokens: reused KV, much less compute, usually billed at a lower input rate. The discount factor varies by provider; do not assume a single industry-wide multiplier.

    Output tokens are still billed at the generation rate. Hits do not make the model “speak for free.” Caching optimizes repeated long prefixes, not shorter completions.

    How a hit happens

    A typical path has four steps.

    1. The service computes a cache key for the input prefix from the model, tokenizer version, and cache policy.
    2. It looks in the cache window for a reusable KV prefix.
    3. On a hit, computation continues from the matched length for new tokens. On a miss, it computes everything and may write the result per policy.
    4. Billing accumulates hit tokens and miss tokens separately.

    Caches have TTLs, capacity limits, and eviction. Higher miss rates at peak may mean entries were evicted, not a badly written prompt. Multi-replica and multi-region routing can send a request to an instance that never saw that prefix. Hit rate is a statistic, not a per-request guarantee.

    What breaks a hit

    Any of the following invalidates the prefix from the first difference onward:

    • System prompts, tool descriptions, personas, or output-format rules rewritten, reordered, or given dynamic fields.
    • Timestamps, request IDs, random few-shots, or user nicknames inserted in the middle of a stable prefix.
    • History summarized, truncated, or reordered so prefix boundaries move.
    • Switching models, context-window tiers, or tokenizer-related settings.
    • Alternating different “hidden instruction” templates in the same conversation.

    A practical rule: put content that does not change across requests first, and per-turn changes last. The later the first change, the longer the reusable prefix.

    A typical layout:

    1. [stable system instructions]
    2. [stable tools and constraints]
    3. [reusable long documents or knowledge]
    4. [session history, append-only when possible]
    5. [this turn’s question and ephemeral parameters]

    If you must inject dynamic fields, put them at the end of the prefix so they do not pollute everything before them.

    Relation to cost

    For long-document Q&A, codebase Q&A, multi-turn support, and agent tool loops, input is often much longer than output. Whether the input cache hits can matter more to the bill than the output unit price. Split a call roughly as:

    • hit input tokens × hit rate
    • miss input tokens × miss rate
    • output tokens × output rate

    Do not evaluate a design on a single “average unit price.” After cold start, cache expiry, or traffic shifts, the miss share can jump. Capacity planning should leave headroom for a miss upper bound, not assume an ideal hit rate at full load.

    Time-of-day or peak/off-peak pricing, if present, is a separate mechanism from cache hits: one depends on when the request runs, the other on whether the prefix is reusable. Batching heavy traffic into quieter hours only changes the time-based rate; it does not replace prefix stability. Conversely, a well-designed prefix can still cut compute at peak.

    Promotional, permanent, or weekend prices are commercial policy and will change. Treat unit prices as configuration. In the gateway or billing reconciliation, account for three token classes—hit, miss, and output—and change only coefficients when the price table updates, not the call structure.

    Raising hit rate

    Pin templates. Version system prompts, but keep bytes stable within a live version. For canaries, put old and new templates in different cache namespaces so they do not steal each other’s prefixes.

    Keep documents and retrieval results stable. Reordering retrieved chunks every turn shatters the prefix. For a session, cache already injected document blocks and append only when the documents actually change, instead of rewriting the whole context each turn.

    Append history. If every turn re-summarizes and replaces the front of the prompt, hit length collapses. Safer: keep the original prefix and add the summary as a later block. If you must truncate, treat it as a new prefix version and accept one cold start.

    Control concurrency and routing. A burst of identical prefixes spread across nodes with no shared cache looks like a false miss. For batch jobs on the same knowledge base, sticky routing by prefix hash can send the same prefix back to the same cache domain. Stickiness is for reuse probability, not correctness.

    Avoid pointless uniqueness. Do not put trace IDs, user IDs, or wall-clock time in the system prompt. Put those in request metadata or at the end of the prefix.

    Observability and acceptance

    Record at least, per call: total input tokens, hit tokens, miss tokens, output tokens, model id, and prompt template version. Prefer hit rate as “hit tokens / total input tokens,” not “share of requests that hit,” because one long-prefix hit is worth far more than a short-sentence hit.

    Break metrics down by template version, product scenario, and time window. A global average hides “code Q&A hits well, support rewrites the system prompt every turn and does not.” Alert on miss-token share or cost per session jumping, not only on error rate.

    For acceptance, send two calls with a fixed prefix: the second should show hit tokens close to the stable prefix length. Then change one token in the middle of the prefix and confirm the hit length falls to just before the edit. That tests your understanding of the boundary rather than relying on console copy.

    Common mistakes

    A cache hit does not guarantee the same answer. Temperature, sampling, and later extra context still change generation. Do not treat hits as idempotency or an audit trail.

    Longer prompts are not always better. Unreused long input inflates miss cost and burns context. Drop unused instructions first, then optimize caching.

    Do not fake “billing cache” in the application as a substitute for server-side KV reuse unless you are only doing local deduplication. String equality is not a server hit; a server hit is not proof that your business cache is still valid.

    Before sensitive content enters a reusable prefix, confirm isolation: tenant, API key, or region. Multi-tenant shared prefix caches may reuse compute state only; they must never leak one tenant’s raw text into another tenant’s response. Require isolation by caller identity on the server, and do not put tenant A’s private documents into a template tenant B might reuse.

    Checklist

    1. Split the three token classes, keep unit prices external, budget against a miss upper bound.
    2. Put system instructions, tool definitions, and static knowledge first, and version them.
    3. Put dynamic fields, retrieval deltas, and user questions last.
    4. Prefer append-only session history; treat truncation as a new prefix.
    5. Monitor hit-token share and cost per task; regress with two fixed requests.
    6. Optimize time-based pricing and cache hits separately; they do not replace each other.

    Input cache hits are the billing and latency consequence of turning repeated prefix compute into reusable state. Stabilize the prefix, push changes later, and treat hits as an observable metric so cost stays controllable when prices change—without rewriting how you call the model every time the rate card updates.