Rising LLM API Prices: How to Calculate Costs per Task

    This article explains why a higher per-token price does not always raise the cost of a task. It covers how reasoning output, prompt caching, retries, and tool-call loops change total spend, and it provides a Python example for estimating cost per task. It also outlines how to compare models on cost and completion rate, and how to set up cost monitoring.

    1. Why Pricing Changes Draw Attention

    LLM services usually charge per million tokens, with separate prices for input and output. When unit prices rise, developers first notice the list price. But the list price alone does not show whether a workload is affordable. To judge the cost of a model, look at the total spend for one specific piece of work.

    Pricing strategies also have underlying logic. Some providers align their prices with a standard market tier. Others treat the price as a valuation of the model’s capability. Both reflect pricing strategy, so neither should be used directly as a cost basis. In addition, the same model can cost very different amounts across channels, because promotions, volume discounts, and third-party hosting all change the amount actually paid. When a list price changes, confirm the billing terms and the channel you are using.

    2. Reasoning Models Make Output the Main Expense

    Early chat models usually received long inputs and produced short outputs. Reasoning models generate an internal reasoning process before giving the final answer. These reasoning tokens are generally billed as output tokens. They are not free just because the user cannot see them.

    This has a direct consequence. The more thoroughly a model reasons, the more output tokens it produces, and the higher the bill. If the server’s default reasoning depth is high, actual spending rises even when the unit price stays the same. Therefore, evaluation should track two metrics: the unit price and the average output length per call. Looking only at the unit price can underestimate the real cost.

    3. Cache Hits Change the Input-Side Cost

    Multi-turn conversations and agent workflows repeatedly send the same system prompts, tool definitions, and conversation history. If the server supports prefix caching, the cached portion is usually billed at a lower unit price.

    Engineers can use this. Place stable content at the beginning of the prompt and content that changes with each request at the end. This increases the prefix cache hit rate. Note that cache lifetimes, minimum cacheable lengths, and billing rules vary by provider. Read the relevant API documentation before integrating, and measure with real requests.

    4. Shifting from Per-Token Accounting to Per-Task Accounting

    In automation and batch scenarios, the business usually cares about how much it costs to complete one task. The cost of a single task has three components:

    • Input, cached input, and output token counts for each model call
    • The number of calls, including follow-up inference after tool results and retries after failures
    • The unit price for each token type

    The formula is:

    Task cost = sum of all call costs, where call cost = fresh input tokens × input price + cached input tokens × cache price + output tokens × output price

    The code below estimates task cost using this formula. The example prices are for demonstration only and do not represent any actual quotes.

    1. def single_call_cost(input_tokens, cached_tokens, output_tokens,
    2. price_in, price_cache, price_out):
    3. """Calculate the cost of a single call. Prices are in CNY per million tokens."""
    4. fresh_tokens = input_tokens - cached_tokens
    5. cost = (fresh_tokens * price_in
    6. + cached_tokens * price_cache
    7. + output_tokens * price_out)
    8. return cost / 1_000_000
    9. def task_cost(calls, **prices):
    10. """calls is a list. Each item holds the token statistics for one call."""
    11. return sum(single_call_cost(**c, **prices) for c in calls)
    12. if __name__ == "__main__":
    13. prices = dict(price_in=10.0, price_cache=1.0, price_out=50.0) # Example prices
    14. calls = [
    15. {"input_tokens": 20000, "cached_tokens": 15000, "output_tokens": 3000},
    16. {"input_tokens": 26000, "cached_tokens": 24000, "output_tokens": 1200},
    17. ]
    18. print(f"Total task cost: {task_cost(calls, **prices):.4f} CNY")

    5. Comparing Models on a Common Basis

    Comparing costs across models should not rely on unit price alone. A more reliable approach is to fix a set of representative tasks and one evaluation standard, then record three metrics: task completion rate, average cost per task, and average latency.

    A model with a higher unit price can end up cheaper overall if it passes more tasks on the first try and needs fewer retries. Conversely, a model with a lower unit price may exceed the expected budget if it needs many rounds of correction.

    When you self-host open-weight models, costs also include GPU purchase or rental, operations staff, and GPU utilization. These costs are not measured on the same basis as API unit prices, so convert them separately before comparing.

    6. Controlling Costs in Agent Systems

    Agents decide for themselves how many calls to make and how deeply to reason, so costs can easily get out of control. Common controls include:

    • Setting an upper limit on reasoning depth, and choosing the level based on task difficulty instead of always using the highest one
    • Setting a maximum number of calls and a maximum token count for each task, and stopping and logging the task when a limit is exceeded
    • Truncating or summarizing long tool outputs to prevent the context from growing with every turn
    • Assigning simple subtasks to cheaper models, and escalating to a higher-tier model only for complex decisions
    • Logging token usage per task, reviewing cost distribution regularly, and finding the task types with unusually high consumption

    7. Implementation Steps

    1. Collect data: At the gateway or the model-call wrapper, record the model name, input tokens, cached tokens, output tokens, and latency for every request.
    2. Set a baseline: Select a representative task set and measure the current average cost and completion rate.
    3. Tune parameters: Adjust reasoning depth and context length separately, and observe how cost and quality change.
    4. Route by tier: Assign models by task type, so simple tasks use the lower-cost path.
    5. Monitor continuously: Set alert thresholds for cost per task and daily spend, and investigate promptly when an anomaly appears.

    Conclusion

    A higher unit price does not necessarily mean a higher cost per task, and a lower unit price does not necessarily mean lower costs. Total spend depends on task completion rate, token structure, and the number of calls. When cost is measured per task instead of per million tokens, teams can make comparable decisions about model selection, reasoning depth, and context design.