Prompt Caching: How It Works, How It's Billed, and How to Use It in Production

    Prompt caching lets you reuse the static portion of a prompt across API calls, cutting token consumption and time to first token. This article explains how cache keys are derived from cryptographic hashes, how cacheable content is marked with a control parameter, how cache breakpoints and the sticky latch behave, and how the asymmetric write-versus-read pricing changes the economics. It also covers the minimum token threshold required for caching to activate, TTL tradeoffs, and the failure modes that show up in practice — accidental cache eviction, rapid quota consumption, and tenant isolation — followed by guidance on prompt design, TTL tuning, and hit-rate monitoring.

    What Is Prompt Caching?

    Most prompts sent to an LLM are built from two parts: static content that stays largely unchanged, such as system instructions, long knowledge-base context, few-shot examples, and tool definitions; and dynamic content that changes on every call, such as the user’s latest question. When you call a model through an API repeatedly, re-sending that large static block forces the model to run full attention over it each time. The result is duplicate billing for the same tokens and a noticeably higher time to first token.

    Prompt caching addresses this directly. It lets you reuse the static part of a prompt across multiple API calls, so already-processed content is not computed again. In effect, it temporarily stores the computed result for the “unchanging part” of the prompt; on a cache hit, the runtime reads that result back instead of computing it from scratch, which lowers token consumption while improving response speed.

    How It Works Under the Hood

    Cache Keys and Cryptographic Hashing

    Whether a prompt hits the cache depends on how the cache key is derived. Implementations typically compute a cryptographic hash of the prompt content and use it as the key, which matters for two reasons. First, a hash uniquely identifies a given piece of content, so a cache entry is only reused when the static content is exactly identical, which preserves correctness. Second, hashing provides privacy isolation: caches are organization-specific and are not shared between organizations, so sensitive context cannot leak across tenants.

    Marking Cacheable Content

    You need to explicitly mark which content is cacheable. The common approach is a control parameter such as cache_control, placed at a specific point in the prompt structure to tell the runtime that the static content up to that point can be cached:

    1. system:
    2. knowledge base context ... (static, marked for caching)
    3. cache_control: { type: "ephemeral" }
    4. messages:
    5. [ { role: "user", content: "the new question for this turn" } ] // dynamic, not cached

    Cache Breakpoints and the Sticky Latch

    A prompt can define multiple cache breakpoints; common implementations allow up to roughly four cache control points, so you can cover static prefixes at different levels and different lengths. To manage these breakpoints and prevent unintended invalidation, implementations use a “sticky latch” mechanism. The motivation is cost: every invalidation means the cache has to be rewritten, and that carries a real charge. The system therefore tries to keep breakpoints stable and avoid repeatedly clearing the cache when the context pattern switches.

    Cost Structure and Billing Logic

    The most overlooked aspect of prompt caching, and the one that decides whether an architecture actually pays off, is its differentiated pricing. In general, writing to the cache costs more than the baseline price of a normal call, because writing adds compute and storage overhead. Once the cache is hit, reading from it costs significantly less than the baseline. This “expensive to write, cheap to read” model implies:

    • Prompt caching fits workloads with large static content and high reuse, such as long-lived system prompts, stable knowledge bases, and fixed prefixes in multi-turn conversations.
    • If the static content appears in a single request only, writing to the cache may not pay off, because the read that would amortize the cost never arrives.
    • The break-even decision should be based on the expected number of reuses.

    Enabling Conditions: Minimum Token Length

    To control the fixed overhead that caching introduces, runtimes generally require the static content to reach a minimum token length before caching is enabled. This threshold is defined by the model specification and varies with model size, commonly falling between 1,024 and 2,048 tokens. Larger models tend to set a lower bar, while smaller models require a longer static prefix before caching becomes worthwhile. When designing a prompt, make sure the static portion is long enough. Otherwise caching will not activate, and you end up carrying the extra marking and computation logic for nothing.

    Cache Lifetime and TTL

    Caches are not permanent; their lifetime is governed by a time-to-live (TTL), which determines how long cached content is kept after the last access. In practice, TTL is usually configurable between a short tier (around 5 minutes) and a long tier (around 1 hour). Adjusting TTL is essentially a tradeoff:

    • A longer TTL gives the cache more chances to be hit repeatedly, making each call cheaper, but keeps the cache occupied for longer.
    • A shorter TTL releases the cache faster and lowers storage usage, but in long-context scenarios, a single miss means recomputation whose cost rises sharply.

    In long-context scenarios, up to million-token windows, the cost of a cache miss is amplified dramatically. TTL strategy therefore has to be designed around the workload’s call frequency, the size of the static content, and the budget, rather than applied uniformly.

    Common Pitfalls in Engineering Practice

    Accidental cache eviction. A typical problem: a coding tool or inference engine has a code defect that clears the cache — sometimes even the model’s reasoning/thinking content — while handling a request. This triggers a chain of effects: the model “forgets” context, repeats operations, makes faulty tool calls, and burns through quota too quickly. Such defects are usually fixed through version updates, which is a reminder that cache logic sits on a critical path: any code that touches cache clearing should have thorough regression testing and monitoring alerts.

    Rapid quota consumption. When the hit rate drops — TTL too short, static content changing frequently, breakpoints broken by a context mode switch — a large volume of requests that could have hit the cache falls back to full computation, and token consumption spikes accordingly, showing up as quota being consumed quickly. Monitoring the hit rate along with write and read counts is the key to locating this class of problem.

    Privacy and isolation. Because caches are organization-specific, developers working on multi-tenant systems must make sure that hash key generation and isolation boundaries are correct, so context is never reused across different users or tenants.

    Best Practices

    1. Prioritize stable prefixes. Put the unchanging system instructions, knowledge base, and tool definitions first and mark them for caching; put dynamic content at the end to maximize the hit rate.
    2. Estimate the break-even point. Weigh write and read costs against the expected number of reuses, and enable caching only when reuse is sufficient.
    3. Ensure sufficient length. Keep the static content above the minimum token threshold, so you avoid the “marked but not effective” case.
    4. Tune TTL precisely. Short TTL works well for high-frequency short requests; long-context, low-frequency workloads lean toward a longer TTL. Keep calibrating through monitoring.
    5. Monitor the hit rate closely. Bring hit rate and write/read costs into your observability stack so cache invalidation and quota anomalies surface early.
    6. Pay attention to version changes. After upgrading a runtime or toolchain that touches caching, run regression checks on cache behavior to guard against eviction-type defects.

    The value of prompt caching lies in turning the repeated portion of a prompt from a continuous expense into a one-time cost. Once you understand the five pillars — cache key generation, breakpoint management, differentiated billing, minimum thresholds, and TTL — you can make more sensible architectural tradeoffs between cost, latency, and correctness.