LLM API Pricing Explained: Token Rates, Caching, and Real Task Costs

    Learn how to compare LLM API costs beyond per-token rates. This guide explains input, output, and cached-token billing, shows how to calculate task costs in Python, and covers success rates, subscription plans, and practical criteria for choosing a service.

    1. Why Per-Token Rates Are Not Enough

    LLM API pricing tables typically list several rates: input tokens, output tokens, and cached input tokens. It is tempting to label a service expensive or cheap based on just one of these numbers. That approach has two problems.

    First, rates are usually quoted per million tokens, but actual token usage depends on the task. Even at the same rate, processing long documents, handling multi-turn conversations, or generating extensive reasoning output can lead to costs that differ severalfold.

    Second, billing structures vary. Some services charge much more for output than input. Others offer caching discounts or monthly subscriptions with usage allowances. Comparing a single rate rarely reveals the total cost.

    A better approach is to put services on a consistent basis and compare the cost of completing a task.

    2. Understand the Three Basic Token Categories

    Input Tokens

    Input tokens include system instructions, conversation history, retrieved material, and the user’s current question. Longer contexts consume more input tokens. Many applications resend conversation history with every turn, making this an easy expense to underestimate.

    Output Tokens

    Output tokens are the content generated by the model. Output rates are generally higher than input rates because generation proceeds token by token and requires more computational resources. For tasks that need long responses or extensive reasoning output, output charges can account for a substantial share of the total.

    Cached Input Tokens

    A cache hit occurs when the service reuses a previously processed, identical request prefix rather than processing it again. Cached tokens typically cost much less than ordinary input tokens.

    The savings depend on two conditions: the request prefix must remain stable, and repeated requests must arrive before the cache expires.

    Caching can significantly reduce costs for fixed system prompts, question answering over long documents, and batch processing against the same source material. If the prefix changes with every request, caching offers little benefit.

    3. Calculate the Cost of a Single Task in Python

    The following function combines all three token categories. The prices are hypothetical; use the relevant provider’s actual pricing when estimating costs.

    1. def estimate_task_cost(
    2. input_tokens: int,
    3. output_tokens: int,
    4. cached_tokens: int,
    5. price_in: float, # CNY per million tokens
    6. price_out: float, # CNY per million tokens
    7. price_cache: float, # CNY per million tokens
    8. ) -> float:
    9. """Calculate task cost in CNY. cached_tokens are included in input_tokens."""
    10. uncached = input_tokens - cached_tokens
    11. cost = (
    12. uncached * price_in
    13. + cached_tokens * price_cache
    14. + output_tokens * price_out
    15. )
    16. return cost / 1_000_000
    17. # Hypothetical prices for demonstration only; not actual provider rates.
    18. cost = estimate_task_cost(
    19. input_tokens=200_000,
    20. output_tokens=20_000,
    21. cached_tokens=150_000,
    22. price_in=10,
    23. price_out=40,
    24. price_cache=1,
    25. )
    26. print(f"Cost per task: CNY {cost:.2f}")

    This example highlights an often-overlooked point: even when cached input is inexpensive, output can still dominate the bill if the model generates enough tokens. Cost optimization therefore needs to address both the input cache hit rate and output length.

    4. Move from Cost per Attempt to Cost per Successful Task

    The cost of a single attempt does not fully capture the cost of obtaining a usable result. If a model requires retries, manual corrections, or repeated follow-up questions, the practical cost is higher than the initial API charge suggests.

    A more useful metric is cost per successful task:

    Cost per successful task = Average cost per attempt ÷ First-pass success rate

    For example, if an attempt costs CNY 1 and the first-pass success rate is 50%, the estimated cost per successful task is CNY 2. Another service that costs CNY 1.5 per attempt but succeeds 90% of the time has an estimated cost per successful task of about CNY 1.67—lower despite its higher per-attempt price.

    Evaluate services using real business samples rather than demonstration data alone. Include simple tasks, complex tasks, and edge cases. For each request, record input and output token usage, latency, and whether the result is usable.

    5. Compare Subscriptions with Usage-Based Billing

    Some services offer monthly or annual plans with included usage allowances. To compare these plans with usage-based pricing:

    1. Identify how the allowance is measured. It may be based on tokens, request counts, or another conversion rule.
    2. Estimate actual monthly usage. Use historical logs where possible rather than relying on intuition.
    3. Calculate the effective rate. Divide the subscription price by the available monthly allowance.
    4. Check the restrictions. Review rate limits, concurrency limits, allowance rollover rules, and overage charges.

    A plan’s advertised allowance is not necessarily the amount you can use in practice. Peak-time concurrency restrictions and per-request context limits can reduce the number of tasks you can actually complete.

    6. Evaluate Performance and Cost Together

    Public benchmarks show how models perform on specific evaluations, but those evaluations may not match your workloads. Compare services across the following dimensions:

    • Capability: Accuracy, consistency, and format compliance on your own task set.
    • Cost: Cost per attempt and cost per successful task.
    • Latency: Time to first token and total response time, which affect user experience and throughput.
    • Control: Support for adjusting caching strategies, limiting output length, and using structured output.
    • Reliability: Service availability, rate-limiting policies, and notifications about version changes.

    A model with higher token rates is not necessarily more expensive overall. If it completes tasks in fewer attempts, its cost per successful task may be lower. Conversely, a low-rate model can become more expensive if it requires frequent retries.

    7. Practical Recommendations for Enterprise Selection

    1. Standardize cost tracking. Record token usage, cache hits, latency, and result status for each task.
    2. Run small-scale A/B tests with real workloads. Compare cost per successful task across the same task set.
    3. Optimize prompt structure. Place stable content at the beginning of requests to improve cache hit rates.
    4. Set reasonable output limits. Request structured formats to reduce unnecessary generation.
    5. Use different models for different task values. Avoid routing every request to the same model tier.
    6. Review bills alongside business metrics. Reassess after pricing changes or model updates.

    The central question in LLM cost management is not simply how much a token costs, but how much you pay for each usable result. Comparing billing units, caching behavior, subscription allowances, and output quality within one framework leads to more reliable model-selection decisions.