Evaluating LLM API Cost-Effectiveness: From Pricing Rules to Hands-On Testing
This guide explains how to compare LLM API costs beyond per-token prices. It covers common billing dimensions, including separate input and output pricing, cache-hit pricing, time-based pricing, periodic quotas, and free trial quotas. It also explains hidden cost factors such as context window size, output quality, stability and rate limits, and latency. The article describes a unit task cost metric, a method for testing with real business samples, a worked monthly cost estimate, and engineering practices for reducing token consumption.
1. Unit Price Is Not the Full Cost
When comparing LLM services, many people look first at the price per million tokens. Unit price matters, but it only tells you what one token costs. Your actual spending also depends on how many tokens each request consumes, how much of the input hits the cache, and whether the output needs rework. A single call may be cheap, but if it needs three retries, the total cost may not be lower.
2. Understanding Common Pricing Dimensions
1. Input and output are priced separately. Most services charge more for output tokens than for input tokens, because the model generates output one token at a time. Long responses and outputs that include reasoning steps can raise costs significantly.
2. Cache-hit pricing. When the prefix of a request matches a previous request, the service can reuse the already computed content. This part of the input is billed at a lower price. The cache hit rate depends on how you structure your prompts. Placing fixed content (system instructions, templates, and reference material) at the beginning, and variable content (the user’s question) at the end, usually produces a higher hit rate.
3. Time-based pricing. Some services offer different discounts or prices at different times of day. These rules change often. Always follow the provider’s official announcements, and record the query date in your evaluation notes.
4. Periodic quotas. Weekly or monthly quota limits determine the maximum usage you can get. The speed at which you consume a quota and the unit price are two separate things, so you need to calculate them separately.
5. Free and trial quotas. These resources usually come with limits on context length, rate, or stability. They suit testing and low-frequency tasks, but they should not be used directly as the basis for production cost estimates.
3. Hidden Factors That Affect Effective Cost
- Context window. A smaller window means long documents need to be split into more pieces. This increases the number of calls and the amount of repeated input.
- Output quality. Results that fall short require manual correction or a second call. Both add real cost.
- Stability and rate limits. Frequent timeouts or throttling increase retries and slow down your business processes.
- Task fit. Simple tasks such as classification and information extraction can be handled by a less capable model. Using a large model for these tasks wastes resources.
- Latency. Latency affects user experience and determines how much concurrency you need, which indirectly affects infrastructure cost.
4. Building Evaluation Metrics
The core metric is unit task cost:
Unit task cost = Total spend during the test period / Number of tasks that pass acceptance
The denominator counts tasks that pass acceptance, not the number of calls. This way, rework and failures are included in the cost. Supporting metrics include average latency, first-pass rate, context overflow rate, and cache hit rate.
5. Hands-On Testing Method
- Build a test set. Draw 50 to 200 samples from real business data. Cover three difficulty levels: simple, medium, and complex. Keep reference answers or scoring rules for each sample.
- Fix the parameters. Use the same sampling temperature, maximum output length, and system prompt across all tests so that parameter differences do not distort the comparison.
- Record usage. Count input, output, and cache-hit tokens using the usage fields returned by the service. Do not estimate tokens from character counts.
- Score the outputs. Score each output manually or automatically, and calculate the first-pass rate.
- Calculate unit task cost. Use the code below to complete the calculation.
from dataclasses import dataclass@dataclassclass Pricing:input_price: float # yuan per million uncached input tokenscached_price: float # yuan per million cached input tokensoutput_price: float # yuan per million output tokensdef request_cost(usage: dict, p: Pricing) -> float:"""usage is the usage record of one request. Field names should follow the provider's documentation. Shown for illustration only."""cached = usage["cached_prompt_tokens"]uncached = usage["prompt_tokens"] - cachedcompletion = usage["completion_tokens"]cost = (uncached * p.input_price+ cached * p.cached_price+ completion * p.output_price)return cost / 1_000_000def unit_task_cost(records: list[dict], p: Pricing) -> float:"""Each item in records contains usage and passed (whether the task passed acceptance)."""total = sum(request_cost(r["usage"], p) for r in records)passed = sum(1 for r in records if r["passed"])return total / passed if passed else float("inf")
6. Monthly Cost Estimation
Use this formula:
Monthly cost = Average daily requests × 30 × Average cost per request
If the service uses time-based pricing, first split your request volume by time period, multiply each part by its matching price, and then add the results together.
The numbers below are for demonstration only and do not reflect any actual prices. Assume 1,000 requests per day, an average of 2,000 input tokens and 300 output tokens per request, and a 60% cache hit rate. The uncached input price is 1 yuan per million tokens, the cached input price is 0.1 yuan per million tokens, and the output price is 4 yuan per million tokens.
- Input cost per request: (2,000 × 0.4 × 1 + 2,000 × 0.6 × 0.1) / 1,000,000 = 0.00092 yuan
- Output cost per request: 300 × 4 / 1,000,000 = 0.0012 yuan
- Total cost per request: 0.00212 yuan
- Monthly cost: 0.00212 × 1,000 × 30 = 63.6 yuan
The cache hit rate has a large effect on cost. Assume cached tokens are priced at 10% of the uncached price. If the hit rate rises from 60% to 90%, the effective input price drops from 0.46 to 0.19, a reduction of about 59%. Improving prompt structure can therefore save more than switching to a cheaper model.
7. Engineering Practices for Reducing Token Consumption
- Trim prompts. Remove repeated descriptions, and use structured formats instead of long paragraphs.
- Fix the prefix. Place system instructions and templates at the start of each request, and put dynamic content at the end to improve the cache hit rate.
- Constrain outputs. Require results in a fixed set of fields, limit output length, and cut pleasantries and redundant explanations.
- Route by tier. Send simple tasks to smaller models, and escalate to stronger models only for complex tasks.
- Reuse results. Cache results for identical inputs at the application layer to avoid repeated calls.
- Batch requests. Combine requests for non-real-time tasks to reduce the number of calls and the associated overhead.
- Manage conversation history. Summarize earlier turns and keep only the content relevant to the current question.
- Monitor usage. Track token consumption by feature module so you can spot unusual growth early.
8. Notes on Free and Low-Cost Resources
Free quotas and promotional offers are useful for validating features and building prototypes. Before using them, confirm that the context length meets your needs, check the rate limits and stability, find out whether the quota may change, and review the data usage terms to make sure they are acceptable. These resources change quickly. Include them in the same evaluation framework, retest them regularly, and do not base decisions on past experience or outdated feedback from others.
If your data includes personal information or confidential business information, first confirm the provider’s data processing terms. Where necessary, remove sensitive details before sending the data.
9. Conclusion and Checklist
The key to choosing an LLM service is comparing options with the same set of metrics. Prices and promotions change, but the evaluation method can be reused over the long term. Before making a decision, check the following items:
- Have you calculated input, output, and cache-hit costs separately?
- Have you recorded the query date and rule version for each price?
- Are you comparing options by unit task cost rather than unit price?
- Have you included the costs of retries, failures, and manual corrections?
- Have you completed at least one round of testing with real business samples?
- Have you reviewed data compliance and privacy requirements?
