LLM API Pricing Logic and Cost Optimization Practices

    This article explains why comparing per-million-token prices alone is misleading and introduces total cost of ownership (TCO) as the right framework for evaluating LLM API costs. It outlines three structural pricing trends, six engineering techniques for reducing spend, a four-step model selection methodology, and common anti-patterns to avoid.

    1. From Unit Price to Total Cost of Ownership

    When evaluating whether an LLM API is “cheap,” the most common mistake is comparing only the listed price per million tokens. The real bill is determined by several variables: the price gap between input and output, cache hit rate, context length distribution, retries and failed requests, the extra overhead of structured output, and whether the task requires stronger reasoning. The right frame of reference is therefore total cost of ownership (TCO), not a price sheet.

    A practical abstract billing model looks like this:

    1. Cost per request =
    2. input tokens × input price × (1 - cache hit rate × cache discount)
    3. + output tokens × output price
    4. + redundant token cost from failed retries

    This formula shows that when output is much longer than input (long-form generation, code generation), the output price dominates cost. When input is much longer than output (long-document QA, retrieval augmentation, codebase understanding), the input price and caching strategy dominate.

    From publicly visible pricing patterns in the industry, three trends can be abstracted, and they are more useful than any single number.

    First, model tiering has become mainstream. A single generation of capability is split into flagship, balanced, and lightweight tiers, with prices stepping down accordingly. The engineering implication is that a routing layer must exist to split traffic by task difficulty, rather than sending everything to the strongest model.

    Second, asymmetric input/output pricing has become entrenched. Output prices are generally several times the input price, which directly makes architectures that “make the model write less and cite more” cheaper.

    Third, cache hit discounts have become a standard capability. Some offerings give a significant discount on cached input (for example, as low as roughly one tenth of the original price), which matters greatly for multi-turn conversations, fixed system prompts, and repeated questions over long documents.

    It is worth stressing that the price gap between tiers is often several times, while the capability gap is not linear across most business tasks. This means “default to the strongest model” is usually uneconomical in engineering terms.

    3. Six Engineering Techniques for Cost Optimization

    3.1 Stabilize the Prompt Prefix and Design for Caching

    Put invariant content (system instructions, tool definitions, domain notes, examples) at the very front of the prompt, and volatile content (user input, timestamps, retrieved snippets) at the end. The more stable the prefix, the higher the cache hit rate. A counter-example is putting a random ID or the current time at the beginning, which invalidates the entire prefix.

    1. [Stable prefix] System role + tool schema + a few examples
    2. [Semi-stable section] Domain knowledge, long documents
    3. [Volatile tail] The current user question

    3.2 Compress Context and Replace Long Context with Retrieval

    Change “stuff everything in” to “retrieve, then stuff in.” Through chunking, vector retrieval, and reranking, send only the most relevant snippets to the model. Reducing context from tens of thousands of tokens to a few thousand can cut cost by an order of magnitude, while also reducing quality degradation from attention dilution.

    3.3 Constrain Output

    Use structured output, field whitelists, maximum length limits, and summarize-then-generate approaches to compress output tokens. Since output prices are higher, this item usually delivers the most direct savings.

    3.4 Routing and Cascades

    Use a lightweight model first for classification, extraction, and rewriting; escalate to the flagship model only when confidence is low or the task is complex. Practical signals include: failed rule validation, failed schema validation, confidence thresholds, and inconsistent answer consistency spot checks.

    1. def route(task, draft, validator):
    2. if validator(draft): # lightweight model output is usable
    3. return draft
    4. return strong_model(task) # escalate to the flagship model

    3.5 Batching and Asynchrony

    For offline tasks (log summarization, batch labeling, offline summarization), use asynchronous batch endpoints, which are usually more cost-effective than real-time endpoints and can significantly improve throughput.

    3.6 Close the Observability Loop

    You must collect input tokens, output tokens, cache hit status, retry counts, and cost per request for every request, aggregated by business dimension. Without this data, any optimization is guesswork. At minimum, build three dashboards: unit business cost, cache hit rate, and the share of calls per model tier.

    4. A Four-Step Selection Methodology

    Step one, build an evaluation set. Construct 100 to 500 cases from real business samples (not public leaderboards), annotating expected outputs and hard constraints.

    Step two, map capability tiers. Group cases into three difficulty levels, observe the pass-rate curves for the lightweight, balanced, and flagship tiers, and find the “cost-performance inflection point.”

    Step three, calculate TCO. Using the formula from Section 1, plug in the real input/output length distribution and expected cache hit rate to compute the cost range per thousand business calls.

    Step four, canary and regression. Start with a small traffic canary, monitor quality and cost metrics, then ramp up gradually; run the regression set on every prompt or model change to avoid cost reductions accompanied by quality collapse.

    5. Common Anti-Patterns

    One is repeatedly passing entire long documents without caching, so cost grows linearly with turns. Two is embedding random content in prompts, permanently disabling caching. Three is using the flagship model for all simple classification. Four is looking only at unit price and ignoring the output length share. Five is lacking a retry budget, causing sudden bill spikes during anomalies. Six is an evaluation set disconnected from the real distribution, making selection conclusions non-reproducible.

    6. Conclusion

    The absolute level of API prices cannot answer “is it worth it” on its own. What truly determines cost is architecture: tiered routing, prefix stability and caching, retrieval instead of long context, output constraints, batching, and observability. Get these things solid, and unit business cost stays controllable and predictable even in a market with frequent price changes. In related engineering practice, Baidu AI Cloud likewise recommends replacing the unit-price view with a TCO view, and using data-driven evaluation and canary mechanisms to complete model selection and cost governance.