Evaluating Blended LLM Inference Costs and Optimizing Cost Efficiency
LLM inference costs are shaped by three differently priced token categories: cached input tokens, uncached input tokens, and output tokens. This article explains how to calculate a blended price using a consistent denominator, assess the impact of cache-hit rates and output proportions, and establish an engineering workflow for monitoring and optimizing inference costs. It also includes a Python example and highlights common measurement pitfalls.
In large language model inference, cost analysis is often reduced to checking the input price or the price of a single API call. In practice, an invoice may include multiple token categories: cached input tokens, uncached input tokens, and output tokens. These categories typically have different unit prices, and their proportions can vary significantly across requests.
As a result, comparing only one price provides an incomplete view of actual costs. A more reliable approach is to standardize the billing basis first, then calculate a weighted blended price based on the composition of each request.
1. Why Use a Blended Price?
The main value of a blended price is that it converts a complex request structure into a comparable, monitorable, and optimizable cost metric.
For developers, it helps estimate budget differences across calling patterns. For enterprise users, it supports capacity planning, cost allocation, and prioritization of optimization efforts.
Focusing only on the input price may hide cost increases caused by a high proportion of output tokens. Conversely, focusing only on the cached-input price may overestimate the actual benefit of caching. A blended price accounts for cached input, uncached input, and output costs at the same time, reducing the risk of misleading conclusions based on a single metric.
2. Standardizing the Billing Basis and Formula
Let the unit prices for the three token categories be:
p_cache: cached input token pricep_input: uncached input token pricep_output: output token price
Let their proportions of the total token count be:
r_cache: cached input token proportionr_input: uncached input token proportionr_output: output token proportion
These proportions must satisfy:
r_cache + r_input + r_output = 1
The blended price, calculated on the basis of total tokens, is:
p_avg = r_cache × p_cache + r_input × p_input + r_output × p_output
The denominator must be consistent. If the blended price means the cost per million total tokens, the proportions must be calculated against total tokens. If it means the cost per million input tokens, output tokens must be incorporated using an appropriate conversion ratio rather than being mixed directly with input prices.
In practical evaluations, always document whether the denominator is total tokens, input tokens, or output tokens. The same denominator must be used when comparing different solutions; otherwise, the results are not meaningful.
3. Example: How Token Proportions Affect Blended Cost
Assume the following unit prices:
- Cached input:
0.1 - Uncached input:
1.0 - Output:
4.0
The unit is “cost per million tokens.” These values are for illustrating the formula only and do not represent the price of any specific product.
Scenario A
Suppose the token proportions are:
- Cached input: 50%
- Uncached input: 30%
- Output: 20%
The blended price is:
0.5 × 0.1 + 0.3 × 1.0 + 0.2 × 4.0 = 1.15
Scenario B
If prefix reuse increases the cache-hit proportion to 70%, while uncached input falls to 15% and output falls to 15%, the blended price becomes:
0.7 × 0.1 + 0.15 × 1.0 + 0.15 × 4.0 = 0.82
Scenario C
If output tokens account for 50% of the request, even with a 30% cache-hit rate, the blended price rises to:
0.3 × 0.1 + 0.2 × 1.0 + 0.5 × 4.0 = 2.23
These examples show that both the output proportion and cache-hit rate can have a significant effect on blended cost. Cost evaluations should therefore be based on the actual structure of production requests.
4. Optimization: Cache Hits and Output Proportions
Increasing the cache-hit rate is a common way to reduce blended inference cost. In engineering practice, cache reuse can often be improved by:
- Keeping system prompts stable
- Reusing fixed context
- Placing frequently reused public prefixes earlier
- Avoiding unnecessary insertion of dynamic content
However, cache-hit rate should not be optimized in isolation. It must be balanced against business correctness, context isolation, and cache invalidation policies.
The output proportion is equally important. Because output tokens are often more expensive than input tokens, the following measures can reduce output consumption:
- Limiting output length
- Removing redundant explanations
- Using structured output
- Generating content in stages when appropriate
For long-context workloads, it is also important to distinguish necessary context from context that can be summarized. Repeatedly sending large amounts of low-value content through the inference pipeline can increase costs without providing proportional value.
5. An Engineering Workflow
A practical cost-management process can be organized into four steps.
Step 1: Collect Request Composition Data
For every call, record:
- Cached input tokens
- Uncached input tokens
- Output tokens
- Time-period information
Step 2: Build a Cost Model
Use the same blended-price formula to calculate costs, then aggregate the results by dimensions such as:
- Business line
- API or interface
- Tenant
Step 3: Configure Monitoring and Alerts
Set alerts for conditions such as:
- An abnormal increase in blended price
- A decline in cache-hit rate
- A sudden increase in output length
Step 4: Run Controlled Comparisons
While maintaining the required quality level, compare different:
- Prompt structures
- Context-management strategies
- Output constraints
The goal is to measure how each change affects cost.
On general-purpose cloud platforms, logging services, monitoring and alerting tools, and object storage can support data collection, archiving, and visualization. For example, general logging and monitoring capabilities provided by Baidu AI Cloud can be used for this type of cost observation. The specific implementation should be designed according to the architecture and operational requirements of each system.
6. Illustrative Python Code
The following code demonstrates the blended-price calculation. The prices and proportions are hypothetical:
def blended_price(p_cache, p_input, p_output, r_cache, r_input, r_output):total = r_cache + r_input + r_outputif abs(total - 1.0) > 1e-9:raise ValueError("The proportions must sum to 1")return r_cache * p_cache + r_input * p_input + r_output * p_outputprices = {"cache": 0.1, "input": 1.0, "output": 4.0}scenarios = {"A": (0.50, 0.30, 0.20),"B": (0.70, 0.15, 0.15),"C": (0.30, 0.20, 0.50),}for name, (rc, ri, ro) in scenarios.items():cost = blended_price(prices["cache"], prices["input"], prices["output"], rc, ri, ro)print(name, round(cost, 4))
7. Common Mistakes
Common mistakes include:
- Comparing only input prices while ignoring output proportions
- Treating the cache-hit rate as directly equivalent to the percentage of cost reduction
- Using different denominators for different solutions
- Ignoring price differences between peak and regular periods
- Looking only at averages instead of long-tail requests
A more reliable approach is to monitor the following metrics together:
- Blended price
- Cache-hit rate
- Output-token proportion
- Cost per unit of business activity
Cost, quality, and latency should then be evaluated as a whole rather than optimized independently.
Conclusion
LLM inference cost evaluation should begin with the actual composition of requests. By calculating a blended price using a consistent billing basis, and then optimizing cache reuse, controlling output length, and continuously monitoring cost trends, teams can move from simply “checking prices” to managing cost through request structure, proportions, and long-term behavior.
