LLM Cost Calculation Methods: Weighted Input and Output Pricing
This article explains how to calculate and compare large language model (LLM) API costs across different providers. It introduces a weighted unit price formula based on input and output token consumption ratios, walks through a step-by-step pricing verification process, highlights common comparison pitfalls (such as mixing model versions or ignoring cache discounts), and provides a Python code example for cost estimation. The goal is to help engineering and operations teams make data-driven decisions when choosing LLM channels or optimizing existing usage.
Why LLM API Costs Are Hard to Compare Directly
LLM APIs typically charge per token, with separate pricing for input and output tokens. Output token prices are generally higher than input token prices. The total cost is therefore determined by two factors: the unit prices themselves, and the ratio of input to output consumption in your specific workload. The same model may also carry different list prices across different channels due to service tiers, regional nodes, billing methods, volume discounts, and caching policies.
Two common misconceptions are worth addressing. First, some assume that official direct access is always the cheapest, or that third-party channels are necessarily more expensive—both assumptions need to be validated with public pricing data. Second, comparing different model versions or tiers as if they were the same product yields meaningless results, and such comparisons should be avoided when the versions differ.
Establishing a Unified Cost Calculation Standard
Weighted Unit Price Formula
Let:
- P_in = input unit price
- P_out = output unit price
- r = proportion of input tokens to total token consumption
The weighted unit price is:
weighted price = r × P_in + (1 − r) × P_out
The value of r depends on the use case. For question-answering applications with long input contexts and short outputs, r may exceed 0.7. For coding agents that read large amounts of context and continuously generate code across multiple calls, the output ratio tends to be higher. The actual ratio should be derived from call logs rather than assumed from experience alone. The examples in this article use a 30% input / 70% output ratio.
Example Calculation
Assume a model has an input price of 2 CNY per million tokens and an output price of 8 CNY per million tokens (illustrative figures only, not actual quotes). With a 30% input / 70% output split:
weighted price = 0.3 × 2 + 0.7 × 8 = 6.2 CNY per million tokens
If caching is enabled, with a cache-hit price of 0.2 CNY per million tokens and a 60% cache hit rate, the effective input price becomes:
0.6 × 0.2 + 0.4 × 2 = 0.92 CNY per million tokens
The weighted price then drops to:
0.3 × 0.92 + 0.7 × 8 = 5.876 CNY per million tokens
This represents a reduction of approximately 5%. In scenarios with long contexts and frequently repeated prefixes, caching yields more significant savings, so the cache hit rate should be tracked as a core metric.
Standardizing Currency and Statistical Timeframes
Cross-region channels often price in different currencies. When converting, use the exchange rate from the same date and note both the date and the rate in your report. Pricing pages are subject to change at any time; record the page URL and the time of retrieval when verifying prices, so your conclusions can be reproduced.
Standard Procedure for Price Verification
- Define the comparison target: model name, version number, and context length tier must be identical across channels.
- Collect public pricing: obtain input, output, and cache prices from each channel’s official pricing page, and record the source.
- Handle missing fields: if a page does not list a field, leave it blank and note it as unavailable; do not fill in estimated values.
- Calculate the weighted unit price and convert all prices to the same currency.
- Recalculate with real traffic: substitute the actual input and output token counts from your logs into the formula to derive monthly or project-level total costs.
Some pricing pages are rendered by front-end scripts, meaning static scraping may not capture the full table. In such cases, manually verify the data or use a reliable alternative source, and note any source discrepancies in your conclusions.
Common Pitfalls
Mixing Flagship and Lightweight Versions
Flagship and lightweight versions of a model differ significantly in both capability and price. If a channel only offers the lightweight version, mark that channel as “no matching version available” rather than substituting the lightweight price into the comparison table for the flagship model. Otherwise, your conclusions will be systematically skewed in favor of cheaper, lower-tier versions.
Looking at List Price Only, Not Effective Cost
Effective cost also includes token consumption from retries, rate-limit responses, and failed requests. If a channel has low concurrency limits, clients may frequently receive rate-limit responses and retry, increasing the actual token consumption for the same task. When evaluating a channel, record the success rate, average latency, and retry rate, then convert these into a per-task cost.
Ignoring Price Updates
Prices can change within weeks. Your cost analysis report should note the effective date of the prices used, and set a review cycle—for example, re-scraping prices monthly. For systems with hardcoded prices in code or configuration, store the price table in a separate config file to facilitate centralized updates.
Overlooking Cache Pricing and Volume Discounts
Some channels offer cached token pricing or batch API discounts at different rates. Comparing standard unit prices alone will ignore these differences. In your analysis, calculate both “no-cache” and “with-cache” scenarios separately, and use the one that aligns with your actual workload.
Python Code Example for Cost Calculation
The following example provides functions for computing the weighted unit price and monthly cost, and demonstrates how changes in the input ratio affect channel ranking.
def weighted_price(price_in, price_out, input_ratio=0.3):"""Return the weighted unit price, in the same unit as input/output prices."""if not 0 <= input_ratio <= 1:raise ValueError("input_ratio must be between 0 and 1")return price_in * input_ratio + price_out * (1 - input_ratio)def monthly_cost(tokens_million, price_in, price_out, input_ratio=0.3):"""tokens_million is the monthly consumption in millions of tokens."""return tokens_million * weighted_price(price_in, price_out, input_ratio)channels = {"Channel A": (2.0, 8.0),"Channel B": (1.5, 9.0),}for ratio in (0.3, 0.7):print(f"Input ratio: {ratio:.0%}")for name, (p_in, p_out) in channels.items():cost = monthly_cost(1000, p_in, p_out, ratio)print(f" {name}: monthly cost {cost:.1f} CNY")
Running this code yields the following results:
- At a 30% input ratio: Channel A costs 6200.0 CNY per month, Channel B costs 6750.0 CNY per month—Channel A is cheaper.
- At a 70% input ratio: Channel A costs 3800.0 CNY per month, Channel B costs 3750.0 CNY per month—the ranking reverses, and Channel B is cheaper.
This example illustrates that the input/output ratio can change the relative ranking of channels. Conclusions drawn from list prices alone, without considering the actual ratio for your business, are unreliable.
Practical Recommendations
First, base decisions on weighted unit price and effective cost, not on list prices or channel type. Second, derive the input/output token ratio and cache hit rate from call logs before selecting a calculation framework. Third, keep price tables, exchange rates, retrieval timestamps, and source URLs in a single document, and review them monthly. Fourth, run small-scale trial traffic across multiple channels for the same model, using real success rates and latency data to correct paper-based estimates.
If your team’s infrastructure is already deployed on Baidu AI Cloud, you can integrate the template above into your existing cost monitoring system, using the same calculation framework to compare different options and reduce cross-platform accounting overhead.
