How to Evaluate the Cost and Response Speed of LLM APIs
This guide explains how to evaluate LLM API costs and response speed beyond list prices. It covers billing metrics, cache hit rates, subscription versus pay-as-you-go plans, streaming latency measurement, peak-hour troubleshooting, and a framework for comparing and switching providers.
1. Unit Prices Do Not Reflect Actual Cost
Many teams start by reading the unit price table and then estimate their monthly bill. This works reasonably well when usage is simple. However, an LLM API bill depends on several variables. Input and output tokens are usually priced differently, cached input tokens may have their own price, and some providers charge differently during peak and off-peak hours. Comparing only the listed unit prices can lead to wrong conclusions.
Cost evaluation requires a consistent measurement basis first. Fix a reporting period, such as a day or a project, and record the following metrics:
- Total input tokens, and how many of them were cache hits
- Total output tokens
- Number of calls and average context length per call
- The time period in which each call was made
With this data, you can compare different providers on the same basis.
2. Calculating Unit Cost
After splitting billable items by request, the cost of a single call can be expressed as:
Cost per call = uncached input tokens × uncached input price + cached input tokens × cached input price + output tokens × output price
The code below totals the cost of each request and calculates the cache hit rate and output share. Prices are in CNY per million tokens, and the results are also in CNY.
def estimate_cost(usage: dict, price: dict) -> float:"""Estimate the cost (CNY) of a single request from its token usage.Prices in `price` are in CNY per million tokens."""cached = usage["cached_tokens"]miss_in = usage["input_tokens"] - cachedcost = (miss_in * price["input_miss"]+ cached * price["input_cached"]+ usage["output_tokens"] * price["output"])return cost / 1_000_000def summarize(records: list[dict], price: dict) -> dict:"""Summarize billing metrics for one reporting period."""total_input = sum(r["usage"]["input_tokens"] for r in records)total_output = sum(r["usage"]["output_tokens"] for r in records)total_cached = sum(r["usage"]["cached_tokens"] for r in records)total_cost = sum(estimate_cost(r["usage"], price) for r in records)total_tokens = total_input + total_outputreturn {"Total tokens": total_tokens,"Cache hit rate": total_cached / total_input if total_input else 0.0,"Output share": total_output / total_tokens if total_tokens else 0.0,"Total cost (CNY)": round(total_cost, 2),"Cost per million tokens (CNY)": round(total_cost / total_tokens * 1_000_000, 4) if total_tokens else 0.0,}
The results of this code appear directly on the bill. Suppose your input consists mainly of long context with a high cache hit rate. In that case, the price of cached input becomes the main cost driver. Conversely, when the output share is low, a high output price has only a limited effect on total cost.
Impact of Cache Hit Rate
Multi-turn conversations and agent-style tasks repeatedly send historical context, so their cache hit rates are usually high. For each one percentage point increase in the hit rate, the savings depend on the difference between the uncached and cached prices, multiplied by the number of affected tokens. Therefore, do not evaluate based on the average unit price alone. Measure the share of each type of input separately.
Subscription Plans vs. Pay-as-You-Go
Whether a monthly plan is worthwhile depends on two factors: whether actual usage approaches the plan’s quota, and how usage beyond the quota is billed. Pay-as-you-go is more flexible at low usage levels. A subscription plan may save money when usage is stable and high. To decide, replay the past month’s actual usage under both billing methods instead of relying on estimates.
3. Metrics for Response Speed
Response speed should be broken down into several separate dimensions:
- Time To First Token: The time from sending a request to receiving the first data chunk. It reflects queuing and preprocessing overhead.
- Sustained throughput: The number of output tokens per unit of time, usually measured in tokens/s.
- Reasoning rate vs. answer rate: Some models output their reasoning process before the final answer, and the two rates may differ. Measure them separately.
- Total request duration: The time needed to receive the complete response. It is closely tied to output length, so interpret it together with output volume.
The code below calculates these metrics from the data chunks of a streaming interface. Field names vary by provider, so refer to the relevant API documentation for the exact structure. This example uses a generic format.
```python
import time
def measure_stream(stream_iter) -> dict:
“””Measure time to first token, token counts, and throughput of a streaming response.
Each item yielded by stream_iter is a chunk shaped like {“type”: “reasoning” | “answer”, “tokens”: int}.
“””
start = time.perf_counter()
first_at = None
reasoning_tokens = 0
answer_tokens = 0
reasoning_time = 0.0
answer_time = 0.0
prev = start
current_type = None
for chunk in stream_iter:now = time.perf_counter()if first_at is None:first_at = nowkind = chunk["type"]elapsed = now - previf kind == "reasoning":reasoning_tokens += chunk["tokens"]reasoning_time += elapsedelse:answer_tokens += chunk["tokens"]answer_time += elapsedprev = nowcurrent_type = kindend = time.perf_counter()return {"Time to first token (s)": round(first_at - start, 3) if first_at else None,"Reasoning rate (token/s)": round(reasoning_tokens / reasoning_time, 1) if reasoning
