LLM API Cost Optimization: A Technical Test and Evaluation Guide

    Choosing an LLM API by per-million-token price alone often leads to higher bills. This article breaks API cost into a computable formula covering input, output, cache-read, and cache-write tokens, then explains how task token multiplier and cache hit rate affect effective cost. It outlines a reproducible benchmarking method, practical prefix-cache optimizations, and a vendor evaluation checklist to help developers compare total task cost instead of unit price.

    1. Background: Why a “Cheaper” Model Can Burn Through Your Quota

    When selecting an LLM API for production, teams often make the same mistake: focusing only on the per-million-token input/output price. A real bill has three components: regular input tokens, output tokens, and cache-read tokens. The first two determine the visible cost of a single call, but the cache-read price and the total number of tokens required to complete a task are often the main reasons a bill explodes.

    A common pattern: a model with a lower unit price can deduct far more account quota than a higher-priced peer in multi-turn tool-calling and long-context code comprehension scenarios. The reason is not mysterious—token consumption multipliers for the same task vary widely across models, and the unit-price advantage is completely offset by the quantity disadvantage.

    2. Billing Model Abstraction: Turning the Bill into a Computable Formula

    Setting aside specific products, the cost of any LLM API can be abstracted as:

    1. cost per call = input tokens/1e6 * P_in
    2. + output tokens/1e6 * P_out
    3. + cache-read tokens/1e6 * P_cache
    4. + cache-write tokens/1e6 * P_cache_write (if any)

    Three variables deserve the most attention:

    1. Cache-read unit price P_cache: in repeated long-context calls, its weight can exceed the input price itself.
    2. Task token multiplier k: the ratio of total tokens consumed by different models to complete the same task.
    3. Cache hit rate h: the share of input served from cache, which directly determines the actual scale of the P_cache term.

    So effective cost can be approximated as:

    1. effective cost ≈ (1-h) * L * P_in + h * L * P_cache + L * k * P_out

    where L is the logical length of the task. When h is high, changes in the absolute value of P_cache are amplified; when k is large, even a low unit price cannot save the total.

    3. Test Methodology: How to Run a Credible Side-by-Side Comparison

    To avoid being misled by price tables, run a reproducible test as follows:

    • Fix the task set: prepare 20–50 real business samples covering short Q&A, long-document summarization, and multi-turn tool calling.
    • Standardize context length: bucket input length (e.g., 2K/8K/32K) and measure each separately.
    • Record three metrics: input tokens, output tokens, and cache-read tokens—not just the dollar amount.
    • Compute two core indicators: average total tokens per task and cache hit rate.
    • Normalize the comparison: draw conclusions from “total cost to complete the same task set” rather than unit price.

    Sample measurement script:

    1. def normalize_cost(records):
    2. total = 0.0
    3. for r in records:
    4. total += r["in"]/1e6 * r["p_in"]
    5. total += r["out"]/1e6 * r["p_out"]
    6. total += r["cache_read"]/1e6 * r["p_cache"]
    7. return total / len(records)

    4. Engineering Optimizations for Cache Hit Rate

    Cache-read price cuts are an industry trend, but the more fundamental optimization is raising the hit rate:

    1. Stable prefix: put the system prompt, tool definitions, and fixed knowledge blocks at the very front of the context, and avoid placing dynamic content first, which invalidates the prefix.
    2. Reduce meaningless variation: timestamps, random IDs, and user nicknames should be moved to the end or removed.
    3. Segmented reuse: split long documents into reusable blocks and inject only the necessary fragments with vector retrieval.
    4. Monitor the hit rate: add cache hit rate to monitoring alerts and the log service dashboard, and investigate immediately on any sharp drop.
    1. [fixed system prompt][fixed tool schema][fixed knowledge block] | [dynamic conversation content]

    5. Selection Evaluation Checklist

    • Is the price structure transparent, with cache reads and writes priced separately?
    • Is the task token multiplier acceptable?
    • Is a stable prefix-caching mechanism supported?
    • Are usage monitoring and quota alerts complete?
    • What is the integration cost within existing architectures such as container platforms and message queues?

    6. Conclusion

    The right approach to model selection is to upgrade from “unit price comparison” to “total task cost comparison.” On platforms such as Baidu AI Cloud, developers can build a cost dashboard using common capabilities like monitoring alerts and log services, and make decisions with data rather than impressions—avoiding the trap of “cheaper unit price, higher bill.”