The Real Cost of LLM API Calls: Pricing Models and Prefix Cache Optimization

    Most LLM API budgets are built by multiplying a per-token price by an estimated token volume. That linear model assumes every input token is billed at the same rate, which is no longer how mainstream inference services charge. With prefix caching, tokens that match a previously computed prefix are billed at a lower cached rate, so the effective input price depends on the cache hit rate rather than call volume alone. This article breaks down the three-parameter caching cost model, works through a full calculation at a 90% hit rate, explains what determines whether a prefix is reused, and provides an engineering checklist for structuring prompts, maintaining session state, and monitoring hit rates so that a nominal list price becomes a predictable, reproducible bill.

    1. Why Standard Pricing Misleads Cost Estimates

    When budgeting for LLM API usage, the most common approach is to multiply the unit price by an estimated token count and arrive at a linear cost. The model is simple and intuitive, but it rests on an assumption: that every segment of input is billed at the same price. Real billing does not work that way.

    Mainstream inference services have broadly adopted prefix caching. When the input prefix of a request is byte-for-byte identical to that of an earlier request, the server can reuse the intermediate state it already computed and skip the duplicate work. Those tokens are settled at a noticeably lower cached price; only the portion that misses the cache is charged at the standard rate. Input cost therefore stops being a simple product of unit price and token count, and becomes a weighted result of two price tiers blended by the hit rate.

    This means the variable you actually need to forecast is not call volume but the cache hit rate. As the hit rate moves from near 0 to near 1, the effective unit price for the same input can differ by several multiples. Treating standard pricing as the real cost is equivalent to assuming a zero hit rate, which is the most conservative and least realistic assumption available.

    2. The Basic Model of Cache Billing

    Setting aside any specific vendor’s price sheet, cache billing can be abstracted into three parameters: the standard input unit price, the cached input unit price, and the cache hit rate. The cached price is usually a discounted fraction of the standard price. How deep that discount goes varies by service, but “a hit means a discount” is the industry norm.

    The effective input unit price is then straightforward:

    1. effective input unit price = standard price × (1 - hit rate) + cached price × hit rate

    Rewriting it in discount form makes it easier to read:

    1. discount relative to standard price = (1 - hit rate) + discount ratio × hit rate

    Two things follow from this expression. First, the discount is a linear function of the hit rate, so the marginal benefit is constant: every additional percentage point of hit rate saves a fixed amount. Second, the discount has a floor. Even as the hit rate approaches 100%, the effective price never drops below the cached price itself, which means the cache discount ratio sets the ceiling on cost optimization.

    3. A Full Calculation at a 90% Hit Rate

    Assume a multimodal model with a standard input price of 4 CNY per million tokens and a cached input price of 0.7 CNY per million tokens, giving a cache discount ratio of 0.175. With the hit rate holding steady at 90%:

    • The 900,000 cached tokens: billed at 0.7 CNY per million tokens, roughly 0.63 CNY;
    • The 100,000 tokens that miss: billed at the standard 4 CNY per million tokens, roughly 0.4 CNY;
    • Total: approximately 1.03 CNY per million tokens.

    Dividing 1.03 by 4 gives roughly 0.26, or about a 74% reduction off the standard price. Note that these figures only demonstrate the structure of the calculation; the actual unit prices, cache discount ratio, and billing conventions should follow the official documentation of the service you use.

    Swapping in other hit rates shows how sensitive the result is: at a 50% hit rate the effective price is about 2.35 CNY, a 41% reduction; at a 0% hit rate it is the full 4 CNY. The same business logic can cost three to four times more or less purely because of how prompts are organized. That is why capacity planning has to be built on hit rate assumptions rather than on a price sheet.

    4. What Determines the Hit Rate

    A cache entry can only be reused when the prefix matches byte for byte, which makes the hit rate fundamentally a function of input structure stability, not of call volume. Four factors matter most.

    Prefix stability. Caching matches on the longest common prefix, so any change made early in the input invalidates everything after it. Placing volatile content at the end of the prompt is the most direct way to raise the hit rate.

    Session and multi-turn reuse. In a multi-turn conversation, the message history naturally forms a stable prefix. As long as you append rather than rewrite, later turns will hit the cache at a much higher rate than the first. Conversely, summarizing or rewriting the history on every turn causes the cache to miss repeatedly.

    Where time-sensitive content is injected. Timestamps, random IDs, and retrieved document fragments inserted ahead of the system prompt effectively change the prefix on every request. Moving them into a variable section, or injecting them as placeholders at the end, is a common optimization.

    Traffic concurrency and cache lifetime. Cache entries have a time-to-live, so long gaps between requests or widely scattered traffic lead to expirations. Grouping similar requests into a steady rhythm maintains a high hit rate more reliably than sporadic calls.

    5. An Engineering Checklist

    Structure the prompt. Separate the fixed part from the variable part explicitly, as shown below:

    1. [fixed prefix] system role, tool descriptions, output format constraints, a few examples
    2. [variable suffix] current user input, retrieved fragments, timestamp, session ID

    Keep prefix serialization stable. When serializing JSON, fix the key order, avoid floating-point formatting drift, and do not casually reorder or add and remove fields; all of these details change the prefix bytes.

    Maintain session state by appending. Append to the history and never rewrite it. When compression is genuinely necessary, append the compressed result as a new stable segment instead of going back to modify older content.

    Instrument the hit rate. Use logging and monitoring alerts to collect the cached token count and total input token count for each request, grouped by business line, model, and prompt version, and build a comparable hit rate dashboard.

    Feed the hit rate into capacity and budget models. Run a sensitivity analysis with the formulas above and produce cost ranges at 60%, 80%, and 90% hit rates as inputs for budgeting and load testing.

    6. Common Pitfalls

    Treating the hit rate as a metric where higher is always better. A high hit rate accompanied by degraded business results means trading quality for cost, so output quality metrics need to be monitored alongside it.

    Looking at the hit rate only in aggregate. Averages hide the long tail; some low-hit scenarios may account for most of the input cost, so you have to drill down by dimension.

    Overlooking the specifics of multimodal input. Large blocks of content such as images and audio invalidate the prefix as soon as they change. Putting stable text instructions first and media content last usually yields the most obvious gains.

    Assuming the discount ratio is common knowledge. Cache discounts differ substantially across services, so calculations should rely on the actual billing documentation rather than someone else’s empirical values.

    7. Conclusion

    Evaluating LLM API cost should move from “unit price multiplied by token count” to a hit-rate-weighted tiered billing model. In scenarios where multimodality, long context, and multi-turn conversations are the norm, the cache hit rate often has more influence on the final bill than the unit price does. Only by treating prompt structure, session maintenance, content injection order, and hit rate monitoring as first-class engineering problems can a nominal standard price be turned into a predictable, reproducible real cost.