LLM API Cost Calculation and Model Selection: A Practical Guide

    This guide explains why the lowest per-token price does not always produce the lowest bill, breaks down how normal input, cached input, and output tokens are charged, and shows how to build a request profile, estimate costs, and apply routing, caching, output constraints, and budget guardrails to control LLM API spending.

    1. Why the Lowest Unit Price Is Rarely the Lowest Bill

    When comparing LLM API value, the most common mistake is looking only at the input price per million tokens. A real bill is determined by three components: normal input tokens, cache-hit input tokens, and output tokens. Their unit prices are usually not on the same scale, and output is often several times more expensive than normal input. In the source material, one major overseas model family prices output at roughly 6x normal input, while another overseas vendor’s flagship tier follows a similar structure of about 5x output-to-input pricing.

    This means that for a request with 10,000 input tokens and 2,000 output tokens, output can account for more than half of the total cost. If the workload involves long-form generation, code completion, structured extraction, or multi-turn summarization, the output share grows further, and small differences on a price sheet are multiplied across the monthly bill.

    So the first step is not to compare unit prices, but to build your own request profile:

    1. Request profile = (average input length, average output length, cache reuse ratio, daily call volume, peak QPS)

    Only after quantifying these five variables does a price table become meaningful.

    2. Billing Logic and Engineering Implications of the Three Token Types

    Normal Input Tokens

    Billed by raw text length, this is the most straightforward component. System prompts, retrieved document fragments, and conversation history all count. You can control it by tightening prompts and trimming irrelevant context.

    Cache-Hit Input Tokens

    Most major platforms offer context caching: when a request prefix exactly matches a previous request, that portion is billed at a significant discount (shown in the material as roughly one-tenth of the input price). The engineering implication is to place stable content at the front of the prompt and variable content at the end to improve the hit rate.

    Illustrative structure:

    1. [Fixed] System role + tool descriptions + output format constraints + a few examples
    2. [Semi-fixed] Domain knowledge, glossary
    3. [Variable] User question, retrieved fragments, timestamps

    Output Tokens

    The most expensive component, and directly affected by how verbose the model is. Control methods include explicit length and structure limits, JSON Schema constraints, a maximum output length, and avoiding prompts that make the model repeat the input.

    3. Price Tiers and Scenario Matching

    The overseas price structure in the material can be abstracted into three tiers:

    Tier Input price Cache hit Output price Suitable scenarios
    Flagship Highest 1/10 of input 5–6x input Complex reasoning, professional writing, difficult code
    Balanced Medium 1/10 of input 5–6x input Routine business, batch processing
    Lightweight Lowest 1/10 of input 6x input High concurrency, classification, extraction, routing

    Key takeaway: the output-to-input multiplier is similar across all three tiers, indicating that vendors’ pricing structures are converging and the differences are mainly in absolute unit prices. Therefore, the core of cost reduction is not switching to a cheaper tier, but reducing output volume, improving cache hits, and routing simple requests to lower-tier models.

    4. A Practical Cost Estimation Method

    Use a minimal estimation script to turn the bill from a guess into a calculation:

    1. def estimate(in_tok, cached_tok, out_tok, p_in, p_cache, p_out):
    2. return (in_tok - cached_tok) / 1e6 * p_in \
    3. + cached_tok / 1e6 * p_cache \
    4. + out_tok / 1e6 * p_out
    5. # Example: flagship tier, 10k input, 8k cache hit, 2k output
    6. cost = estimate(10000, 8000, 2000, 5.0, 0.5, 30.0)
    7. print(f"Cost per call: ${cost:.5f}")

    Multiply by daily call volume, then by 30, to get a monthly baseline. Then do three things: reduce output length, raise the cache hit rate, and route more than 30% of simple requests to lower-tier models. This typically yields a meaningful cost reduction.

    5. Four Architecture-Level Optimization Paths

    1. Tiered routing: use a lightweight model first for intent recognition and difficulty assessment, then escalate hard cases to the flagship tier, forming a funnel-style call pattern.
    2. Prefix caching: place fixed content first and use the platform’s caching capability to reduce repeated context costs to one-tenth.
    3. Output constraints: compress output tokens with structured output, length limits, and field whitelists.
    4. Budget guardrails: set per-tenant daily token quotas and alert thresholds at the gateway layer, with automatic degradation or queuing for abnormal calls.

    Observability is equally important: instrument normal input, cache-hit, and output tokens separately, and connect them to logging and monitoring alerts so you can identify which request types are consuming the budget.

    6. Selection Checklist

    • Does it support context caching, and what is the discount ratio?
    • Is the output-to-input price ratio transparent?
    • Does it provide structured output and length-control parameters?
    • Are quotas, rate limits, and usage alerts available?
    • Can a unified gateway route across multiple models to avoid vendor lock-in?

    7. Conclusion

    Price changes are the norm; methodology is the long-term asset.固化 the cycle of request profile, three-token estimation, tiered routing, and budget guardrails into engineering standards, and you can quickly find the best value regardless of upstream price changes. If an enterprise wants to build a unified LLM gateway and usage governance on Baidu AI Cloud, it can build one on general-purpose container platforms, message queues, logging services, and monitoring alerts while keeping the architecture neutral and portable.