LLM Prompt Caching: Improving Cache Hits and Reducing Long-Context Costs

    Learn how prompt caching affects long-context LLM costs, how to verify cache hits using token usage and billing data, and how to diagnose misses caused by unstable prefixes, cache expiration, routing, or middleware. The article also explains how request structure influences cache reuse.

    1. Why Long-Context Costs Can Rise Unexpectedly

    In a multi-turn conversation, each request sends the conversation history back to the model. As the conversation grows, the number of input tokens per request increases roughly linearly. Once the context reaches 100,000 tokens, input processing alone can account for a substantial portion of the cost of each call.

    If the input price is P currency units per million tokens, a request with 100,000 input tokens costs approximately:

    1. Input cost ≈ (100,000 / 1,000,000) × P

    If every turn is billed at the full input rate, the cost per request rises as the conversation gets longer.

    When investigating an unexpected bill, do not immediately assume that the model itself is simply too expensive. A more useful approach is to break down each request into input, output, and cache-related usage, then identify which component is driving the increase.

    2. How Prompt Caching Works

    Prompt caching is a common server-side optimization in LLM services. When two requests share an identical beginning, the service can reuse intermediate results from the earlier request instead of processing that content again.

    Several concepts are essential to understanding this mechanism.

    Prefix Matching

    Caching operates on the request prefix. The service checks for an exact match from the beginning of the request. Only the uninterrupted matching portion is eligible for reuse. Once the content changes at a particular position, content from that point onward cannot be reused through that prefix match.

    Separate Charges for Cache Writes and Reads

    Many services distinguish between three types of input usage: ordinary input, cache writes, and cache reads.

    Writing content to the cache for the first time may incur an additional charge. On subsequent cache hits, the reused portion is typically billed at a substantially lower rate than ordinary input.

    Limited Cache Lifetime

    Cached content expires, often on a timescale measured in minutes. Some services refresh the expiration period when the cache is accessed again within its lifetime. If too much time passes between requests, the cache expires and must be written again.

    Cache Breakpoints

    Some services allow callers to mark cache breakpoints in a request to control which content is cached. A breakpoint should generally be placed at the end of stable content.

    The exact syntax varies by service. Refer to the relevant API documentation.

    Content type Suitable for caching? Notes
    Fixed system instructions Yes Identical across requests
    Tool or function definitions Yes Both content and ordering must remain stable
    Established conversation history Yes Reusable when the prefix remains unchanged
    Current user input Generally no Usually changes with each request
    Timestamps and random IDs No Can break prefix consistency

    3. How to Verify That Caching Is Working

    A configuration setting that says caching is enabled is not proof that requests are benefiting from it. Verify the behavior using usage data.

    Step 1: Inspect Usage Statistics

    Check the usage fields returned with each request. Determine whether they distinguish ordinary input tokens, cache-write tokens, and cache-read tokens.

    Field names vary by service, so consult the relevant documentation.

    Step 2: Run Two Consecutive Requests

    Send two requests using the same long context, ensuring that the second request has an identical prefix.

    If caching is working, the second request should report a clearly nonzero number of cache-read tokens and substantially fewer ordinary input tokens.

    Step 3: Reconstruct the Cost

    Multiply the token count in each category by its corresponding rate, then add the results:

    1. cost = uncached_input * p_in
    2. + cache_write * p_write
    3. + cache_read * p_read
    4. + output * p_out

    Use consistent units: if the counts are individual tokens, the prices in this formula must be per-token rates.

    The cache-read rate, p_read, is usually substantially lower than the ordinary input rate, p_in. The cache-write rate, p_write, may be slightly higher than p_in.

    If the calculated cost is close to full-price billing and cache-read usage remains at zero, caching is not taking effect.

    Step 4: Investigate Persistent Misses

    If the prefix appears unchanged but cache-read usage remains at zero, work through the causes in the next section.

    The following Python example compares estimated costs across multiple requests with a baseline in which all input tokens are billed at the ordinary input rate:

    1. def estimate_cost(records, p_in, p_write, p_read, p_out):
    2. total = 0.0
    3. full_price_total = 0.0
    4. for r in records:
    5. total += (r["uncached_input"] * p_in
    6. + r["cache_write"] * p_write
    7. + r["cache_read"] * p_read
    8. + r["output"] * p_out)
    9. # Baseline: bill all input tokens at the ordinary input rate.
    10. full_price_total += ((r["uncached_input"] + r["cache_write"] + r["cache_read"]) * p_in
    11. + r["output"] * p_out)
    12. saving_ratio = 1 - total / full_price_total if full_price_total else 0
    13. return total, full_price_total, saving_ratio

    If saving_ratio stays close to zero over time, caching is providing little cost benefit.

    4. Common Causes of Cache Misses

    Cache misses usually fall into the following categories.

    1. Unstable Prefixes

    This is the most common cause. Any of the following can change the prefix:

    • Dynamic values, such as the current time, username, or session ID, embedded in system instructions.
    • Tool or function definitions appearing in a different order between requests.
    • Temporary status information inserted into the conversation history.
    • Small differences in whitespace or formatting between otherwise identical requests.

    2. Incorrect Breakpoint Placement

    Placing a breakpoint after content that changes can prevent the intended stable portion from being cached and reused as expected.

    3. Prefixes Below the Minimum Cacheable Length

    Services commonly require a prefix to reach a minimum length before it can be cached. A shorter prefix may not produce a cache hit even when it is identical across requests.

    4. Requests Arriving After Cache Expiration

    If the interval between requests exceeds the cache lifetime, the cached content is no longer available. This is especially relevant for infrequent requests.

    5. Requests Routed to Different Backends

    Caches are often scoped to a particular service node or provider. If requests are dynamically distributed across backends—for example, according to price, latency, or availability—requests from the same conversation may reach different destinations.

    When those destinations do not share a cache, the cache-hit rate falls.

    Changing the routing policy does not necessarily solve the problem and can sometimes reduce the hit rate further. Evaluate routing changes using usage fields and controlled tests rather than intuition alone.

    If requests pass through a protocol-conversion layer or an aggregation gateway, provider-specific cache fields may be dropped or ignored.

    The client may appear to have caching enabled while the underlying service receives an ordinary request without the required cache configuration.

    The most reliable diagnostic is a comparison test that bypasses the middleware and calls the underlying service directly.

    5. Structuring Requests for Better Cache Reuse

    Cache-hit rates depend heavily on how the client organizes each request. The core principle is simple: put stable content first and dynamic content later.