How to Evaluate Cost and Inference Latency When Choosing an LLM API

    A developer's guide to evaluating LLM APIs beyond benchmark scores. It covers token-based billing, cache hit rates, output and reasoning token costs, time to first token, throughput during peak load, and whether reasoning mode can be turned off.

    1. Why Evaluate LLM APIs Systematically

    Many teams evaluate LLM APIs by checking leaderboard scores and trying a few sample questions. This approach rarely answers the questions that matter for production: What will the monthly bill be? Is the service stable during peak hours? Is the time to complete a single task acceptable for the business?

    The cost and performance of an API call depend on three factors: the pricing rules, the server-side load, and how the calls are made. If any one of these is ignored, the estimate can be significantly off.

    This article takes a developer’s perspective and splits the evaluation into three dimensions: cost structure, response speed, and the reasoning process. Each dimension includes an approach to judging it and example code. Readers can substitute their own business data. The numbers in this article are illustrative. They show how to run the calculations and do not represent the pricing or performance of any specific service.

    2. Cost Evaluation: From Unit Price to Actual Bill

    2.1 Understanding Token-Based Billing

    Most pay-as-you-go model services measure usage in tokens. The cost of a request usually consists of input tokens and output tokens. The input portion may also be priced differently when it hits the cache.

    Therefore, looking only at the “price per million tokens” does not let you calculate the bill directly. To estimate costs, you need the input-to-output ratio, the cache hit rate, and the total call volume.

    2.2 The Impact of Cache Hit Rate

    In multi-turn conversations or long-context scenarios, the system prompt and conversation history repeat in every request. If the service recognizes and reuses this identical prefix, the cached portion is billed at a lower unit price. The higher the hit rate, the lower the effective average price.

    Common ways to improve the hit rate include keeping the system prompt stable, avoiding dynamic timestamps or random identifiers in the prefix, and placing unchanging content at the beginning of the request.

    2.3 Output Share and Reasoning Token Billing

    Output tokens are usually priced higher than input tokens. If a task mainly generates long text, the output share can significantly increase costs.

    Some models generate reasoning content before the answer. This reasoning content also counts as output tokens. Therefore, base your estimates on the output volume shown in the bill, not only on the length of the final answer displayed in the interface.

    2.4 Example: A Cost Estimation Function

    1. def estimate_cost(total_tokens, output_ratio, cache_hit_rate,
    2. input_price, output_price, cache_price):
    3. # Unit prices are in CNY per million tokens; ratio parameters range from 0 to 1
    4. output_tokens = total_tokens * output_ratio
    5. input_tokens = total_tokens - output_tokens
    6. cached = input_tokens * cache_hit_rate
    7. uncached = input_tokens - cached
    8. cost = (cached * cache_price
    9. + uncached * input_price
    10. + output_tokens * output_price) / 1_000_000
    11. return round(cost, 2)
    12. # Example assumptions: 1 billion tokens, 5% output share, input price 1 CNY, output price 4 CNY, cache price 0.2 CNY
    13. print(estimate_cost(1_000_000_000, 0.05, 0.80, 1.0, 4.0, 0.2)) # Cache hit rate 80%, about 542 CNY
    14. print(estimate_cost(1_000_000_000, 0.05, 0.50, 1.0, 4.0, 0.2)) # Cache hit rate 50%, about 770 CNY

    The two results show that, with all other conditions equal, raising the cache hit rate from 50% to 80% lowers the cost by roughly 30%. A sensitivity analysis like this is more useful than a single unit price.

    2.5 Building Cost Models by Task Type

    Token structures vary widely across business scenarios, so it is best to build a separate model for each task type:

    • Knowledge Q&A and document retrieval: Mostly input tokens, with a high degree of repeated context prefixes, so the cache hit rate is usually good.
    • Code generation and rewriting: Higher output share, so pay close attention to the output price.
    • Batch text processing: Each input is short, but the number of calls is high, so track request counts and concurrency.
    • Multi-step agent tasks: Each step carries the conversation history, so input volume accumulates as the steps increase.

    For each task type, collect at least one week of real usage data, calculate its cache hit rate and output share, and then aggregate the results into the total cost.

    3. Response Speed: Throughput, Time to First Token, and Peak-Hour Variation

    3.1 Defining Key Metrics

    When evaluating response speed, track these three metrics together:

    • Time to First Token (TTFT): The time from sending a request to receiving the first data chunk. It directly affects how quickly users perceive the response starting.
    • Output throughput: The number of tokens generated per second. It determines how long a long answer takes to complete.
    • End-to-end latency: The total time to finish a complete task, including reasoning, generation, and tool calls.

    Averages alone can hide problems, so also record percentiles such as P50 and P95.

    3.2 Common Causes of Peak-Hour Slowdowns

    服务端 Services usually enforce concurrency and quota limits. When load rises, queueing time increases, and the generation speed of individual requests may also drop.

    To tell whether slowness comes from the network or the server, run a control test: call other services or other endpoints at the same time. If several independent services slow down together, the cause is most likely the client network or local environment. If only one service slows down, the cause is more likely that service’s load.

    3.3 Example: Monitoring Streaming Responses

    1. import time
    2. def probe_stream(stream_iter):
    3. start = time.monotonic()
    4. first_at = None
    5. chunks = 0
    6. for _ in stream_iter:
    7. if first_at is None:
    8. first_at = time.monotonic()
    9. chunks += 1
    10. end = time.monotonic()
    11. if first_at is None:
    12. return {"错误": "未收到任何数据"}
    13. gen_time = end - first_at
    14. return {
    15. "首包延迟秒": round(first_at - start, 3),
    16. "数据块速率": round(chunks / gen_time, 1) if gen_time > 0 else 0,
    17. "总耗时秒": round(end - start, 2),
    18. }

    Note that the number of data chunks does not exactly match the number of tokens, so this metric is only useful for relative comparisons. For an accurate token rate, use the usage fields returned by the server.

    4. Reasoning: Visibility and Trade-offs

    4.1 Can Reasoning Mode Be Turned Off?

    Some models let you configure whether reasoning is enabled in the request. Others always have reasoning turned on. When reasoning is always on, even simple questions consume reasoning tokens and take longer to answer.

    During model selection, confirm whether reasoning mode is configurable, and treat this as a hard requirement if it matters for your workload. If reasoning cannot be disabled, the application layer needs to handle tasks