How to Calculate the True Cost of Using Large Language Models Amid Price Increases

    As LLM service prices rise, unit price per million tokens is no longer the sole metric for budgeting. This article explains how to calculate true usage costs by breaking down billing units (input, output, and cache hit tokens), comparing subscription plans with pay-as-you-go billing, measuring average task consumption, and factoring in retries and quality. It also provides practical implementation strategies for cost optimization.

    Beyond Unit Price: Evaluating True Cost After Price Increases

    LLM services are typically priced per million tokens. A significant increase in unit price directly impacts budgets, but the unit price is only one input. Under the same unit price, the actual cost of a single task depends on the consumption of input, output, and cached tokens, as well as the number of attempts needed to complete the task. Therefore, to assess the impact of price hikes, you must convert unit prices into “cost per task” and “cost per effective output.”

    1. Understanding the Three Billing Units

    1. Input tokens: Includes user prompts, context, conversation history, and retrieval results. In multi-turn conversations and long document tasks, input volume grows rapidly.
    2. Output tokens: The content generated by the model. The output unit price is generally higher than the input unit price. Some reasoning models also generate internal reasoning tokens, which may be billed as output in certain services.
    3. Cache hit tokens: When the same prefix is reused, the service provider may charge a lower price. Stable system prompts and fixed document prefixes are most likely to hit the cache.

    2. From Unit Price to Cost Per Task

    The formula for calculating the cost of a single task is as follows:

    Task Cost = (Uncached Input Tokens × Input Unit Price + Cache Hit Tokens × Cache Unit Price + Output Tokens × Output Unit Price) ÷ 1,000,000

    Where uncached input tokens equal total input tokens minus cache hit tokens. The following Python function implements this calculation:

    1. from dataclasses import dataclass
    2. @dataclass
    3. class PriceTable:
    4. input_per_m: float # Price per million input tokens
    5. output_per_m: float # Price per million output tokens
    6. cache_hit_per_m: float # Price per million cache hit tokens
    7. def task_cost(price: PriceTable, input_tokens: int,
    8. output_tokens: int, cache_hit_tokens: int) -> float:
    9. uncached = max(input_tokens - cache_hit_tokens, 0)
    10. total = (
    11. uncached * price.input_per_m
    12. + cache_hit_tokens * price.cache_hit_per_m
    13. + output_tokens * price.output_per_m
    14. )
    15. return total / 1_000_000

    Assume a task has the following sample prices (not actual pricing): input at 4 per million tokens, output at 16 per million tokens, and cache hits at 0.4 per million tokens. The task has a total input of 50,000 tokens, of which 40,000 hit the cache, and an output of 8,000 tokens.

    • With cache enabled: 10,000 × 4 + 40,000 × 0.4 + 8,000 × 16 = 184,000, which is 0.184.
    • Without cache: 50,000 × 4 + 8,000 × 16 = 328,000, which is 0.328.

    Enabling cache reduces the cost of this task by approximately 44%. The savings depend on the hit rate, so the cache hit rate should be tracked as a continuous monitoring metric.

    3. Comparing Subscription Plans vs. Pay-As-You-Go

    Subscription plans typically provide a certain quota, which may be represented by the number of requests, billing points, or monetary amount. When comparing, you should convert the plan into a unit task cost:

    Effective Cost Per Task = Monthly Subscription Fee ÷ Number of Tasks Actually Completed in a Month

    Example: A monthly subscription costs 300. Assuming an average consumption of 0.2 per task, it can cover approximately 1,500 tasks, making the effective cost per task 0.2. If only 800 tasks are actually completed, the effective cost rises to about 0.375.

    When comparing plans, note the following:

    • Plan utilization determines the real price. Low utilization means fixed monthly fees will drive up the cost per task.
    • Before purchasing, confirm whether the quota covers all models, if concurrency and rate limits exist, and how overages are billed.
    • Quota rules should be based on the official documentation provided by the service; budgets should not rely on secondhand information.

    4. How to Measure True Average Task Consumption

    A few exceptionally long tasks can significantly skew the average, so it is recommended to also track percentiles. The measurement steps are as follows:

    1. Sample at least 50 tasks from real business scenarios, covering short, medium, and long tasks.
    2. Record the usage fields returned by each request in the call logs, including input, output, and cache hit token counts.
    3. Calculate the median, P90, and P99. The median reflects regular tasks, while P90 and P99 reflect tail costs.
    4. Repeat the test on candidate models using the same task set to ensure a fair horizontal comparison.

    Here is a Python code example for the statistics:

    1. import statistics
    2. def summarize_costs(costs: list[float]) -> dict:
    3. ordered = sorted(costs)
    4. n = len(ordered)
    5. return {
    6. "mean": statistics.mean(ordered),
    7. "median": statistics.median(ordered),
    8. "p90": ordered[min(int(n * 0.9), n - 1)],
    9. "p99": ordered[min(int(n * 0.99), n - 1)],
    10. }

    5. Factoring Retries and Quality into Cost

    If a model with a lower unit price has a low first-pass success rate, it will require multiple retries or manual corrections, meaning the actual cost may not be lower. The effective cost can be estimated using the following formula:

    Effective Cost = Task Cost ÷ First-Pass Success Rate

    This formula assumes that each attempt is independent. If the task cost is 0.184 and the first-pass success rate is 60%, the effective cost is about 0.307. If the success rate improves to 90%, the effective cost drops to about 0.204. In practice, failures often concentrate on high-difficulty tasks, so the retry cost is usually higher than this estimate.

    Beyond cost, you should also evaluate the following factors:

    • Latency: Interactive products are sensitive to time-to-first-byte and total duration.
    • Context window: The window size required for long document tasks determines whether they can be processed in a single pass.
    • Stability: Timeout and error rates affect retry costs and user experience.
    • Data compliance: Data storage locations, retention policies, and training usage terms need legal confirmation.
    • Migration cost: The adaptation effort for prompts, evaluation sets, and tool calling methods across different models.

    6. Implementation Recommendations

    1. Build a cost dashboard: Break down usage by project, feature, and model to identify cost-concentrated areas.
    2. Set budgets and alerts: Define daily usage and monthly limits for each project, and notify owners when approaching thresholds.
    3. Optimize caching: Place stable system prompts and knowledge base prefixes at the beginning of the request to minimize prefix changes.
    4. Tiered routing: Assign simple tasks like classification and extraction to lower-cost models, and upgrade complex reasoning tasks to high-capability models.
    5. Regular reviews: Re-evaluate using the same task set every quarter, as unit prices, model capabilities, and usage patterns will change over time.