LLM API Cost Estimation and Model Selection: A Practical Guide

    This guide explains how to estimate large language model API costs and choose the right model tier. It breaks down the three token types—regular input, output, and cached input—and shows how input/output ratio, cache hit rate, and context length affect total spending. You will find a reusable cost formula, a sample calculation, scenario-based tier selection advice, six practical optimization techniques, and common budgeting mistakes to avoid.

    1. Why “How Much Does 1 Million Tokens Cost?” Has No Single Answer

    When evaluating LLM API costs, the most common—and most misleading—question is how much one million tokens costs. The question is hard to answer because it compresses a multi-dimensional pricing problem into a single number. Mainstream LLM APIs typically bill three token categories separately: regular input tokens, output tokens, and input tokens that hit the cache. Their unit prices often differ by an order of magnitude: cached input is usually much cheaper than regular input, while output is often several times the price of regular input.

    So any unit price quoted without three premises—the input/output ratio, the cache hit rate, and the context length range—has almost no decision-making value. A more rigorous approach is to define the pricing dimensions first, plug in real workload characteristics, and then convert the result into per-request and monthly total costs.

    Scope boundary: this article covers only standard text generation token costs. It excludes possible extra charges for add-on capabilities such as web retrieval, image and speech multimodal processing, and region-specific deployment, and it does not address operational constraints such as rate limiting and concurrency quotas.

    2. Pricing Logic and Cost Implications of the Three Token Types

    1. Regular Input Tokens: Linear in Context Length

    The regular input price applies to the entire prompt that does not hit the cache. Its cost is determined directly by context length, including the system prompt, conversation history, document fragments stitched in through retrieval augmentation, tool definitions, and tool return results. Many teams underestimate this part because they count only the words in the user’s question and ignore the context that keeps expanding on the engineering side for the sake of quality.

    2. Output Tokens: Highest Unit Price and Highly Variable Length

    Output is usually several times the regular input price. With a pricing structure common in the industry, an output unit price six times the input unit price is not unusual. This means a classification task with “long input, short output” and a generation task with “short input, long output” can differ severalfold in cost even when the total token count is the same. The first priority in cost optimization is often not compressing the prompt but constraining output length.

    3. Cached Input Tokens: A Discount for Stability

    When multiple requests share an identical prompt prefix, the server can reuse the already computed intermediate results, and these tokens are billed at a significantly lower price. The engineering implication is that putting stable, unchanging content first and dynamic content last is an almost zero-cost optimization. Note, however, that caching usually requires the prefix to match exactly, and a tiny change anywhere can break the prefix match.

    3. A General Cost Estimation Model

    Let a single request have in regular input tokens, out output tokens, and hit input tokens that hit the cache, with unit prices p_in, p_out, and p_hit. Per-call and monthly costs can be written as:

    1. cost_per_call = in * p_in + out * p_out + hit * p_hit
    2. monthly_cost = cost_per_call * QPS * 86400 * 30 * utilization

    Illustrative implementation (unit prices must be taken from the official documentation; this only demonstrates the calculation structure):

    1. def estimate(in_tok, out_tok, hit_tok, p_in, p_out, p_hit, calls_per_month):
    2. per_call = in_tok * p_in + out_tok * p_out + hit_tok * p_hit
    3. return per_call, per_call * calls_per_month
    4. # Convert per-million-token prices to per-token prices
    5. M = 1_000_000
    6. print(estimate(8000, 500, 6000,
    7. 2.0 / M, 12.0 / M, 0.2 / M,
    8. 3_000_000))

    The value of this model is that it moves the discussion from “unit price comparison” back to “workload profiling”: only when the business workload is fixed does the cost ranking between different models become meaningful.

    4. Matching Model Tiers to Scenarios

    Mainstream vendors generally use a tiered product structure, which can be abstracted into three tiers:

    • Flagship tier: for complex reasoning, long-chain planning, difficult coding, and specialized domain tasks; highest unit price.
    • Balanced tier: a compromise between capability and cost, suitable for most routine business and the default first choice.
    • Lightweight tier: for high-concurrency, low-complexity, cost-sensitive scenarios such as intent recognition, format extraction, and batch labeling.

    The selection advice follows the order “evaluate on a small sample first, then pick a tier”: run real business samples through all three tiers and compare whether the quality difference is large enough to justify the cost difference. In practice, a large share of requests actually falls within the capability range of the lightweight and balanced tiers, and putting all traffic on the flagship tier is a common waste of budget.

    5. Six Actionable Optimization Techniques

    1. Output constraints: explicitly limit word count, structured fields, and enumeration ranges to avoid output bloat from the model improvising.
    2. Prefix stabilization: put the system prompt, tool definitions, and examples at the front, and dynamic fields such as timestamps and user identifiers at the end, to improve the cache hit probability.
    3. Context trimming: rerank and truncate retrieval results, keeping only fragments strongly relevant to the question.
    4. Tiered routing: run a lightweight classification first, then route by difficulty to different model tiers.
    5. Batching and asynchrony: merge requests for offline tasks and run them off-peak to reduce peak spending.
    6. Observability: use logging services and monitoring/alerting to record the actual consumption of each token type, attribute it by business dimension, and form a continuous optimization loop.

    6. Common Pitfalls

    • Comparing only the input unit price: ignoring the output multiple leads to severely skewed budgets.
    • Ignoring the caching premise: assuming all requests hit the cache, when even a slight prefix change invalidates it.
    • Estimating peaks with average tokens: long-tail requests can significantly raise actual spending.
    • Overlooking retries and failed requests: timeout retries also consume input tokens and must be included as budget headroom.

    7. Conclusion

    Cost control for LLM APIs is essentially turning a “unit price problem” into a “workload engineering problem.” First establish a three-dimensional measurement standard, then estimate with a unified formula, and finally keep optimizing through output constraints, prefix stability, tiered routing, and observability, so that a stable balance between quality and budget can be reached. If you need elastic compute and a full set of supporting capabilities such as monitoring, logging, and storage, you can build a unified invocation and observability foundation on top of the relevant general cloud products from Baidu AI Cloud.