Large Language Model API Cost Analysis and Model Selection: A Practical Guide
Choosing the cheapest LLM API per million tokens rarely produces the cheapest bill. This guide breaks down how input, cached input, and output pricing combine, shows how to model costs with a formula, lists six cost-optimization levers, and introduces a four-dimension scoring framework for model selection. It also includes a worked example comparing flagship, balanced, and lightweight tiers, plus a recommended cloud architecture for cost governance.
1. Why the Lowest Unit Price Rarely Means the Lowest Bill
When evaluating the cost-effectiveness of a large language model API, the most common mistake is comparing only the input price per million tokens. A real bill is determined by three components working together: regular input tokens, cache-hit input tokens, and output tokens. Their unit prices typically differ by an order of magnitude, and the shape of your calls determines the mix among the three.
Publicly available pricing structures from major providers show a consistent pattern:
- The output price is usually several times the regular input price (commonly 3–6x).
- The cache-hit input price is usually around one-tenth of the regular input price.
- Within the same model family, tiers are split by capability into “flagship / balanced / lightweight,” with prices stepping down accordingly.
The implication: a task with very long input and very short output (such as long-document extraction or code base retrieval) is dominated almost entirely by input cost, while a task with short input and long output (such as long-form generation or extended complex reasoning) gets amplified by the output price. Ignore this, and your selection conclusions will be systematically skewed.
2. Turning the Billing Model into a Computable Formula
Let Pin be the regular input price, Pcache the cache-hit price, and Pout the output price, with corresponding token counts Nin, Ncache, and Nout. The cost of a single call is:
Cost = Nin * Pin + Ncache * Pcache + Nout * Pout
What is more useful in engineering is an estimate of “cost per thousand requests”:
def estimate_cost(nin, ncache, nout, pin, pcache, pout):"""Return the cost of a single call, in the same unit as the prices (e.g. USD per million tokens)"""return (nin * pin + ncache * pcache + nout * pout) / 1_000_000# Scenario A: long input, short output (document extraction)cost_a = estimate_cost(8000, 0, 300, 5.0, 0.5, 30.0)# Scenario B: short input, long output (content generation)cost_b = estimate_cost(500, 0, 2000, 5.0, 0.5, 30.0)print(cost_a, cost_b)
Plugging your real business distribution into the formula is more meaningful than reading a price sheet. The practical approach: first collect the median and P95 of Nin/Ncache/Nout under low traffic, then back-calculate the cost, rather than estimating from averages—long-tail requests are often the main source of cost.
3. Six Levers for Cost Optimization
Input side: slim down the context. Remove duplicated system prompts, replace large reference passages with retrieved snippets, and summarize and compress conversation history. Reductions in input tokens improve the bill linearly and predictably.
Cache side: put the stable prefix first. Place system instructions, tool definitions, and few-shot examples at the very front of the prompt and keep them byte-for-byte stable so the prefix can be cached. Note: once you insert volatile content such as timestamps or random IDs ahead of the stable prefix, the entire cache is invalidated.
Output side: constrain generation length. Set an explicit maximum output length, require short structured output, and avoid redundant phrasing that “thinks at length before giving the conclusion.” Since the output price is the highest, every 100 tokens saved here yields the biggest gain.
Routing side: tiered dispatch. Use lightweight-tier models for deterministic tasks such as classification, extraction, and format conversion, and reserve complex reasoning and long-chain planning for the flagship tier. In engineering, a small model can produce a result along with a confidence score first, and low-confidence cases are escalated to the large model, forming a cascade:
Request -> Lightweight model -> Confidence evaluation|-> High: return directly|-> Low: escalate to flagship model
Interaction side: streaming and batching. Streaming output improves perceived experience but does not lower the unit price; real savings come from asynchronous batch endpoints (usually discounted) and merged requests, which reduce repeated billing caused by duplicated prefixes.
Observability side: build cost observability. At the gateway layer, log the three token counts, the model tier, and whether the cache was hit for every call, then build a cost dashboard sliced by business line or tenant and configure budget alerts. Without fine-grained instrumentation, there is nothing to optimize from.
4. A Selection Framework: Four-Dimension Scoring
Score candidate models in a unified matrix (1–5 points each), with weights adjusted to the business:
| Dimension | Key question | Suggested weight |
|---|---|---|
| Cost | Cost per thousand requests, back-calculated from the real token distribution | 30% |
| Quality | Task pass rate on your own evaluation set | 35% |
| Latency | P50/P95 time to first token and overall latency | 20% |
| Engineering fit | Structured output reliability, rate-limit quotas, observability | 15% |
The “own evaluation set” cannot be skipped. Public leaderboards differ enormously from your business distribution; only a pass rate measured on real samples (200 or more recommended, covering the long tail) can be compared with cost in the same coordinate system.
5. Worked Example: Cost Structure Across Three Model Tiers
Assume a platform offers three tiers in the same model family with a typical stepped price structure (the numbers illustrate the method only and are not quotes for any specific product):
| Tier | Input | Cache hit | Output | Output/input ratio |
|---|---|---|---|---|
| Flagship | 5.00 | 0.50 | 30.00 | 6x |
| Balanced | 2.00 | 0.20 | 12.00 | 6x |
| Lightweight | 0.20 | 0.02 | 1.20 | 6x |
If a business consumes 800,000 input tokens and 200,000 output tokens per thousand requests, the cost for each tier is roughly:
Flagship: 0.8*5.00 + 0.2*30.00 = 10.00Balanced: 0.8*2.00 + 0.2*12.00 = 4.00Lightweight: 0.8*0.20 + 0.2*1.20 = 0.40
The conclusion is clear: when output accounts for 20% of tokens, the output term contributes about 60% of the flagship tier’s cost. If you then constrain output length and raise the cache hit rate so that output drops to 100,000 tokens and the cache hit rate reaches 50%, the flagship cost falls to about 0.4*5.0 + 0.4*0.5 + 0.1*30 = 5.2—nearly a 50% reduction with no compromise in model capability.
6. A Recommended Architecture on the Cloud
- Ingress layer: a unified model gateway that abstracts away differences between tiers and supports canary releases and fast rollback.
- Cache layer: a caching service that stores retrieval results and intermediate artifacts to reduce repeated input.
- Async layer: a message queue to smooth peaks and route deferrable tasks into the batch channel.
- Storage layer: object storage for long documents and call samples, feeding evaluation-set iteration.
- Observability layer: a logging service plus monitoring and alerting that connect token metering with budget thresholds.
Baidu AI Cloud can provide general-purpose capabilities for this kind of architecture, including a container platform, object storage, a message queue, a logging service, and monitoring and alerting. Combined with a unified way of onboarding model services, this makes it easier for teams to turn the cost-governance practices above into platform-level standards rather than relying on individual experience.
7. Conclusion
Cost-effectiveness is not a single number on a price sheet; it is the product of “real token distribution × model tier × engineering optimization.” Measure first, model second, optimize third, and make decisions with the four-dimension scoring method on your own evaluation set—only then will large language model API spending stay under control as the business grows.
