Large Language Model API Cost: How to Evaluate Token Efficiency

    Choosing a large language model API by price per million tokens alone often leads to wrong cost estimates. This article breaks the total cost of a task into input tokens, cached input tokens, output tokens, and retries, explains why token efficiency can differ by two to three times across models on long, tool-heavy workflows, and outlines a reproducible benchmark method that judges success by task completion and validation rather than unit price.

    1. Why Unit Price Alone Leads to Wrong Estimates

    When integrating large language models into real workflows, teams most often make one mistake: treating the price per million tokens as the only decision criterion. An actual bill is determined by several variables together — input tokens, output tokens, cached input tokens, tokens consumed by retries and failures, and the number of round trips needed to reach the same task goal. A model with a low unit price but repeated trial and error can end up costing far more than a model with a higher unit price that gets the task right on the first attempt.

    The original observation comes from a typical scenario: coding and automated testing tasks, where a model must read a large number of files, apply changes across multiple rounds, run tests, and keep iterating based on feedback. These tasks are characterized by long contexts, many tool calls, and frequent failure retries, so token consumption is tightly coupled with task quality. The focus of evaluation should therefore shift from comparing unit prices to comparing the total cost of completing the same task.

    2. Cost Model: Breaking the Bill into Computable Parts

    A more rigorous estimate expresses the cost of a single task as:

    1. Total cost = input tokens × input price
    2. + cached input tokens × cached input price
    3. + output tokens × output price
    4. + extra token cost from failed retries

    Cached input usually accounts for the largest share in long-context tasks. If two models differ noticeably in cached input price, the total cost can diverge by several times even when input and output prices look similar. For this reason, evaluation must first confirm how caching is billed: whether cache is charged by hits or by writes, how long the cache stays valid, and whether it is charged again after expiring. These are common billing dimensions across platforms and must be confirmed against the actual documentation rather than assumed.

    3. Token Efficiency: The Undervalued Variable

    Token efficiency means the total number of tokens consumed to complete the same task. For the same amount of work, consumption across models can differ by more than a factor of two. The reasons include how context is organized, the tool-calling protocol, the strategy for handling long files, and whether the model tends to return a complete change in one shot — all of which affect the number of round trips.

    A sample record table can quantify this:

    Task type Model A consumption Model B consumption Completion quality
    Multi-file read/write + tests ~100K ~250K–300K Both require manual spot checks
    Single-file refactor To be measured To be measured To be measured

    In practice, token consumption on long-chain tasks can differ by 2–3x. Layer a difference in cached input price on top of that, and the gap in total cost widens further. A headline discount such as “40% off the unit price” must therefore be validated inside the full cost model rather than taken as proof that a model is cheaper.

    4. Measurement Method: A Reproducible Comparison Process

    Use a fixed task set for controlled experiments so that variables stay manageable:

    1. Select 3–5 real tasks covering multi-file changes, test fixes, and log analysis.
    2. Fix the prompts, tool set, and maximum number of rounds, and record input and output tokens for each round.
    3. Record cache hits and the number of failed retries.
    4. Use “task completed and validation passed” as the only success criterion, and total up the cost.
    5. Repeat three times and take the median to avoid occasional fluctuations.

    Sample statistics script:

    1. record = {
    2. "task_id": "multi_file_fix_01",
    3. "input_tokens": 0,
    4. "cached_input_tokens": 0,
    5. "output_tokens": 0,
    6. "rounds": 0,
    7. "success": False,
    8. }
    9. # Accumulate after each call; set success=True once task validation passes

    5. Selection Advice: A Three-Dimensional Judgment

    First, the cost dimension: use the full cost model instead of a unit price table. Second, the efficiency dimension: look at the total tokens and number of rounds needed to complete the same task. Third, the quality dimension: look at the first-pass success rate and the proportion of manual rework. Only by combining all three can teams avoid the trap of “cheaper but more expensive.”

    For implementation, general cloud capabilities from Baidu AI Cloud can help — for example, a log service to record call details, monitoring and alerting to track cost fluctuations, and object storage to archive measurement data — forming a cost baseline that can be reviewed continuously. The final conclusion should rest on reproducible measurement data, not an impression formed from a single price point.