Reducing LLM Costs with Prompt Caching: A Practical Guide to Input Cache Hits

    This guide explains how input caching, also known as prompt caching, reduces LLM calling costs by reusing stable prompt prefixes. It covers the general mechanics of cache hits, prefix design and layering, invalidation and consistency risks, cost measurement, and a practical rollout path, while outlining the limits of caching as a probabilistic optimization rather than a correctness guarantee.

    1. Background: A Structural Shift in Calling Costs

    On September 9, 2026, a cloud provider announced a price reduction for its flash-series models, with overall calling costs expected to decline further. Price cuts, however, are only the surface phenomenon. What really determines the size of a bill is the calling structure: input tokens often make up the bulk of a single request, while output tokens usually carry a higher unit price but appear in limited quantities. As a result, engineering teams that focus only on “how much the unit price dropped” will soon see those gains swallowed by growth in call volume. A more controllable lever is reducing the actual billed volume of repeated input, which is exactly the value of the input cache hit mechanism.

    It is worth noting that vendors differ in how they account for caching, how they determine a cache hit, and how they expire cached content. This article discusses only the general principles and engineering methods widely accepted in the industry, without referring to any specific API names or parameters.

    2. How Input Cache Hits Work

    The core idea of input caching (Prompt Caching) is to retain the computed results of the stable, unchanged prefix of a request on the server side—system prompts, tool definitions, long documents, few-shot examples, and knowledge snippets. When a later request shares an identical prefix, the repeated computation is skipped, and only the newly added portion is processed incrementally.

    The input of a request can usually be split into two parts:

    1. [ stable prefix P ] + [ changing suffix S ]
    2. token billing ≈ discounted price for the hit portion + standard price for the missed portion

    Three necessary conditions for a hit (the common industry understanding):

    1. The prefix is byte-for-byte identical, including whitespace, line breaks, punctuation, and ordering;
    2. The prefix is at the beginning of the request; inserting changing content in the middle invalidates everything after it;
    3. The prefix is used again within the cache’s survival window; a long idle period leads to eviction.

    This yields a key engineering constraint: pushing changing content as far back as possible is the cheapest way to improve the hit rate.

    3. Key Design and Prefix Orchestration

    Whether a hit occurs is essentially a prefix comparison, so the stability of the prompt template is directly equivalent to the smoothness of the cost curve. The recommended approach is to layer the prompt:

    1. Layer 0: Safety policies and role settings (almost never change)
    2. Layer 1: Tool/function schema (version-controlled; changes require gradual rollout)
    3. Layer 2: Domain knowledge and long documents (split by business domain, updated independently)
    4. Layer 3: Session history and the user's current input (changes frequently)

    Supporting engineering practices:

    • Keep templates under version control; any minor wording tweak causes a full miss, so the cost of changes must be evaluated;
    • Push volatile fields such as timestamps, random IDs, and request tracing numbers down into the suffix;
    • In multi-turn conversations, history messages grow by appending, which naturally fits incremental caching;
    • For batch offline tasks, aggregate and sort requests with the same prefix to reduce cache eviction.

    Illustrative pseudocode:

    1. def build_request(system_v, domain_doc, history, user_input):
    2. prefix = system_v + domain_doc # stable, cacheable
    3. suffix = history + user_input # changing, always billed
    4. return {"messages": prefix + suffix}

    4. Invalidation Governance and Consistency Risks

    Caching introduces two types of problems that require active governance. The first is the “false hit”: developers assume the prefix is unchanged, but differences in serialization order, floating-point formatting, or Unicode normalization cause a complete miss, showing up as no improvement in the bill. The remedy is to compute a prefix hash on the client side and report it, comparing it against the measured hit volume.

    The second is “stale content”: if an old cache is still hit after knowledge has been updated, factual errors result. It is advisable to introduce an explicit version number for knowledge snippets and encode that version number into the prefix, achieving “change the version to invalidate” rather than relying on natural expiration.

    1. cache_key = hash(system_version + doc_version + normalized_text)

    5. Measuring Results and the Cost Model

    Do not look only at the total bill; establish an attributable set of metrics:

    • Hit rate = hit input tokens / total input tokens;
    • Average cost per request = cost of the hit portion + cost of the missed portion + output cost;
    • First-token latency and end-to-end latency percentiles (a hit usually lowers first-token latency but does not necessarily reduce total duration);
    • Quality regression metrics, to ensure cost reduction has not sacrificed accuracy.

    A simplified decision rule: when “stable prefix ratio × hit rate” is high and call frequency is sufficient, caching yields significant benefits; if requests are highly personalized with very short prefixes, the benefit is limited, and other means such as output length control and model-tier routing should be considered instead.

    6. Rollout Path and Boundaries

    A four-step approach is recommended: first, sample requests and measure prefix ratios to quantify the optimization space; then, complete template layering and push volatile fields down; next, validate the three metrics of hit rate, latency, and quality through gradual rollout; finally, solidify monitoring and alerting, and add the hit rate to the daily dashboard.

    The boundaries must be clear: caching is a probabilistic optimization, not a correctness guarantee, so scenarios with strong consistency requirements need fallback logic. In addition, the caching mechanism does not change data compliance responsibilities; whether sensitive data can be cached must follow the data processing policies and tenant isolation agreements of the platform in use. In the general large-model service practice of platforms such as Baidu AI Cloud, this kind of prefix reuse and layered orchestration is likewise a foundational means of cost governance and can serve as a default option in architecture design rather than an after-the-fact patch.