Why API Pricing Alone Is Not Enough: The Real Cost of Completing a Task

    API prices may remain unchanged while the cost of completing a task increases. This article explains how input, cached input, and output tokens contribute to total spending, why longer outputs can raise costs, and how to compare models using cost per successful task. It also covers benchmarking, token tracking, cache optimization, output control, budget alerts, and a Python example for calculating task costs.

    1. Why Unit Price Is No Longer the Only Metric

    In the past, developers often compared models primarily by their price per million tokens. This can be useful when model capabilities are similar, but it overlooks two important variables: how many tokens are actually required to complete the same task, and how likely the task is to succeed.

    As models become more capable, teams are shifting from asking, “How much does each token cost?” to asking, “How much does it cost to complete one task?” The former is a number on a pricing page. The latter has a direct impact on the budget.

    2. What Makes Up the Cost of a Single Task

    The cost of a model call usually consists of three components:

    1. Uncached input tokens;
    2. Cached input tokens;
    3. Output tokens.

    Under most pricing models, cached input tokens are charged at a lower rate. One important detail is that cached tokens are part of the total input token count. Therefore, uncached input must be calculated by subtraction.

    The cost of a single call can be expressed as:

    1. Call cost =
    2. (uncached input tokens × input price
    3. + cached input tokens × cached price
    4. + output tokens × output price) / 1,000,000

    The cost of a task is the sum of all calls made during that task. Agent-based workflows often involve multiple rounds of tool calls. Each round may resend the context, making both the number of calls and the context length major cost factors.

    3. Why Costs Can Rise Even When Prices Stay the Same

    With unchanged unit prices, the total cost still depends on how many tokens each task consumes. Let s represent the share of output tokens in the total task cost, and let r represent the percentage increase in output tokens. If all other conditions remain unchanged, the increase in task cost is approximately:

    1. s × r

    For example, if output accounts for half of the total cost and output tokens increase by 20%, the total cost will rise by approximately 10%. This increase is unrelated to whether the listed prices have changed. It depends only on the output cost share and the change in output length.

    Output may become longer for several reasons:

    • The model tends to provide more detailed reasoning or explanations;
    • The prompt does not specify an output format;
    • Intermediate results are returned in full during multi-step tasks;
    • Additional self-checking or correction steps are introduced to improve success rates.

    When evaluating a new model version, teams should therefore track both capability metrics and token usage for each task. Looking only at unit prices can lead to an underestimation of actual spending.

    4. Measure Value with Cost per Successful Task

    A model with a lower unit price is not necessarily cheaper. If its task success rate is lower, retries or manual corrections may increase the total cost.

    A useful metric is cost per successful task:

    1. Cost per successful task = total task cost / number of successful tasks

    This metric evaluates cost and quality on the same scale. To compare different models fairly:

    • Use the same task set, with consistent task types and difficulty;
    • Apply the same criteria to determine whether a task succeeded;
    • Track retries and include retry costs in the total;
    • Record average input length, output length, and cache hit rate.

    Comparisons are meaningful only when these conditions are consistent. Third-party benchmark scores can provide background information, but they may not accurately reflect a specific business workload. Internal benchmarks are still necessary for validation.

    5. Engineering Practices

    1. Build an Internal Benchmark Set

    Extract representative samples from real production requests. The set should cover simple question answering, code generation, document processing, and multi-step tool calls.

    For each task category, retain the input, expected result, and evaluation criteria. This makes repeated testing possible whenever the model or prompt changes.

    2. Record Token Usage for Every Call

    Most APIs include usage information in their responses. Store the total input tokens, cached input tokens, and output tokens in logs using a request ID, then aggregate the data by task type.

    This helps identify which parts of the workflow are actually driving costs.

    3. Improve Cache Hit Rates

    Caching depends on a stable request prefix. System prompts, tool definitions, and fixed examples should appear at the beginning of the request, while dynamic content should be placed at the end.

    Putting variable timestamps or random IDs in the prefix can directly reduce cache hit rates.

    4. Control Output Length

    Output length can be constrained in several ways:

    • Specify the required format in the prompt, such as JSON or a fixed set of fields;
    • Set a reasonable maximum output token limit and implement error handling for truncated responses;
    • Replace complete intermediate results with summaries to reduce the amount of content sent back.

    5. Set Budget Alerts

    Include per-task cost in the monitoring system and use standard monitoring and alerting capabilities to define thresholds.

    For example, when the average cost of a task category remains above 1.5 times its baseline for one hour, trigger a notification. This makes it easier to investigate prompt changes or model version upgrades that may have affected spending.

    6. Cost Estimation Example

    The following code calculates the cost of a single task and the cost per successful task. Prices are passed in as parameters. In production, use the latest prices published in the service provider’s official pricing documentation.

    1. from dataclasses import dataclass
    2. @dataclass
    3. class Usage:
    4. input_tokens: int # Total input tokens, including cached tokens
    5. cached_input_tokens: int # Cached input tokens
    6. output_tokens: int # Output tokens
    7. @dataclass
    8. class Price:
    9. input_per_m: float # Price per million uncached input tokens
    10. cached_per_m: float # Price per million cached input tokens
    11. output_per_m: float # Price per million output tokens
    12. def call_cost(usage: Usage, price: Price) -> float:
    13. uncached = usage.input_tokens - usage.cached_input_tokens
    14. if uncached < 0:
    15. raise ValueError("Cached tokens cannot exceed total input tokens")
    16. return (
    17. uncached * price.input_per_m
    18. + usage.cached_input_tokens * price.cached_per_m
    19. + usage.output_tokens * price.output_per_m
    20. ) / 1_000_000
    21. def task_cost(calls: list[Usage], price: Price) -> float:
    22. # The task cost is the sum of all call costs within the task
    23. return sum(call_cost(u, price) for u in calls)
    24. def cost_per_success(costs: list[float], successes: list[bool]) -> float:
    25. # Cost per successful task: total cost divided by successful tasks
    26. ok = sum(1 for s in successes if s)
    27. if ok == 0:
    28. return float("inf")
    29. return sum(costs) / ok

    To use the code, first collect the list of calls associated with each task. Pass that list to task_cost to calculate the task cost, then use cost_per_success to compare the cost-effectiveness of different models or prompt versions.

    7. Important Considerations

    • Use the latest pricing rules published by the service provider. Prices, caching rules, and minimum billing units may change.
    • Composite benchmark scores have a limited shelf life and may not match a specific business workload. Use them together with internal benchmark results.
    • Results can differ by task type. A model that performs well for code generation may not be the best choice for long-document summarization.
    • Capability improvements often come with changes in output length. Always measure quality and token usage together.
    • If you plan to move to an open-weight deployment in the future, also account for compute, operations, and scaling costs. API pricing alone is not sufficient.

    Conclusion

    API unit pricing is only one component of total cost. To determine whether a model is cost-effective, teams must consider unit prices, cache hit rates, output length, and task success rates together.

    An internal benchmark, detailed per-call usage tracking, and cost per successful task provide a more reliable basis for decision-making. With this approach, model selection can remain data-driven and reproducible even as models and pricing structures evolve.