LLM API Cost in Practice: A Guide to Effective Cost and Model Selection

    Choosing an LLM API by sticker price alone is misleading. This article explains how to calculate effective cost, why accuracy targets change the bill, how prompt caching and time-based pricing pull in opposite directions, and how to run a reproducible one-week A/B test. It also covers four underestimated cost traps, an engineering cost-reduction checklist, and an ROI view of deployment on Baidu AI Cloud.

    1. The Real Dispute Is Effective Cost, Not Unit Price

    Whenever a new model reaches the market, the discussion usually starts with its input and output price per million tokens. But unit price is only the starting point on paper. What actually determines the bill is the effective cost: all the tokens a business task consumes, multiplied by the corresponding unit prices. One new model may advertise under one yuan per million input tokens and under three yuan per million output tokens, while another option uses time-based floating pricing. Comparing the two statically is not a fair exercise.

    Effective cost can be expressed as a formula:

    1. effective cost = (input tokens + redundant tokens from retry amplification) × input unit price
    2. + (output tokens + redundant tokens from retry amplification) × output unit price
    3. - discount from cache hits

    Three variables are easy to overlook: the cache hit rate, the extra rounds added to improve accuracy, and the price fluctuation caused by time-based billing. A change in any one of them can significantly narrow or even reverse the gap on paper.

    2. Converting Accuracy into Cost

    High accuracy is not free. Common approaches include sampling multiple times and voting, self-verification, and decomposing a task into multiple reasoning steps before aggregating the results. All of these multiply token consumption. Assume a single call has accuracy p. Taking a majority vote over three independent samples triples the number of calls, while the error rate may drop from (1-p) to a small quantity close to a cubic term.

    When comparing options, the right metric is the total cost of reaching the same accuracy threshold:

    1. def effective_cost(unit_price, tokens, rounds, cache_hit_rate=0.0, discount=0.0):
    2. billable_in = tokens["in"] * rounds * (1 - cache_hit_rate * discount)
    3. billable_out = tokens["out"] * rounds
    4. return billable_in * unit_price["in"] + billable_out * unit_price["out"]

    If one option is cheaper per round but needs three rounds to pass, and another is more expensive per round but passes in one, the total cost of the latter may be lower. Putting accuracy into the cost model is the first step toward avoiding a wrong decision.

    3. Caching and Time-Based Pricing: Two Levers Pulling in Opposite Directions

    Prompt caching reuses the stable prefix of a request, such as the system prompt, tool descriptions, and a few examples. On a cache hit, that portion of the input is billed at a lower price. The hit rate depends on whether the prefix is byte-for-byte stable; any timestamp, random identifier, or dynamically concatenated content invalidates the cache. In engineering terms, keep invariant content at the front and variable content at the back.

    Time-based pricing works the other way: the price moves with load, higher at peak and lower off-peak. For offline batch jobs, scheduling work into off-peak windows is a direct way to cut cost. For online interactions, you need to weigh how much latency and experience degradation is tolerable. When the two levers are combined, model them against your actual traffic distribution rather than estimating with ideal values.

    4. How to Run a Reproducible A/B Test

    Conclusions should come from data, not impressions. The recommended minimum viable approach:

    1. Fix the evaluation set: sample no fewer than a few hundred requests from real business traffic, covering long documents, short question answering, structured extraction, and other types.
    2. Fix the parameters: keep temperature, maximum output length, retry policy, and timeout settings identical.
    3. Run both tracks in parallel: run for one week in the same environment, recording input and output tokens per call, cache hit status, number of retries, and whether the call finally passed validation.
    4. Unify the metrics: use cost per thousand successful tasks and P95 latency as the primary metrics, not unit price.

    An illustrative record structure:

    1. {"task_id":"t-001","model":"A","in":1820,"out":260,"rounds":2,"cache":true,"pass":true,"latency_ms":2140}

    Only after a week, aggregated by task type, can you see which option is actually cheaper for your load profile.

    5. Four Underestimated Cost Traps

    • Retry storms: retrying blindly whenever validation fails can multiply token usage several times over. Set a maximum number of rounds and a fallback strategy.
    • Runaway output: without a maximum output length, long-tail requests will eat the budget.
    • Context bloat: carrying the entire history without trimming makes input cost grow linearly with the session. Apply summarization and retrieval-based recall.
    • Cache invalidation: dynamic fields polluting the prefix make the discount useless.

    6. An Engineering Cost-Reduction Checklist

    • Tiered routing: send simple tasks to a lightweight model and escalate complex ones, using rules or a small classifier for the upfront decision.
    • Batch first: for offline scenarios, use the asynchronous batch API, combined with object storage and a message queue to smooth out peaks.
    • Structured constraints: constrain output with a schema to reduce repeated calls caused by parsing failures.
    • Observability: build a token dashboard through logging and monitoring with alerts, and set budget alerts per business line.
    • Elastic capacity: scale elastically on a container platform so inference traffic aligns with business peaks.

    7. The ROI View and Implementation on Baidu AI Cloud

    Cutting cost does not mean cutting quality. It comes back to return on investment: does the reduction in per-task cost cover the engineering effort and the business loss caused by accuracy fluctuation? It is advisable to start with a canary release in edge scenarios and then gradually expand traffic.

    On Baidu AI Cloud, model services can be combined with general capabilities such as object storage, message queues, container platforms, logging, and monitoring with alerts to build a closed loop of evaluation set management, dual-track canary release, a token dashboard, and budget alerts. This lets data drive model selection and parameter tuning, instead of being led by a single price sheet.

    8. Conclusion

    The essence of the price dispute is inconsistent measurement. Put the cache hit rate, retry rounds, time-based fluctuation, and accuracy threshold into the same cost model, then validate with a reproducible one-week A/B test, and model selection turns from a subjective argument into an engineering problem.