LLM API Selection and Cost Optimization: From Unit Price to Total Cost
This article explains why a lower per-token price can still result in higher actual LLM API costs. It presents a token-based cost model, a five-dimension evaluation framework covering task fit, real-scenario cost simulation, caching, batch processing, and engineering optimizations, and a practical workflow for balancing performance and budget.
LLM API Selection and Cost Optimization: From Unit Price to Total Cost
1. Background: Why “Price Cut” and “Too Expensive” Can Coexist
A recent industry discussion is quite representative: after a platform released a new-generation flagship model, comments included both praise for significantly improved capability and criticism that it was too expensive to use. On the surface, these reactions seem contradictory. In reality, they reflect a mismatch in how model API costs are evaluated.
A common phenomenon is that the API unit price of a new-generation model may indeed be lower than that of the previous generation. For example, the previous-generation model might have had an input price of CNY 20 per million tokens, while a newer version with a larger parameter scale and stronger capabilities is priced lower. Yet users feel that it is “more expensive.” The reason for this perception gap is often that actual usage cost is not equal to unit price. Understanding this is the first step toward effective model selection and cost control.
2. Understanding Token Billing: The Basic Formula for Actual Cost
2.1 What Is a Token
Large language models process text in units called tokens. In Chinese, one character usually corresponds to one or more tokens; in English, one word corresponds to roughly 1 to 1.5 tokens. Billing usually separates input and output, and the two unit prices are generally different, with output priced higher than input.
2.2 A Simple Cost Estimation Model
The cost of a single call can be approximated as:
Cost per call = input tokens × input unit price + output tokens × output unit price
For a typical RAG (Retrieval-Augmented Generation) application, the input often includes the system prompt, retrieved document snippets, and conversation history. The number of input tokens is often several times or even dozens of times the output. This means:
- Input unit price often has a greater impact on total cost than output unit price;
- System prompts are sent again in every turn, so applications with long prompts continuously amplify input costs.
2.3 Hidden Costs of Context Length
New-generation models often support longer context windows. Long context is an advance in capability, but it changes the cost structure:
- Cumulative effect in multi-turn conversations: If the full history is included in every request, cost grows approximately quadratically with the number of turns. The input size in turn n is roughly the sum of all content from the previous n-1 turns.
- Barrier to long-document processing: Processing a document of dozens of pages may require hundreds of thousands of input tokens in a single request. Even if the unit price is low, the cost per call is not low.
Therefore, a reasonable explanation for “the unit price dropped but it feels more expensive” is that users are using longer contexts and more complex tasks on the new model. The task scale has changed, so the cost naturally differs.
3. A Comprehensive Cost Evaluation Framework: Five Key Dimensions
When selecting a model, do not rely only on the price table. Consider building an evaluation framework around the following dimensions:
3.1 Task Fit
Model performance varies significantly across tasks. Simple classification and structured extraction do not necessarily require a flagship model; complex reasoning, code generation, and long-document understanding are where stronger capabilities matter. Using a smaller model for simple tasks is the biggest cost lever.
3.2 Cost Simulation in Real Scenarios
Before full integration, run a small-scale stress test using real business samples:
# Illustration: estimate the cost per task based on real samplesdef estimate_cost(samples, in_price, out_price):total = 0for s in samples:in_tokens = count_tokens(s["prompt"])out_tokens = count_tokens(s["completion"])total += in_tokens * in_price + out_tokens * out_pricereturn total / len(samples) # Average cost per task
Multiply the average cost per task by the estimated call volume to obtain a monthly budget. This is far more reliable than looking only at the unit price table.
3.3 Caching and Reuse Mechanisms
Many model services provide context caching: repeated prefixes, such as long system prompts or fixed documents, are billed at a lower rate or receive a cache-hit discount. For applications with fixed prompts, caching often delivers significant cost reductions and deserves close attention during model selection.
3.4 Batch Processing and Non-Real-Time Channels
For latency-insensitive tasks, such as offline data labeling and batch content generation, batch interfaces usually offer notable discounts. Properly separating real-time and non-real-time traffic can reduce overall costs.
3.5 Engineering-Side Optimizations
- Prompt compression: streamline system prompts and remove redundant examples;
- History trimming: in multi-turn conversations, keep the most recent turns with a sliding window or summarize the history;
- Output control: limit output length by explicitly instructing the model to “output JSON only,” etc.;
- Tiered model routing: route requests to different model tiers based on task complexity.
4. Balancing Performance and Cost: An Actionable Decision Workflow
- Define the task list and quality standards: identify which tasks must be completed at high quality and which only need to be “good enough.”
- Compare candidate models: select 2-3 candidates, including different tiers from the same vendor, and compare accuracy and cost on a self-built evaluation set.
- Calculate the cost-effectiveness curve: use “quality achieved per unit cost” as the metric rather than absolute performance alone.
- Canary validation: integrate with a small traffic share, observe real call costs and quality, then ramp up gradually.
- Continuous monitoring: set up monitoring and alerts for token consumption to prevent abnormal calls (such as infinite loop retries) from causing cost overruns.
5. Common Pitfalls
- Pitfall 1: Looking only at input unit price. Output unit price, caching strategy, and context length matter equally.
- Pitfall 2: Assuming a flagship model fits everything. Most simple tasks can use lightweight models; flagship models should be reserved for high-value scenarios.
- Pitfall 3: Ignoring engineering optimization. Optimizing prompt and history management often reduces costs by more than 30% and is easier to implement than switching models.
- Pitfall 4: Treating free quotas as long-term costs. Always calculate budgets based on standard pricing.
6. Conclusion
Criticism that a model is “too expensive” essentially reflects a question of fit among capability, cost, and application scenario. New-generation models may have lower unit prices but longer contexts and stronger capabilities, so actual cost depends on how they are used. For developers, rather than fixating on a single price point, it is better to build a comprehensive evaluation system: task tiering + cost simulation + engineering optimization + continuous monitoring. This helps find the optimal trade-off between performance and budget for your business.
