Calculating the Blended Cost per Token for LLM API Calls: Formula and Practice

    When you call an LLM API, different token types—cached input, uncached input, and generated output—are billed at very different rates. This article explains why a single quoted price can be misleading, introduces a blended cost-per-token formula that accounts for cache hit rate and input/output ratio, walks through a worked example, and shows how to integrate cost estimation into your monitoring pipeline for model selection, budgeting, and cost tracking.

    Why You Need a Blended Unit Price

    LLM services typically bill by token, but token prices vary widely by type. Within a single request, the cached portion of the system prompt, the new user input, and the model’s generated output are each charged at different rates. Looking at any single rate in isolation can lead to wrong conclusions. For example, a plan with a very low input price but a high output price may not actually be cheaper for workloads dominated by long-form generation.

    Engineering teams therefore need a unified metric that folds multiple pricing components into a single comparable number: the blended unit price. It answers one question—under a specific workload profile, what is the average cost per token?

    Three Pricing Components

    Cached input (cache hit). When a request’s prefix matches a recent request, the server can reuse previously computed intermediate results, and those tokens are billed at a lower read price. Cache read pricing is usually significantly cheaper than standard input pricing, so improving cache hit rate is one of the most direct ways to cut costs.

    Uncached input (cache miss). New content or uncached prefixes are billed at the standard input price. This is typically the largest share of input cost, especially in multi-turn conversations or scenarios where each request carries fresh documents.

    Output. Tokens generated by the model are billed at the output price. Output rates are usually higher than input rates, so even small changes in output length can affect total cost more than comparable changes in input.

    The Blended Unit Price Formula

    Let a request have I input tokens and O output tokens, with cache hit rate r, cache read price p_cache, uncached input price p_in, and output price p_out. Total cost is:

    1. C = I × [r × p_cache + (1 − r) × p_in] + O × p_out

    Average cost per token (the blended unit price) is:

    1. P = C / (I + O)

    Let w = O / (I + O) be the output share, so the input share is 1 − w. The formula becomes:

    1. P = (1 − w) × [r × p_cache + (1 − r) × p_in] + w × p_out

    This has two implications. First, the blended price depends on three workload parameters: cache hit rate, the input/output ratio, and the individual prices. Second, it puts cache, input, and output under the same denominator, so results are directly comparable.

    Worked Example

    Suppose a workload has 80% input tokens and 20% output tokens per request (w = 0.2), with a cache hit rate of 50%. Assume the cache read price is 10% of the input price, and the output price is 4× the input price. Using the input price as 1 unit:

    1. Input component = 0.5 × 0.1 + 0.5 × 1 = 0.55
    2. Blended price = 0.8 × 0.55 + 0.2 × 4 = 0.44 + 0.8 = 1.24

    The result shows that even with half of the input served from cache, output still accounts for a large share of the blended cost. If the output share rises from 20% to 30%, the blended price climbs noticeably. Note that these multipliers are illustrative assumptions only—always use the provider’s official pricing page for real calculations.

    Key Variables That Affect the Blended Price

    Cache hit rate is the most sensitive variable. It depends on whether your prompt structure is stable, whether sessions are continuous, and whether the cache’s TTL is long enough to cover the gap between requests. Placing fixed system instructions and examples at the front of the prompt and variable user content at the end makes a stable prefix more likely.

    Input/output ratio is determined by the workload. Summarization, classification, and extraction tasks produce short outputs, so the blended price is driven mainly by input. Writing and code generation tasks produce longer outputs, so the output price carries much more weight.

    Pricing rules can also change the result. Some providers charge different rates for peak vs. off-peak hours, or tier pricing by context length—rates may rise overall once context exceeds a threshold. When computing the blended price, calculate separately for each time window and length tier based on your actual request distribution, then take a weighted average rather than applying a single tier’s rate.

    Comparing Options Fairly

    When comparing plans or providers, keep the basis consistent:

    1. Same currency and unit — e.g., normalize everything to price per million tokens.
    2. List price vs. effective price — official reference prices may differ from actual settlement prices due to discounts, credits, or volume tiers. Don’t mix them.
    3. Record the effective date of prices, since pricing rules change frequently.
    4. Use the same workload assumptions — identical hit rate and input/output ratio—otherwise rankings are meaningless.

    A single blended-price ranking shouldn’t drive decisions on its own. Latency, throughput, context length limits, output quality, reliability, and compliance requirements may all matter more than unit price. Treat the blended price as the cost dimension’s input to a broader evaluation.

    Engineering Practice: Building Cost Estimation Into Your System

    You can log per-request token usage along the call chain, aggregate it periodically, and compute the actual blended price with the formula. The following pseudocode is illustrative and language-agnostic:

    1. function estimateBlendedPrice(hitRate, inputPrice, cacheReadPrice, outputPrice, outputShare):
    2. inputComponent = hitRate × cacheReadPrice + (1 − hitRate) × inputPrice
    3. blendedPrice = (1 − outputShare) × inputComponent + outputShare × outputPrice
    4. return blendedPrice

    In production, log the following metrics and feed them into your monitoring and alerting system: daily cache hit rate, average input/output ratio, per-request cost broken down by model and use case, and the effective date of each price. A sudden drop in hit rate often signals that a prompt template was changed or that session management is broken—these anomalies usually deserve priority over price changes when investigating.

    Summary

    The blended unit price collapses multiple pricing components into a single comparable number, making it useful for vendor selection, budgeting, and cost monitoring. Computing it requires accounting for cache hit rate, input/output structure, and the applicable pricing rules, while keeping comparison baselines consistent. Only by combining the formula with real workload data and the provider’s official pricing can you arrive at a reliable cost assessment.