Evaluating LLM API Cost-Effectiveness: A Practical Engineering Guide
This guide explains why the lowest per-token price does not always mean the lowest cost when building AI agent, batch text processing, or online Q&A applications. It introduces a unified cost model based on input/output tokens, cache hit rate, retries, and task success rate, analyzes four key variables, and outlines a practical selection and load-testing workflow to help developers continuously reduce per-task costs.
1. Background: Why the Lowest Unit Price Is Not the Lowest Cost
When building agent applications, batch text processing pipelines, or online Q&A services, teams often focus on the model’s unit price and assume that the cheapest option must be the most cost-effective. In practice, the cost of a single task is a function of multiple variables, and unit price is only one factor. If cache hits, output length, retry rate, and task success rate are ignored, it is easy to end up with a counterintuitive result: a cheaper unit price but a higher bill.
A lightweight general-purpose LLM recently released its official version, with unchanged pricing: CNY 1 per million input tokens and CNY 2 per million output tokens. Compared with some competing options, even after significant price cuts, their unit prices remain higher than this lightweight option. With an additional discount of nearly 2% from cache hits, the per-task cost on standard evaluation tasks with comparable intelligence can be as low as about 60%. At the same time, the official version shows almost no output length inflation, with similar capabilities but significantly improved performance in agent scenarios. This illustrates that cost-effectiveness is a systems engineering problem, not simply a price comparison.
2. Cost Model: Breaking the Bill Down into Computable Factors
To rigorously compare different options, start by establishing a unified cost function. Let the number of input tokens be Tin, the number of output tokens be Tout, the input unit price be Pin, the output unit price be Pout, the cache hit ratio be h, the cache discount rate be d (0<d<1), the number of retries be r, and the success rate of a single task be s. Then:
Cost per call C = Tin * Pin * (1 - h + h*d) + Tout * PoutExpected cost per task E = C * (1 + r) / s
This formula reveals three commonly overlooked conclusions:
- The cache discount applies to the input side and provides the greatest benefit in scenarios with long contexts, fixed system prompts, and repeated document Q&A; it provides almost no benefit for short prompts.
- The output unit price is usually higher than the input unit price, and output length inflation directly amplifies the bill. Therefore, a model that is “more verbose” may be more expensive even with a lower unit price.
- Success rate and retry count are multiplier factors. A cheaper option that requires multiple retries to complete a task may have a higher expected cost than a more expensive but more stable option.
3. In-Depth Analysis of Four Key Variables
3.1 Input and Output Pricing Structure
The pricing ratio between input and output varies significantly across options. For “read-heavy, write-light” tasks, such as long-document summarization and code repository retrieval Q&A, input accounts for a large proportion, making the input unit price and caching capability more critical. For generation-intensive tasks, such as copywriting and structured data extraction, output accounts for a large proportion, making the output unit price and output length control more critical. During selection, first build a token profile using real traffic, calculate the input/output ratio, and then plug it into the formula.
3.2 Caching Mechanism and Hit Rate
Caching usually refers to billing discounts for reusing repeated prefixes (system prompts, tool definitions, long documents). To improve the hit rate, engineering practices should include: keeping prompt prefixes stable, placing variables at the end, and avoiding inserting timestamps or random identifiers into fixed sections. An illustrative refactoring is shown below:
# Bad example: random content placed in the prefix prevents cache reuseprompt_bad = f"Request ID:{uuid4()}\nYou are a customer service assistant...\nUser question:{question}"# Good example: fixed prefix first, dynamic content lastSYSTEM = "You are a customer service assistant and must follow these guidelines:..."def build_prompt(question: str) -> str:return f"{SYSTEM}\nUser question:{question}"
3.3 Output Length and Task Completion
If the output token count inflates with version upgrades, it creates hidden costs. During evaluation, keep the same set of prompts and decoding parameters, and measure the average output length and task success rate to avoid “slightly better capabilities but a much larger bill.” Structured output constraints can also be used to limit length, such as requiring fixed fields, to reduce ineffective elaboration.
3.4 Agent Capabilities and Success Rate
Agent scenarios involve multiple rounds of tool calls, and cost is determined by “number of rounds × tokens per round.” If a model has high tool selection accuracy and few ineffective rounds, its total cost may be lower even with a slightly higher unit price. Recommended metrics include: first-pass task success rate, average number of tool call rounds, proportion of ineffective calls, and timeout rate.
4. A Practical Selection and Load-Testing Workflow
- Build an evaluation set: sample no fewer than several hundred requests from real business traffic, covering long and short inputs and different output types.
- Unify parameters: fix the temperature, maximum output length, and prompt version to ensure comparability.
- Collect metrics: record input/output tokens, cache hit status, retry count, success rate, and end-to-end latency.
- Plug into the model: use the formula above to calculate the expected cost per task instead of comparing unit prices.
- Validate with canary releases: roll out to a small traffic share before full rollout, and continuously monitor cost and latency drift.
For monitoring, a combination of general cloud capabilities can help: use a logging service to record call details, use monitoring and alerting to set thresholds for cost and success rate, use a message queue to smooth peak and valley traffic, use object storage to archive evaluation samples, and use a container platform to host load-testing tasks, turning cost governance into a routine capability. If using related general product categories from Baidu AI Cloud, it is also recommended to follow the design principles of “observable, replayable, and comparable.”
5. Common Pitfalls and Engineering Recommendations
- Pitfall 1: Only looking at unit price. Instead, look at the expected cost per task.
- Pitfall 2: Ignoring output inflation. Include output length in version acceptance metrics.
- Pitfall 3: Using caching carelessly. Govern prompt structure to keep prefixes stable and reusable.
- Pitfall 4: Ignoring failure costs. Include success rate and retries in cost accounting.
- Pitfall 5: One-time selection. Establish a quarterly review mechanism to dynamically adjust routing strategies in response to price and capability changes.
6. Conclusion
The official version of the lightweight general-purpose LLM once again pushes cost-effectiveness to a new level. Its real lesson is that cost optimization is the combined result of prompt engineering, cache governance, output constraints, and stability design. As long as developers establish a unified cost formula, insist on load testing with real traffic, and incorporate caching and success rates into daily monitoring, they can continuously reduce per-task costs while maintaining comparable capabilities, truly spending the budget on business value.
