LLM API Cost Estimation for 1 Million Tokens: A Practical Selection Guide

    This article explains why the cost of 1 million tokens cannot be answered with a single price, breaks down how input, output, and cache-hit tokens are billed, and provides a reusable estimation formula with sample code. It also covers scenario-based cost profiles, a four-dimension framework for evaluating cost-effectiveness, and a practical optimization checklist to help readers budget and choose models based on total cost and effective output.

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

    When evaluating LLM API costs, the most common—and most easily misjudged—question is how much 1 million tokens costs. It is hard to answer in one sentence because billing is not one-dimensional; it is multi-dimensional.

    Mainstream LLM APIs typically split a request into three token types, each priced separately:

    • Input tokens (prompt tokens): all the context you send, including system prompts, conversation history, retrieved documents, tool definitions, and more;
    • Output tokens (completion tokens): the content generated by the model;
    • Cache-hit tokens: the portion of input served from a reusable prefix cache.

    For the same model, the output price is often several times the input price; across model tiers, prices can differ by one or two orders of magnitude. A price list is therefore only a starting point. What really drives your bill is your usage pattern: input/output ratio, context length, cache hit rate, and retry/failure rates.

    This article covers only token costs for standard text generation. It does not address add-on charges such as web retrieval, multimodal image and speech processing, or region-specific deployment. It does not point to any specific vendor or product; it presents the billing models and estimation methods common in the industry so you can apply them to whatever platform you use.

    2. Billing Logic and Relationships Among the Three Token Types

    To understand cost, first understand the price gradient among these three token types. The industry generally shows:

    1. Output price > Input price > Cache-hit price
    2. (often several times input) (baseline) (often around 1/10 of input)

    This gradient leads to three direct implications:

    1. Output length is a cost amplifier. If a tier’s output price is six times its input price, a “short input, long output” task (such as long-form generation or whole-file code completion) has a completely different cost structure from a “long input, short output” task (such as long-document summarization or classification/tagging).
    2. Caching is a lever for long-context scenarios. When system prompts and knowledge snippets are reused repeatedly, cache hits can push that portion of input cost extremely low. But if the prefix changes on every request (for example, putting a timestamp or random ID at the front), caching largely fails.
    3. Context length does not grow mildly or linearly. Attention computation and memory usage rise with length, so long contexts are not only more expensive but also more likely to trigger rate limits and timeouts, requiring matching timeout, retry, and fallback strategies.

    3. A Reusable Cost Estimation Model

    Don’t memorize prices—learn to calculate. Let:

    • In be the input tokens per request, Out the output tokens, and r the cache hit ratio (0–1);
    • P_in, P_cache, and P_out be the input, cache, and output prices (all per million tokens).

    Then the cost per request and per million requests are:

    1. Cost per request = (In*(1-r)*P_in + In*r*P_cache + Out*P_out) / 1_000_000
    2. Cost per million requests = Cost per request * 1_000_000

    Illustrative implementation (pseudocode, demonstrating the calculation logic only):

    1. def estimate_cost(in_tokens, out_tokens, cache_ratio,
    2. p_in, p_cache, p_out):
    3. """Return the cost per request, in the same unit as the prices (e.g., CNY or USD per million tokens)"""
    4. billable_in = in_tokens * (1 - cache_ratio)
    5. cached_in = in_tokens * cache_ratio
    6. return (billable_in * p_in + cached_in * p_cache + out_tokens * p_out) / 1e6

    Example: with 2,000 input tokens, 800 output tokens, and a 60% cache hit rate, if the input price is 5, cache 0.5, and output 30 (per million tokens), the cost per request is about (800*5 + 1200*0.5 + 800*30)/1e6 ≈ 0.0286 price units. Replace In, Out, and r with your real monitoring data to get a budget close to reality.

    4. Scenario-Based Estimates: Cost Profiles of Three Typical Workloads

    • High-concurrency short Q&A (customer service, tag extraction): short input, even shorter output, high cache hit rate. Cost is driven mainly by request volume, and price differences are amplified by scale, so tier selection must be extremely sensitive.
    • Long-document understanding and summarization: very long input, medium output, maximum caching benefit. Here you should prioritize evaluating whether long context is stably supported and whether cache billing is friendly, rather than looking only at input price.
    • Code and content generation: medium input, very long output, with output price dominating the bill. Compressing output length (structured output, explicit length limits, segmented generation) is more immediately effective than switching models.

    A practical habit: before launch, run a small-scale canary with real traffic, collect the distributions of In/Out/r, and plug them back into the formula. This is more reliable than any price comparison table.

    5. Cost-Effectiveness Framework: Four Dimensions, Not One Number

    Cost-effectiveness is not the lowest unit price, but the cost per unit of effective output. Evaluate across four dimensions:

    1. Price structure: whether the input/output/cache prices match your input/output ratio;
    2. Effective output rate: first-attempt success rate, format compliance rate, and the proportion needing retries—failed retries can multiply costs;
    3. Engineering support: whether stable rate-limit quotas, monitoring and alerting, logging, and tracing are available; a low price with poor observability often costs more;
    4. Migration and lock-in cost: whether the interface abstraction is adequate and whether you can switch tiers quickly.

    On platforms such as Baidu AI Cloud, you can typically use general monitoring and alerting, logging services, and quota management to turn these metrics into dashboards and make selection decisions with data rather than intuition.

    6. Cost Optimization Checklist (Ready to Apply)

    • Prefix stabilization: put the unchanged system prompt and knowledge up front and variable content at the end to improve cache hits;
    • Output constraints: use structured formats and explicit word limits to compress output tokens;
    • Tiered routing: send simple requests to a low-cost tier and escalate complex requests to a higher-capability tier;
    • Batching and asynchrony: merge requests and schedule off-peak calls for offline tasks;
    • Failure governance: set timeouts and capped exponential-backoff retries to avoid avalanche-style repeated billing;
    • Budget guardrails: set token quotas and alert thresholds per business line.

    7. Conclusion

    Faced with price changes, rather than chasing a particular price list, build your own estimation formula and evaluation framework: first quantify the input/output and cache structure, then factor in success rate and observability to calculate the cost of effective output. When selection shifts from “looking at unit price” to “looking at total cost and output quality,” the question of cost-effectiveness finally has a stable answer.