How to Calculate LLM API Costs: A Fair Comparison of Pricing Across Providers
A reliable comparison of large language model API costs requires more than looking at input or output prices in isolation. This guide explains how to standardize pricing units, account for input/output ratios and cache hits, align currencies and model versions, estimate per-task costs, verify pricing data, and evaluate practical factors beyond the lowest listed price.
1. Why List Prices Cannot Be Compared Directly
Public pricing pages typically list separate rates for input tokens, output tokens, and cached inputs, usually expressed as a price per million tokens. Looking at only one of these figures can lead to misleading conclusions for three main reasons.
First, input and output rates can differ substantially. Output tokens are often much more expensive than input tokens, so considering only the input price can significantly underestimate the cost of tasks that generate a large amount of content.
Second, models with the same name may refer to different versions or context-window configurations. Their prices may therefore not represent the same technical setup.
Third, providers may use different currencies, tax rules, minimum spending requirements, and credit or top-up policies. Prices become meaningful only after they have been converted to a consistent basis.
Before comparing providers, standardize the pricing basis and calculate the blended cost.
2. Standardize Pricing Fields First
Token-based services generally include three types of prices:
| Field | Meaning | Considerations |
|---|---|---|
| Input price | The cost of prompts, context, and tool results sent to the model | In multi-turn conversations, historical content may be counted repeatedly |
| Output price | The cost of content generated by the model | This is usually higher than the input price, and reasoning-oriented tasks may generate longer outputs |
| Cache-hit price | The input cost when a repeated prefix is served from cache | It applies only when a cache hit occurs, so the actual hit rate must be measured from logs |
If a provider does not offer a separate cache price, calculate cached tokens at the standard input rate.
3. Apply Weights Based on the Business Scenario
Actual cost depends on the ratio of input to output tokens. Conversational applications typically use more input tokens, while code-generation and agent-based workloads may produce a larger share of output tokens.
A common approach is to assign scenario-specific weights—for example, 30% input and 70% output—and calculate the blended price:
Blended price = input share × input price + output share × output price
The weights should come from your own usage logs rather than assumptions. If the actual input/output distribution differs significantly from the estimate, update the calculation.
4. Normalize Currencies and Exchange Rates
International providers often bill in U.S. dollars, while other providers may use different local currencies. Before comparing prices, convert them to a single currency and document the exchange rate and date used.
Exchange-rate movements can change the result, so retain the conversion basis for future review.
5. Align Model Versions
Before comparing two prices, verify three points:
- Are the model version numbers the same?
- Do the models have the same context-window size?
- Do they represent the same capability tier, such as a flagship model versus a lightweight model?
If a provider offers only a previous-generation version, mark it separately instead of comparing it directly with a newer model. If no matching version can be found, leave the entry blank and explain why rather than filling it with approximate data.
6. Common Integration Options
Large language model APIs are commonly accessed through several types of channels:
- Direct access from the model provider: Pricing is usually transparent, but availability may be subject to regional restrictions or quota limits.
- Cloud platforms: These services often provide enterprise-oriented authentication, auditing, and billing management.
- Aggregated routing services: They unify the interfaces of multiple models, making it easier to switch between providers.
- Self-hosted gateways: Internal gateways can centralize rate limiting, caching, and cost tracking.
Pricing does not necessarily follow the type of channel. The same model may have different prices across providers because of promotions, volume discounts, or settlement terms. Each provider must therefore be checked individually.
7. Cost Estimation Example
The following code calculates the cost of a single task and compares the blended price of two channels. The figures are for demonstration only and do not represent the pricing of any real platform.
from dataclasses import dataclassfrom typing import Optional@dataclassclass PriceCard:channel: strinput_per_m: float # CNY per million input tokensoutput_per_m: float # CNY per million output tokenscache_hit_per_m: Optional[float] = Nonedef task_cost(card: PriceCard, in_tokens: int, out_tokens: int,cache_ratio: float = 0.0) -> float:"""Calculate the cost of one task in CNY.cache_ratio is the share of input tokens served from cache."""cache_price = card.cache_hit_per_m if card.cache_hit_per_m is not None else card.input_per_mhit = in_tokens * cache_ratiomiss = in_tokens - hitcost = (miss * card.input_per_m + hit * cache_price + out_tokens * card.output_per_m) / 1_000_000return costdef blended_price(card: PriceCard, in_share: float = 0.3) -> float:"""Calculate the blended price in CNY per million tokens."""return in_share * card.input_per_m + (1 - in_share) * card.output_per_mif __name__ == "__main__":a = PriceCard("Channel A", input_per_m=2.0, output_per_m=8.0)b = PriceCard("Channel B", input_per_m=4.0, output_per_m=6.0)for card in (a, b):print(card.channel, round(blended_price(card), 2))# Channel A 6.2, Channel B 5.4:# the channel with the higher input price is cheaper overall
The example shows that a provider with a higher input price may still have a lower overall blended cost if its output price is lower. Conclusions based on a single pricing field are therefore unreliable.
8. How to Verify Pricing Data
Cost data should be reproducible and auditable. Use the following process:
- Record prices from each provider’s public pricing page, along with the page address and retrieval date.
- Save screenshots of the relevant pages so that the original information can be reviewed after the pages are updated.
- For dynamically rendered pages that cannot be retrieved directly, document the source and mark the data as pending verification. Do not fill in missing prices based on assumptions.
- List prices for unmatched model versions separately and exclude them from rankings.
- Schedule periodic reviews in the project. Pricing can change frequently, and outdated data may lead to poor decisions.
9. Practical Recommendations for Provider Selection
Price is only one factor in provider selection. Engineering teams should also consider the following:
- Quality and task fit: Differences in task performance between models may matter far more than differences in token prices. Evaluate models using representative real-world samples.
- Reliability and quotas: Rate limits and timeouts during peak periods can increase retry costs.
- Caching strategy: Placing stable system prompts and tool descriptions in the request prefix can improve cache-hit rates.
- Output control: Limit output length and use structured output to reduce unnecessary generation.
- Portability: Abstract the API layer so that the application can switch between providers, reducing dependence on a single vendor.
The goal of cost analysis is to determine the true cost per task, not simply to find the lowest listed unit price. Standardizing the calculation basis, preserving supporting evidence, and reviewing prices regularly will produce provider-selection conclusions that are easier to validate and maintain.
