Evaluating LLM API Cost-Effectiveness: A Practical Guide for AI Agents
Choosing an LLM API by its lowest per-token price often leads to higher bills. This guide explains how to evaluate the true cost per completed task by accounting for prompt caching discounts, output token growth, and retry overhead. You will learn a reusable evaluation framework and practical agent optimization techniques—such as prefix fixing, context pruning, and tiered routing—to reduce real-world expenses while maintaining model capability.
1. Why the Lowest Unit Price Isn’t the Lowest Bill
When selecting a large language model API, many teams first look at the price per million tokens. However, the actual bill is determined by several variables, and the unit price is only one factor. The total cost of a complete task can be abstracted as:
Total task cost ≈ (input tokens × input price × (1 - cache hit rate × cache discount))+ (output tokens × output price)+ overhead from failed retries and multi-turn context accumulation
This formula highlights three things. First, cache hits can compress input-side costs to a very low level. Second, the output price carries significant weight in long-text generation and code generation tasks. Third, agent applications involve multi-turn tool calls where context is repeatedly carried, amplifying input tokens by several times and further magnifying the impact of caching.
After the official release of a certain high cost-performance model, its pricing remained at 1 yuan per million input tokens and 2 yuan per million output tokens. Even if a comparable competitor cut prices by 80%, the absolute unit price was still higher. Combined with a cache discount of approximately 98%, running a general intelligence index evaluation resulted in a per-task cost about 60% lower than a competitor with similar intelligence. The key takeaway is that cost evaluation must return to the metric of “cost per completed task” rather than just the listed price.
2. Three Underestimated Cost Variables
2.1 The Leverage Effect of Cache Discounts
The essence of the caching mechanism is reusing the attention computation results of identical prefixes. In scenarios such as structured prompts, fixed system instructions, and long-document question answering, the prefix is highly repetitive, and the hit rate can be substantial. A cache discount approaching 98% means the matched portion of the input is almost free. For agents, system prompts and tool descriptions often make up the bulk of the context, making them naturally suitable for cache hits.
2.2 Hidden Growth in Output Tokens
During version iterations, some models exhibit a “longer thinking” phenomenon: output token volume increases, directly driving up costs and increasing latency. An ideal new version should maintain roughly the same output length while offering comparable capability, or even be more concise. The evaluation method is straightforward: compare the average output length and success rate of the old and new versions using the same set of prompts. If capability is equal but output is shorter, cost-effectiveness has effectively improved.
2.3 Retry and Failure Costs
In an agent workflow, tool call failures, format non-compliance, and timeouts all trigger retries. A model with cheap tokens but a high failure rate may end up with a higher overall cost. Therefore, the evaluation should incorporate success rate and format compliance rate into the cost model rather than only counting tokens.
3. A Reusable Selection and Evaluation Framework
It is recommended that teams build an internal evaluation pipeline with the following steps:
- Fixed evaluation set: Cover five categories of tasks—general reasoning, code, long-text summarization, structured output, and tool calling—with no fewer than one hundred samples per category.
- Dual-metric recording: Record both the “listed price cost” and the “per-task cost including caching and retries.”
- Capability alignment: Use a comprehensive intelligence index score as an anchor, comparing prices only among candidates with similar capabilities to avoid matching low-capability models against low prices.
- Stability observation: Track first-token latency, end-to-end latency, and error code distribution.
- Gradual rollout: Start with a 5% traffic canary to observe the real hit rate and bill before going full scale.
Illustrative call and accounting logic:
def estimate_cost(in_tokens, out_tokens, cached_tokens,in_price=1.0, out_price=2.0, cache_discount=0.02):billable_in = (in_tokens - cached_tokens) + cached_tokens * cache_discountreturn billable_in / 1e6 * in_price + out_tokens / 1e6 * out_price
4. Cost Optimization Practices for Agent Scenarios
- Prefix fixing: Place system prompts, tool schemas, and few-shot examples at the very beginning and keep them unchanged to maximize cache hits.
- Context pruning: Summarize and compress historical turns, retaining only the conclusions necessary for decision-making to avoid linear expansion.
- Output constraints: Use explicit format specifications and length limits to suppress redundant output, using structured output constraints for fields when necessary.
- Tiered routing: Route simple requests to lightweight models and escalate to more complex reasoning models, forming a cost gradient.
- Observability: Use logging services and monitoring alert systems to tag and track tokens and costs by business dimension, forming a cost dashboard.
5. Implementation Recommendations
If an enterprise already uses model services on Baidu AI Cloud, it can combine object storage for dataset hosting, container platforms for running evaluation tasks, message queues for peak shaving, and logging and monitoring systems for cost attribution to form a closed loop. There is only one core principle: use the cost per completed task as the sole judge. By selecting a cache-friendly, output-convergent, and stability-verifiable solution under the premise of capability alignment, you can truly translate cost-effectiveness into your actual bill.
