Evaluating LLM API Cost and Response Speed: A Practical Measurement Method
This article explains how to evaluate LLM API costs and response speed from an engineering perspective. It covers unified cost accounting for cached and uncached input and output tokens, effective unit price, break-even analysis for monthly plans, TTFT and output-rate measurement, time-of-day testing, observability of reasoning processes, and a deployment checklist.
Introduction
When choosing an LLM API, developers often look first at the list price and then at model leaderboards. However, monthly spending and task efficiency are often determined by usage structure, cache hit rates, output share, and service stability at different times of day. Two services with similar list prices can differ by several times in real-world cost. This article breaks down these factors from an engineering perspective and provides calculation and testing methods that can be applied directly.
1. Standardize Cost Accounting
LLM APIs usually charge by token and distinguish between input and output. Input may also be divided into cached and uncached portions, with a significant price difference between the two, and output token prices are generally higher than input prices. Looking only at total token count therefore cannot reconstruct the real cost. Three usage categories need to be tracked separately: uncached input, cached input, and output.
In practice, the server may sometimes return only a total or only some fields. In this case, log the usage fields for each request locally and aggregate them by date and task type. If cache hit statistics are missing, historical data from similar requests can be used for estimation, but the report should note that the value is an estimate.
The general cost formula is:
Total cost = (uncached input × uncached price + cached input × cached price + output × output price) / 1,000,000
The corresponding pseudocode is:
function EstimateCost(totalTokens, cacheHitRate, outputShare, uncachedPrice, cachedPrice, outputPrice):outputTokens = totalTokens * outputShareinputTokens = totalTokens - outputTokenscachedTokens = inputTokens * cacheHitRateuncachedTokens = inputTokens - cachedTokensreturn (uncachedTokens * uncachedPrice + cachedTokens * cachedPrice + outputTokens * outputPrice) / 1000000
2. Use Effective Unit Price Instead of List Price
Effective unit price refers to the actual average cost per million tokens. It is mainly affected by three variables: cache hit rate, output share, and whether there are peak and off-peak price differences.
The following assumed values demonstrate this effect. Assume uncached input costs RMB 1 per million tokens, cached input costs RMB 0.2 per million tokens, and output costs RMB 4 per million tokens. A task type consumes a total of 1 billion tokens, with an output share of 1%.
When the cache hit rate is 60%, the output portion costs RMB 40, cached input costs about RMB 118.8, and uncached input costs RMB 396, for a total of about RMB 554.8. The effective unit price is about RMB 0.55 per million tokens.
When the cache hit rate rises to 90%, cached input costs increase to about RMB 178.2, uncached input drops to RMB 99, and the total is about RMB 317.2. The effective unit price is about RMB 0.32 per million tokens.
For the same task, cost falls by about 43% solely because of a change in cache hit rate. Recording the cache hit rate therefore reflects real cost better than recording the list price. When the task structure is stable, the effective unit price can be updated weekly and used as the basis for budgeting.
3. Break-Even Analysis for Monthly Plans
To determine whether a monthly plan is worthwhile, calculate the break-even usage:
Break-even usage = monthly fee ÷ pay-as-you-go effective unit price
For example, if the monthly fee is RMB 200 and the pay-as-you-go effective unit price is RMB 0.5 per million tokens, the break-even point is 200 ÷ 0.5 = 400 million tokens. If monthly usage is above this value, the monthly plan is more cost-effective; if it is below this value, pay-as-you-go billing is more suitable.
The calculation should also account for hidden conditions in the plan, such as concurrency limits, rate limits, usage reset cycles, and whether there are priority differences during peak periods. These conditions may make a plan nominally cost-effective while failing to meet demand during business peaks. Break-even analysis should therefore be combined with actual peak concurrency.
4. Speed Measurement: Separate TTFT and Output Rate
A single average speed often hides problems. It is better to split measurement into the following metrics:
- Time to first token (TTFT): the time from sending a request to receiving the first data chunk.
- Output rate: output tokens divided by output duration.
- Inference-stage rate and answer-stage rate: if the server returns data for these two stages separately, measure them separately.
The number of data chunks returned by a streaming interface is not equal to the number of tokens. When calculating rate, use the usage fields returned by the server; otherwise, the result will have a noticeable bias.
Example pseudocode:
startTime = now()firstChunkTime = nullfor each chunk in sendRequestAndReceiveStream(url, requestBody):if firstChunkTime is null:firstChunkTime = now()recordChunk()endTime = now()ttft = firstChunkTime - startTimeoutputRate = serverReturnedOutputTokens / (endTime - firstChunkTime)
5. Time-of-Day Variation and Control Tests
A single speed test has little reference value. Test several times during weekday peak hours, midday, nighttime, and weekends, then take the median and P90 values to form a baseline.
During testing, also compare against other services in the same network environment. If the control service performs normally while the target service drops significantly, the problem is more likely to be server-side load rather than the local network. If your own request concurrency is far below the service’s published limit, a rate drop is also unlikely to be caused by rate limiting; server capacity constraints are a more common cause.
After establishing a baseline, set threshold alerts for production monitoring. For example, if the output rate falls below half of the baseline median for several minutes, automatically notify the relevant personnel and consider switching to a backup option.
6. Observability of the Reasoning Process
Some models force the reasoning process to be enabled and cannot be turned off. This reasoning process increases both TTFT and token consumption, so it should be included in the cost model rather than treated as free.
There are four ways to address this situation:
- Show reasoning status in the interface: let users distinguish between “reasoning” and “service stuck.” If only a blank loading animation is shown, users can easily mistake the state for a failure.
- Tier tasks: use lightweight models or lower reasoning intensity for simple tasks, and call stronger reasoning capabilities only for complex tasks.
- Control the output cap: limit the cost of a single request through the maximum output length parameter, and note that reasoning tokens may count toward this cap.
- Constrain output structure: specify the number of steps and the output format in the prompt to reduce unnecessary expansion.
7. Deployment Checklist
- Log uncached input, cached input, and output usage fields by task type.
- Update the effective unit price weekly using actual data and compare it with the list price.
- Perform break-even calculations for monthly and pay-as-you-go plans, and verify concurrency and rate limits.
- Establish a time-segmented speed baseline and set alert thresholds.
- Display reasoning status and output rate in the user interface.
- Set monthly budget alerts to avoid overspending caused by abnormal tasks.
Conclusion
The real cost of an LLM API depends on usage structure and service performance, not just the list price. By standardizing cost accounting, calculating effective unit price, analyzing break-even points, testing speed across time periods, and paying attention to observability of the reasoning process, teams can move model selection from experience-based judgment to data-driven decisions. Each project should keep a usage and performance ledger and review it regularly so that the approach can be adjusted as business scale and service prices change.
