LLM API Cost Analysis and Model Selection: A Practical Guide

    This guide explains how to calculate the true cost of large language model APIs and choose the right model tier for each workload. It breaks down token pricing into regular input, cached input, and output; provides a cost estimation formula with a peak factor; compares flagship, balanced, and lightweight tiers; and offers a scenario-based selection matrix. It also lists six practical optimization techniques, common budgeting mistakes, and a pre-launch checklist for cost governance.

    1. Why the Lowest Unit Price Is Not the Best Value

    When evaluating LLM API cost efficiency, the most common mistake is comparing only the input price per million tokens. A real bill has three components: regular input, cached input, and output. Their unit prices are often on different scales, and the output price is typically several times the regular input price (commonly 3–6x in the industry). This means a model with cheap input can end up costing more overall if your workload generates long outputs.

    The correct definition of cost efficiency is therefore: the lowest cost per unit of effective output (not per token), provided a quality threshold is met. The quality threshold cannot be skipped—the hidden cost of a low-quality output that requires human rework is far higher than the token price difference.

    2. Three Key Dimensions of Token Pricing

    1. Regular input (prompt): Includes system prompts, conversation history, and document chunks concatenated via retrieval augmentation. A long system prompt is billed repeatedly on every request.
    2. Cached input: When a request prefix exactly matches a recent request, that portion can be billed at a significant discount (commonly around 1/10 of the original price). Whether a cache hit occurs depends on your call pattern, not model capability.
    3. Output (completion): The most expensive component, with length determined by the business use case. Output length can vary several times across summarization, code generation, and structured extraction.

    A general estimation formula:

    1. Cost per request = input tokens/1e6 * P_in
    2. + cached tokens/1e6 * P_cache
    3. + output tokens/1e6 * P_out
    4. Monthly cost = cost per request * QPS * 86400 * active days per month * peak factor

    The “peak factor” corrects for the average-value assumption and is typically 1.2–2.0. Ignoring it is the most common source of budget deviation.

    3. Price Structure Across Model Tiers (Illustrative)

    The table below illustrates the three-tier structure common in the industry to show the proportional relationships. It does not represent any specific vendor’s pricing:

    Tier Input (per million tokens) Cached input Output Typical positioning
    Flagship High ~1/10 of input ~6x input Complex reasoning, specialized tasks
    Balanced Medium ~1/10 of input ~6x input Trade-off between capability and cost
    Lightweight Low ~1/10 of input ~6x input High concurrency, cost-sensitive

    Key takeaway: the output-to-input ratio stays roughly the same across tiers, which means the main levers for cost optimization are output length and cache hit rate, not switching tiers.

    4. Scenario-Based Selection Matrix

    • High concurrency, short output (intent recognition, tag classification, initial content moderation): Choose the lightweight tier. Output is usually only tens of tokens, so cost is dominated by input. Compress prompts rather than upgrading the model.
    • Medium complexity, structured output (information extraction, ticket categorization, SQL generation): Choose the balanced tier. Focus on controlling redundant output formatting and avoid explanatory text from the model.
    • Long-chain reasoning, code, and professional writing: Choose the flagship tier, but pair it with segmentation and result caching; otherwise cost grows linearly with context.
    • Long-document Q&A: Rely on cached input discounts first, and place the invariant prefix (system prompt + document) at the very front of the request.

    5. Six Actionable Optimization Techniques

    1. Prefix stability design: Keep system prompts and tool definitions byte-for-byte unchanged, and put variables at the end to improve cache hit rate.
    2. Output length constraints: Use structured output and explicit length limits instead of “please be as detailed as possible.”
    3. Context trimming: Keep only the Top-K chunks in retrieval augmentation, with deduplication and reranking.
    4. Tiered routing: Use the lightweight tier to judge difficulty first, and escalate only hard samples to the flagship tier.
    5. Semantic caching: Cache final results for high-frequency repeated questions and return them directly on a hit.
    6. Observability: Instrument logs, monitoring, and alerting to record the three token types and attribute cost by business line.

    Illustrative routing logic:

    1. def route(prompt: str) -> str:
    2. # Use the lightweight tier to assess complexity first, so not every request goes to the expensive tier
    3. level = light_model.classify(prompt) # "simple" / "hard"
    4. return flagship_model if level == "hard" else light_model

    6. Common Mistakes and Checklist

    • Looking only at input unit price while ignoring the output multiplier;
    • Budgeting with average token counts without adding a peak factor;
    • Assuming cache hits happen automatically, when they actually depend on call order and prefix consistency;
    • Overlooking duplicate billing from failed retries;
    • No quotas or alerts in place, leaving no cost guardrails.

    Before launch, verify: whether the three token types are instrumented separately, whether monthly budget alerts are configured, whether a switch to downgrade to the lightweight tier exists, and whether there is a dashboard for cache hit rate.

    7. Conclusion

    Model cost efficiency is a dynamic metric that changes with your workload. The practical approach is to formalize the cost formula, structure the selection matrix, and turn optimization into a checklist—make decisions with data rather than a unit price table. If you need elastic compute along with supporting data, storage, and monitoring capabilities, you can build a unified model invocation and cost governance layer on Baidu AI Cloud following the framework above, balancing quality, latency, and budget.