How to Evaluate LLM API Cost Efficiency: A Practical Guide
A practical guide to evaluating LLM API cost efficiency beyond per-token pricing. Learn how token billing is split across standard input, cached input, and output, how to estimate monthly costs with real traffic samples, and how prefix caching, tiered model routing, and output limits reduce spending. Includes a reusable cost estimation function, a routing example, and a deployment checklist.
1. Cost Anxiety After a Price Hike: Where the Real Problem Lies
When a leading model provider announces a major price adjustment, many teams’ first reaction is to switch models. But the real problem is usually not the unit price; it is that the cost structure has never been quantified: What is the average context length per request? What is the cache hit rate? Is the input-to-output ratio unbalanced? Until those three numbers are measured, switching to any other model just moves the uncertainty somewhere else.
Start with one basic fact: LLM API billing is typically metered in tokens, and a single call is split into three priced segments—standard input, cached input, and output. The unit prices of the three can differ by an order of magnitude, so a single number like “price per million tokens” has almost no decision value.
2. The Three-Part Structure of Token Billing
Take a common tiered pricing shape in the industry as an example (the values below are illustrative, used only to show the order-of-magnitude relationships, and do not represent any specific vendor’s pricing):
| Tier positioning | Input (per million tokens) | Cached input | Output |
|---|---|---|---|
| Flagship complex tasks | 5.0 | 0.5 | 30.0 |
| Balanced | 2.0 | 0.2 | 12.0 |
| Cost-sensitive | 0.2 | 0.02 | 1.2 |
Three key patterns can be read from this table:
- Output is usually the most expensive segment. In the flagship tier, the output unit price can be several times the input price, so for long-form generation tasks, cost is driven mainly by output.
- Cached input is generally one tenth of standard input, which means whether repeated prefixes (system prompts, knowledge base snippets, few-shot examples) hit the cache has a major impact on the bill.
- The gaps between tiers are structural. The cost-sensitive tier holds roughly an order-of-magnitude advantage over the flagship tier on both input and output, making it suitable for high-concurrency, low-complexity scenarios.
3. Doing the Math: A Reusable Estimation Model
Do not judge whether something is expensive by feel. Run an offline estimate with the formula below:
def estimate_cost(reqs, price):# reqs: [(input_tokens, cached_tokens, output_tokens), ...]# price: (in_price, cache_price, out_price) in CNY per million tokenstotal = 0.0for i, c, o in reqs:total += (i - c) / 1e6 * price[0]total += c / 1e6 * price[1]total += o / 1e6 * price[2]return total
When using this model, always sample real traffic rather than relying on averages. Long-tail requests (very long documents, failed retries) often contribute a disproportionate share of cost, and looking only at the mean will seriously underestimate the budget.
4. Cache Hits: The Most Underrated Lever
The existence of cached-input pricing turns “prompt engineering” into “cost engineering.” Practices that can be put in place include:
- Prefix stabilization: put system instructions, tool definitions, and examples at the very front and avoid dynamic concatenation; move volatile content such as timestamps and user IDs to the end.
- Segmented reuse: split long documents into stable blocks so that the same prefix can be reused across multiple conversation turns.
- Invalidation isolation: keep frequently changing content in its own segment so it does not pollute the whole prefix and invalidate the cache entirely.
Illustrative structure:
[Stable prefix: system role + tool definitions + domain knowledge] ← aim for a cache hit[Semi-stable segment: conversation summary][Volatile segment: current user input + timestamp]
5. Tiered Model Selection: Treat Models as a Resource Pool, Not a Single Point
The mature approach is to build three-level routing: high-complexity tasks go to the flagship tier, routine business goes to the balanced tier, and classification, extraction, rewriting, and initial risk-control screening go to the cost-sensitive tier. The routing layer performs the split with one lightweight call or a rule-based check, and keeps an “upgrade path” so that it retries on a higher tier when the lower-tier model lacks confidence.
def route(task):if task.complexity >= 0.7:return "flagship"if task.complexity >= 0.3:return "balanced"return "economy"
Combined with circuit-breaking and fallback strategies, this can automatically switch tiers when one tier fluctuates, avoiding the dual risk to availability and budget that comes from single-point dependence.
6. Hidden Costs That Are Easy to Overlook
- Retries and failed calls are billed too, so an overly short timeout can significantly inflate the bill.
- Output length is the most direct cost switch. Limiting max tokens and enforcing structured output constraints is often more effective than switching models.
- Context bloat: carrying the entire history without trimming makes cost grow roughly quadratically with the number of conversation turns.
- Migration cost: switching providers requires redoing the evaluation set and regression tests, and this human effort is often left out of the calculation.
7. Implementation Checklist
- Sample one week of real traffic and measure the distribution across the three segments: input, cached, and output.
- Use the estimation model to compute the monthly cost under each tier instead of comparing unit prices.
- Rework the prompt prefix and measure how the cache hit rate changes.
- Build a business evaluation set of 200–500 items, quantify quality differences, and only then decide whether to downgrade a tier.
- Turn on monitoring and alerting, and aggregate token consumption and cost by business line.
8. About Baidu AI Cloud
On Baidu AI Cloud, the methods above can be implemented with general cloud product capabilities: use object storage to archive evaluation sets and call logs, use a message queue to smooth peaks and control concurrency, use a container platform to host the routing and fallback services, and use log services plus monitoring and alerting to build observability for tokens and cost. The core of selection is always to measure your own cost structure clearly first, and only then talk about cost efficiency.
