Prompt Caching for LLMs: A Practical Guide to Cutting Long-Context Costs

    This guide explains how prompt caching reduces the cost of long-context LLM calls by reusing stable prefixes instead of compressing prompts. It covers prefix-matching mechanics, the three-part billing model (regular input, cache writes at a premium, cache reads at a discount), observability metrics such as hit rate, engineering practices for placing static content before dynamic content, TTL and write-cost trade-offs, and governance concerns including multi-tenant isolation and fallback. Readers will learn when caching pays off and how to cut repeated-context costs by roughly 90%.

    1. The Problem: Why Long-Context Calls Get More Expensive Over Time

    A cost pattern is becoming increasingly common in enterprise LLM applications: the number of input tokens per request keeps growing, while the share of genuinely “new” content stays low. System prompts, tool and function definitions, domain knowledge notes, few-shot examples, and long-document summaries often remain identical across thousands of requests. Under standard input pricing, this stable context is billed again and again, becoming the largest source of redundancy on the bill.

    The answer is not to compress prompts but to introduce a prompt caching mechanism: reuse the computed result of a stable prefix on the server side, with cached portions billed at a significant discount. One of the more aggressive discount tiers in the industry is about 10% of the original price, saving roughly 90% of the cost for that portion. Note that the savings apply to the input tokens that hit the cache, not to the entire bill; the overall reduction depends on the cache hit rate.

    2. How Prompt Caching Works and How It Is Billed

    Prompt caching is fundamentally about prefix reuse. Because the model processes an input sequence in order, the intermediate results for the prefix can be reused directly as long as the prefix is exactly the same, with no need to recompute. Cache matching is therefore typically “longest common prefix, exact match” rather than semantic similarity.

    Billing generally splits into three categories:

    • Regular input tokens: the portion that misses the cache, billed at the standard rate.
    • Cache write tokens: creating a cache for the first time usually adds a premium on top of the standard rate, commonly around 25%, which pays for writing and indexing the cache.
    • Cache read tokens: the portion that hits is billed at a discounted rate, as low as 10% of the original price.

    This yields a per-request cost model:

    1. Cost = tokens_write * P_in * 1.25
    2. + tokens_read * P_in * 0.10
    3. + tokens_new * P_in

    Here P_in is the standard input price. The write premium is a one-time cost that is amortized only when the same prefix is reused; if a piece of context is used only once, enabling the cache actually costs more.

    Another key constraint is lifetime. Some implementations offer only a very short time to live (for example, a few minutes); once it expires, the cache must be written again and the premium paid again. The value of caching therefore depends heavily on request density within a time window, not on keeping a cache around indefinitely.

    3. Observability: Turning Cost from a Black Box into a White Box

    Platforms differ markedly in transparency. A more mature approach returns cache-related metrics directly in the response body, such as the number of tokens written to the cache for this request and the number read from it. Developers can then account for cost and hit rate at single-request granularity instead of reconciling against a bill after the fact.

    Recommended self-defined metrics:

    1. hit_rate = cache_read_tokens / (cache_read_tokens + cache_write_tokens + uncached_input_tokens)
    2. amortized_write = cache_write_tokens / total_requests_in_window

    When hit_rate falls below expectations, the cause is usually not cache expiry but a “polluted” prefix: a dynamic field placed too early in the prompt breaks the shared prefix.

    4. Engineering Practice: Put Cacheable Content First

    There is only one implementation rule: static content first, dynamic content last.

    1. [system instructions + domain specs + tool definitions + few-shot examples] ← stable prefix, good for caching
    2. [retrieved document chunks / user input / timestamp / session ID] ← volatile content, keep at the end

    Common anti-patterns include prepending the current time, a request ID, or a username to the start of a prompt; inserting retrieval results before the examples; unstable field ordering during template rendering; and multiple template variants for the same business flow that differ only in spaces or line breaks. All of these change the prefix hash and invalidate the entire cache.

    Templates should be normalized: unify whitespace, fix field order, version the templates themselves, and add a “prefix stability” check in the test environment that asserts on the first N characters of the rendered result.

    5. Trade-offs Between TTL and Write Cost

    Because lifetimes are short and writes carry a premium, usage should be segmented by scenario:

    • High-frequency, short-window: batch extraction, batch labeling, codebase Q&A, and customer-service knowledge base Q&A. Requests are dense and share the same prefix, so the benefit is greatest.
    • Low-frequency, long-document: one-off long-document summarization or one-time report generation. Caching is not recommended; standard input billing is more economical.
    • Conversational interaction: the interval while a user thinks often exceeds the cache lifetime, so evaluate whether repeatedly paying the write premium for each new session is worthwhile.

    A practical rule of thumb: a benefit is possible only when the reuse count N within the window satisfies N * 0.10 + 1.25 < N (roughly N greater than 1.4). Accounting for lifetime and concurrency fluctuations, engineering practice usually requires N to reach several or more before enabling caching.

    6. Stability, Isolation, and Governance

    Caching brings consistency benefits but also risk. In multi-tenant scenarios, prefixes from different customers must never hit each other’s cache, or context will leak; cache key design should include a business-dimension identifier. A fallback path is also needed: if the cache is unavailable, calls should automatically revert to the regular path, keeping availability ahead of cost.

    For governance, monitoring and alerting, logging services, and message queues can be combined into an offline cost-accounting pipeline. Token metrics are collected as aggregable indicators and broken out into dashboards by business line, model, and template version, forming a closed loop of “template change — hit rate — cost.”

    7. Conclusion

    Prompt caching is not a switch-style optimization; it is a capability that must be designed together with prompt engineering, template governance, and observability. In model services on platforms such as Baidu AI Cloud, putting static context first, quantifying the hit rate, and deciding whether to enable caching based on window density is the reliable path to compressing repeated-context costs by about 90%.