LLM API Cost Estimation and Model Selection: A Practical Guide

    This guide explains why pay-as-you-go LLM API usage often costs more than expected and how to build a reliable cost model before choosing a model. It covers normalizing different pricing units, the impact of input/output price gaps, context caching conditions, rate multipliers and reasoning effort, retries, and long contexts. It also outlines practical cost-control strategies such as request budgets, context compression, tiered routing, observability, and multi-provider redundancy, helping developers estimate costs accurately and control inference spending.

    1. A Real Scenario: Why Pay-As-You-Go Often Exceeds Expectations

    A developer topped up a credit package on a third-party API aggregation platform: 59 CNY for 5,000 credits, plus an additional 20 CNY top-up that included a 10 CNY bonus, bringing the total balance to 30 CNY. Within two hours, the balance was exhausted while using a flagship model with long-context reasoning at the highest reasoning effort setting. The effective price worked out to roughly 3.6 CNY per dollar of credit, while the official list price converts to about 1.8 CNY per dollar of credit. In other words, calling through the middle layer roughly doubled the cost.

    The value of this case is not in judging any particular platform. It exposes a common problem: developers often compare only the per-million-token price while overlooking credit conversion, rate multipliers, caching billing rules, and the effect of reasoning effort on actual consumption. This article abstracts these factors into a reusable estimation and optimization method.

    2. The First Principle of Cost Estimation: Unify the Unit of Measurement

    Pricing methods vary widely across platforms. Three common types are:

    • Billing directly by token, with separate input and output prices;
    • Billing by credit or dollar-denominated credit, topped up in local currency;
    • Billing by credits, where requests consume credits and credits convert to currency at an exchange rate.

    Before comparing costs, normalize them to the same unit, such as CNY per million tokens. The illustrative conversion logic is:

    1. # Normalize unit price: convert any billing unit to CNY per million tokens
    2. def normalize(price, unit, fx=7.2, credits_per_yuan=100):
    3. if unit == "usd_per_mtok":
    4. return price * fx # USD per million tokens -> CNY per million tokens
    5. if unit == "credits_per_ktoken":
    6. return price * 1000 / credits_per_yuan # credits per thousand tokens -> CNY per million tokens
    7. return price # already CNY per million tokens

    The key point is: bonus credits from top-ups lower the nominal unit price but do not change the real resource consumption per token. A bonus is a marketing tactic and should not enter the denominator of a cost model.

    3. Four Underestimated Cost Amplifiers

    1. The Input/Output Price Scissors Gap

    Output prices for mainstream inference services are typically several times the input price (about 5x in the case above). Scenarios such as intelligent coding and long-document analysis are precisely “large input, long output.” Estimating the budget from the input price alone can cause severe deviation. The correct approach is to weight by the actual input/output ratio of the business scenario:

    1. Effective unit price = input price × input share + output price × output share

    2. The Real Conditions for Caching to Take Effect

    Most platforms offer context caching to reduce the cost of repeated prefixes, but it often has prerequisites: the prefix must match exactly, there is a minimum length threshold, there are time-to-live limits, and cache hits and writes may be billed separately. In the case above, the developer “thought there was no caching, but on closer inspection there was.” This shows that whether a cache hit occurs must be verified with observable data, not assumed. It is recommended to log the number of cached tokens per request on the application side and include it in cost accounting.

    3. Rate Multipliers and Reasoning Effort

    Different models, context lengths, and reasoning effort levels correspond to different multipliers. Setting reasoning effort to the highest level can make a single request consume several times more than the standard level. For most engineering tasks, low-to-medium levels are sufficient; the highest level should be reserved for a very small number of difficult reasoning tasks.

    4. Failed Retries and Overly Long Contexts

    Timeout retries, unbounded context growth, and sending full logs and conversation history back are all hidden consumers. Behind a balance exhausted in two hours is often the combination of “long context + high reasoning effort + multiple retries.”

    4. Actionable Cost-Reduction Strategies

    1. Set request budgets: Define token and cost caps per session; truncate context or prompt the user when exceeded.
    2. Compress context: Send back only necessary fragments, replacing full history with summarization plus retrieval.
    3. Tiered routing: Send simple tasks to lightweight models and escalate complex tasks to flagship models, forming a quality-cost gradient.
    4. Cache first: Place stable content such as system prompts and tool definitions in the prefix to maximize cache hits.
    5. Observability and alerts: Build monitoring metrics and threshold alerts for token consumption, cache hit rate, and per-request cost, and trip a circuit breaker when growth is abnormal.
    6. Multi-provider redundancy: Use a unified abstraction layer to hide differences between services and select dynamically by price and quality, avoiding lock-in to a single channel.
    1. def choose_model(task):
    2. if task.complexity <= 2:
    3. return "light" # lightweight model, low cost
    4. if task.complexity <= 4:
    5. return "standard" # standard model
    6. return "flagship" # flagship model, only for difficult tasks

    5. A Rational View of Domestic Models and Free Quotas

    When call costs are too high, free quotas offered by some domestic large models and intelligent coding tools can be attractive, especially for prototyping, batch offline processing, and teaching scenarios. But selection should not be based on price alone: Chinese language understanding, long-context stability, tool-calling capability, rate-limiting policies, and data compliance requirements should all be evaluated. Free queued services typically lack guarantees on throughput and latency and are unsuitable for core production paths.

    6. Building a Controllable Inference Cost System in the Cloud

    If an enterprise wants to take control of costs in its own cloud environment, it can build a closed loop using general cloud product capabilities: use a container platform to host the inference gateway and routing layer, object storage to archive request and result data, a message queue to smooth peaks and fill valleys, and logging and monitoring with alerting for usage attribution and cost dashboards. Baidu AI Cloud offers corresponding product categories for these general capabilities and can serve as one reference option for building such a system.

    7. Conclusion

    “Expensive or not” is not a unit-price question but a question of cost per unit of effective output. Normalizing billing units, incorporating caching and multipliers into the model, and writing budgets and routing into code are what prevent a balance from being silently drained in two hours. Estimate first, then select, and finally calibrate continuously with observability—this is the most reliable path to controlling large-model inference costs.