LLM Token Cost Estimation: Blended Pricing and Routing Optimization
This article explains how to estimate LLM token costs using a consistent statistical basis, blended input/output pricing, and weighted averages across model tiers. It also covers sensitivity ranges for cost reporting, rule-based routing through a gateway, and common causes of token cost spikes in end-to-end monitoring.
1. Why Clarify the Statistical Basis First
When enterprises discuss LLM costs, a common problem is an unclear data basis. The same “share” may refer to the share of token volume or the share of cost. These are not the same: the cost share of a high-priced model is usually much higher than its token volume share. The calculations in this article are based on token volume share. Before using them, confirm which basis your data uses.
In addition, third-party aggregation platforms, provider invoices, and self-hosted gateway logs often cover different scopes. Some include cache hits, while others do not. After obtaining data, record the data source, time period, and statistical scope before starting the calculation.
2. Blended Pricing: Combining Input and Output into One Number
Mainstream LLM APIs usually price input tokens and output tokens separately, and output tokens generally cost more. Looking only at input prices will underestimate costs; looking only at output prices will overestimate costs. A blended price combines the two and makes it easier to compare different models.
The calculation method is as follows:
Blended price = (input share × input price + output share × output price) / (input share + output share)
The input-to-output ratio depends on the business scenario. In scenarios such as RAG (retrieval-augmented generation) and long-document question answering, input is usually much longer than output. This article uses 3:1 as an illustrative assumption. This ratio is not an industry standard, and real projects should replace it with actual statistics on input and output tokens from logs. If the business is mainly code generation or long-form writing, the output share may be higher, and the blended price will rise accordingly.
The following prices are illustrative assumptions only, in USD per million tokens. In actual use, refer to each provider’s current price list.
- Flagship model: input USD 4, output USD 20. Blended price = (3 × 4 + 1 × 20) / 4 = USD 8
- Standard model: input USD 2, output USD 10. Blended price = (3 × 2 + 1 × 10) / 4 = USD 4
- Lightweight model: input USD 0.10, output USD 0.50. Blended price = (3 × 0.10 + 1 × 0.50) / 4 = USD 0.20
3. Weighted Average Cost: Combining Tiers by Share
Once each tier has a blended price, you can weight it by token share. The formula is: weighted average price = the share of each tier × its blended price, then sum the results across all tiers.
The following shares are also illustrative assumptions and do not represent real data from any vendor or platform. Assume that a team’s token distribution is as follows:
| Model tier | Token share | Blended price (USD per million tokens) | Contribution (USD per million tokens) |
|---|---|---|---|
| Flagship model | 50% | 8 | 4.000 |
| Standard model | 22% | 4 | 0.880 |
| Lightweight model | 16% | 0.20 | 0.032 |
| Previous-generation standard model | 6% | 4 | 0.240 |
| Previous-generation flagship model | 6% | 8 | 0.480 |
| Total | 100% | Not applicable | 5.632 |
Based on the table above, the weighted average price is about USD 5.63 per million tokens. This result applies only to the assumed shares and should not be cited directly as an industry benchmark.
The lightweight model accounts for only 16%, but its low unit price means it has a relatively small impact on the overall average price. This conclusion also depends on the specific shares. If the lightweight model share rises and the flagship model share falls, the average price will change significantly. For example, if the flagship model share drops from 50% to 30% while the lightweight model share rises from 16% to 36%, with other tiers unchanged, the weighted average price falls to about USD 4.07 per million tokens. A 20-percentage-point change in each of the two tiers reduces the average price by about 28%. Changes to the routing strategy are directly reflected in costs.
When reporting costs, it is advisable to provide a sensitivity range rather than a single number. For example: “When the flagship and lightweight model shares are swapped by 20 percentage points, the weighted average price ranges from about USD 4.07 to USD 5.63.” Readers can then see the impact of changes in assumptions.
4. Routing Gateway: Sending Requests to the Right Model
The weighted average cost depends on which model tier receives each request. Before a request reaches the LLM, a model routing gateway can be deployed to decide where to forward it based on intent and length.
A common approach is rule-based tiering:
- Greetings, simple Q&A, or very short inputs: check the cache first; on a cache miss, send to the lightweight model.
- Requests requiring multi-step reasoning or containing long code context: send to the flagship model.
- All other requests: send to the standard model.
Below is a structural example. The tokenizer and intent classifier need to be completed according to your own technology stack. The example includes timeout fallback handling.
class ModelRouter:def __init__(self, gateway_client, tokenizer, intent_classifier):self.client = gateway_clientself.tokenizer = tokenizer # Use a tokenizer that matches the target modelself.intent_classifier = intent_classifierdef route_request(self, prompt, history=None):token_len = self.count_tokens(prompt)intent = self.intent_classifier.predict(prompt)if intent == "simple_qa" or token_len < 50:model = "lightweight_model"elif intent == "complex_reasoning" or token_len > 2000:model = "flagship_model"else:model = "standard_model"try:return self.client.call(model, prompt, history)except TimeoutError:# Fall back to the lightweight model on timeout to avoid a direct request failurereturn self.client.call("lightweight_model", prompt, history)def count_tokens(self, text):return len(self.tokenizer.encode(text))
Routing rules should be reviewed regularly against real logs. If the classification error rate is high, output quality from cheaper models may decline, user experience may suffer, and rework costs may offset the savings. Therefore, after each adjustment to routing thresholds, observe both cost and quality metrics.
5. End-to-End Token Monitoring and Alerting
Under high call volumes, a small code issue can quickly increase costs. Common causes include repeatedly inserting history into prompts, repeated retries after tool call failures, and looped agent calls.
