How to Calculate LLM Service Costs: Pricing, Caching, and Quotas Explained

    LLM services are billed by tokens, but the sticker price alone rarely tells you what you'll actually pay. This article breaks down input, output, and cache-hit token pricing, explains the hidden costs of long contexts and multi-turn conversations, shows how to convert subscription quotas into comparable numbers, and provides a benchmark-based method for calculating cost per successful task. It also covers practical engineering tactics for reducing costs and includes a Python code example for estimating per-request expenses.

    How to Calculate LLM Service Costs: Pricing, Caching, and Quotas Explained

    Why List Prices Can’t Be Compared Directly

    LLM services are typically billed by token. A token is the basic unit of text a model processes — one Chinese character roughly maps to one or two tokens, depending on the tokenizer. Even when two services have identical unit prices, the actual cost can differ significantly because completing the same task may consume very different amounts of tokens.

    When comparing prices, you should look at at least three metrics together: unit price, actual token consumption per task, and the task success rate. Looking only at unit price is like comparing fuel prices per liter while ignoring fuel consumption per 100 kilometers.

    Input, Output, and Cache Hits Are Priced Differently

    1. Input tokens: User prompts, conversation history, retrieved documents, and similar content all count as input. The longer the context, the larger the input.
    2. Output tokens: Content generated by the model. Generation requires serial computation token by token, so output prices are usually higher than input prices.
    3. Cache-hit tokens: When the beginning of multiple requests is identical, the server can reuse previously computed intermediate results. This kind of prefix caching (or KV caching) is typically billed at a lower input rate.

    This means placing stable system instructions, role definitions, and fixed reference material at the beginning of a request can significantly improve cache hit rates. If you insert a timestamp or random ID at the start of every request, the prefix can’t be reused and caching will barely help.

    The Hidden Cost of Long Contexts

    In multi-turn conversations, the entire history is resent to the model on every turn. Suppose each turn adds about 500 tokens: after 10 turns, the input on the tenth turn is already close to 5,000 tokens, not 500. Cumulative input grows roughly quadratically with the number of turns — a common reason multi-turn conversation costs spiral out of control.

    Long-document analysis works the same way. Feeding an entire document into the context at once is expensive, and the model’s attention to content in the middle may also degrade. Retrieving relevant passages or summarizing sections first, then sending only those to the model, is usually both cheaper and more accurate.

    Subscriptions and Quota Conversion

    Many platforms use a “subscription plus usage quota” model, where quotas may be tracked on a rolling time window or a weekly basis. The key to understanding these quotas is conversion:

    • First confirm the metering method: whether the quota counts tokens, requests, or some weighted equivalent — this directly affects your conclusions.
    • Run a batch of representative tasks and record how much quota each one consumes.
    • Compare plans using “quota consumed per standard task” instead of the list price.
    • Watch how different windows interact. Once a short window is exhausted, the remaining quota in a longer window doesn’t necessarily mean you can keep using the service.

    Subscriptions suit workloads with frequent, stable usage. Pay-as-you-go suits volatile workloads, high peaks, or occasional use. The break-even point between the two should be calculated from your actual usage data.

    How to Run a Reliable Cost-Effectiveness Evaluation

    Build a small benchmark set using these steps:

    1. Extract 30 to 50 tasks from real business scenarios, covering easy, medium, and difficult cases.
    2. Define verifiable pass criteria for each task, such as passing unit tests or reaching a field-extraction accuracy threshold.
    3. Record input tokens, cache-hit tokens, output tokens, latency, and pass rate for each candidate.
    4. Calculate cost per success, using the number of passed tasks as the denominator — not total tasks.

    The formula is:

    1. Cost per successful task = Total cost / Number of passed tasks

    Failed tasks require manual fixes or reruns, and this hidden cost should be included in the total. A plan with a lower unit price but a poor success rate may end up costing more than one with a higher unit price.

    Engineering Practices for Cost Reduction

    1. Tiered routing: Send simple tasks like classification and format conversion to smaller, cheaper models; reserve stronger models for complex reasoning and code refactoring.
    2. Stable prefixes: Put system instructions and fixed knowledge first to ensure cache hits.
    3. Context trimming: Keep only the history messages and document fragments relevant to the current question.
    4. Output control: Use explicit format requirements and maximum output length limits to avoid wasting output tokens on verbose responses.
    5. Cost monitoring: Track token consumption by module, user, and request type, and regularly review unusually expensive calls.

    Estimation Code Example

    The following function estimates the cost of a single request. The prices in the code are fictional values for demonstration purposes only and do not represent any real service. In practice, replace them with the prices published by your provider.

    1. def estimate_cost(input_tokens: int, cached_tokens: int, output_tokens: int, price: dict) -> float:
    2. """Estimate the cost of a single request. Prices are in currency units per million tokens."""
    3. uncached_tokens = input_tokens - cached_tokens
    4. cost = (
    5. uncached_tokens * price["input"]
    6. + cached_tokens * price["cache_hit"]
    7. + output_tokens * price["output"]
    8. )
    9. return cost / 1_000_000
    10. # Fictional prices for demonstration
    11. demo_price = {"input": 20.0, "cache_hit": 2.0, "output": 80.0}
    12. cost = estimate_cost(input_tokens=12000, cached_tokens=9000, output_tokens=1500, price=demo_price)
    13. print(f"Estimated cost per request: {cost:.4f}")

    When you run this code, the 9,000 cached input tokens are billed at the lower cache-hit rate, so the total cost is clearly lower than if all input were charged at the standard rate. Adjusting the cached_tokens value lets you see directly how cache hit rate affects cost.

    Summary

    To evaluate the cost of an LLM service, start from the actual cost per successful task rather than the list price. The key steps are: understand how tokens and caching are billed, keep context length under control, convert quotas into consumption per standard task, and calculate cost per success using real business samples. Once you’ve done this, your provider selection will be far more reliable than simply comparing numbers, and your subsequent cost-optimization efforts will have a clear starting point.