How to Calculate the True Cost of LLM API Calls After a Price Increase

    When a large model API provider raises its per-token prices, the sticker price alone doesn't tell you how much your costs will actually change. This article breaks down the billing dimensions that matter—uncached input, cached input, and output tokens—and shows how to build a simple cost model, analyze how usage patterns amplify or offset price changes, and apply engineering practices like prefix caching, context trimming, output limits, model routing, and response caching to reduce the cost per task before and after migrating to a new model.

    1. Unit Price Changes Are Not Cost Changes

    A price list only tells you the cost per million tokens. The actual cost of a task depends on how many input tokens and output tokens it uses, how many of those tokens hit the cache, and how many API calls the task requires. A service whose price jumps fivefold may see a much smaller increase in total cost if its cache hit rate is high and outputs are short. Conversely, long contexts, multi-turn conversations, and verbose outputs amplify the impact of a price change.

    So when evaluating a price adjustment, first break the unit price back down into billing dimensions, then calculate the impact based on your actual usage profile.

    2. Breaking Down the Common Billing Dimensions

    Most LLM services bill by token, mainly across three categories:

    1. Uncached input tokens: Content sent to the model for the first time in this request, usually at the highest input price.
    2. Cached input tokens: Content whose prefix matches a previous request and is reused server-side, usually priced significantly below regular input.
    3. Output tokens: Content generated by the model, usually priced above input and slower to produce.

    Some services also bill thinking/reasoning tokens, tool call results, or images separately. Always check each platform’s official billing documentation for exact rules.

    3. Building a Cost Model

    The following function estimates the cost of a single call. Replace the price parameters with values from the actual billing documentation.

    1. def estimate_cost(input_tokens, output_tokens, cached_tokens,
    2. price_in, price_out, price_cache):
    3. """
    4. Prices are in dollars per million tokens (or any consistent unit).
    5. cached_tokens is the number of input tokens served from cache;
    6. it must be less than or equal to input_tokens.
    7. """
    8. uncached_tokens = input_tokens - cached_tokens
    9. cost = (
    10. uncached_tokens * price_in
    11. + cached_tokens * price_cache
    12. + output_tokens * price_out
    13. ) / 1_000_000
    14. return cost

    Here is a demo scenario illustrating the effect of a price change. Suppose a task uses 200K input tokens and 5K output tokens. The unit prices of both plans are hypothetical for illustration and don’t correspond to any specific provider:

    Cache hit rate Plan A (cost) Plan B (cost) Ratio
    0% 0.44 2.25 ~5.1x
    75% 0.17 0.90 ~5.3x
    90% 0.12 0.63 ~5.4x

    Plan A prices input at 2, output at 8, and cached input at 0.2 (per million tokens, in any currency unit). Plan B prices input at 10, output at 50, and cached input at 1—each item is roughly five times Plan A’s price.

    In this example, the total cost ratio is close to the unit price ratio because the price proportions of the two plans are nearly identical. If the ratio of cached price to uncached input price differs between the two plans, or the ratio of output price to input price differs, the total cost ratio will deviate from the unit price ratio. That’s why, before making a migration decision, you should plug your own real request logs into the formula rather than relying on the ratio from the price list.

    4. How Usage Patterns Amplify or Offset Price Changes

    1. Cache hit rate. The higher the hit rate, the lower the input cost and the larger the share of output cost in the total. In that case, reducing output length is more effective than further compressing input.

    2. Context length. Multi-turn conversations resend history as input on every turn. If each turn re-concatenates a large amount of irrelevant history, cost grows with each round.

    3. Output length. Output is usually priced higher and takes longer to generate. Setting a reasonable output length cap controls cost and also reduces latency.

    4. Number of calls. If a workflow includes multiple model calls, small changes in per-call cost get multiplied. For example, when review, rewriting, and summarization are chained together, consider merging or trimming intermediate steps.

    5. Dimensions to Weigh in Model Selection

    Choosing on price alone ignores other costs. Evaluate candidates along these dimensions:

    • Quality and stability: Test accuracy, format compliance, and refusal rates on your own evaluation set, not just on public leaderboards.
    • Latency: Time-to-first-token and overall generation time affect user experience and the resources needed for concurrency.
    • Context window: Long-document processing needs a sufficient context length, but larger windows often come with higher cost and slower responses.
    • Feature support: Whether structured output, tool calling, and multimodal input are supported determines whether your use case can be implemented directly.
    • Compliance and data policy: Data retention, cross-border transfer, and training data usage terms should be confirmed with your legal team.

    If your team uses an LLM service from Baidu AI Cloud, verify each billing unit, caching rule, and model version mapping against the official documentation before adding it to your cost model.

    6. Engineering Practices to Reduce Cost per Task

    1. Keep prefixes stable to improve cache hit rates. Caching typically applies to identical content at the start of a request. Placing system instructions, fixed examples, and style guides at the front and variable user input at the end can significantly improve hit rates.

    1. def build_messages(system_prompt, shared_examples, user_query):
    2. # Stable content first, dynamic content last, so the server can reuse the prefix cache
    3. return [
    4. {"role": "system", "content": system_prompt + "\n" + shared_examples},
    5. {"role": "user", "content": user_query},
    6. ]

    2. Trim context. Keep only history relevant to the current question, and replace older conversation turns with a summary beyond a certain number of turns. The summary itself should also be kept short.

    3. Cap output length. Set a maximum output token limit in the request, and specify the expected output format explicitly in the prompt to avoid redundant explanations.

    4. Tiered routing. Route simple tasks to low-cost models and escalate only complex tasks to high-capability models. Routing rules can be based on task type, input length, or a preliminary classification step.

    1. def route_model(task_type, input_tokens):
    2. if task_type == "classification" and input_tokens < 2000:
    3. return "small_model"
    4. if task_type in ("reasoning", "code_generation"):
    5. return "large_model"
    6. return "medium_model"

    5. Cache results. For scenarios where identical inputs recur—such as fixed Q&A or standardized queries—cache the final result and skip the model call entirely.

    6. Batch processing. For offline workloads like bulk labeling or offline summarization, batch requests and run them during off-peak hours to reduce scheduling overhead. Whether batch discounts are available depends on the provider’s policies.

    7. A Validation Process Before Rollout

    Before migrating or switching models, validate with these steps:

    1. Sample real requests: Extract one week of traffic from production logs and measure the distribution of input, output, and cached tokens.
    2. Plug into the cost model: Use the formula above to compute per-task cost and monthly total cost under both the old and new plans.
    3. Evaluate quality offline: Compare output quality between the two plans on the same sample set. If cost savings come with quality degradation, factor that into the overall assessment.
    4. Canary rollout: Switch a small percentage of traffic first, monitoring cost, latency, and error rates.
    5. Set alerts: Configure thresholds for daily spend, per-task token counts, and cache hit rates, and notify the responsible team when anomalies occur.

    8. Conclusion

    Whether an LLM API price change affects your business depends on your call structure, not the price list itself. Break down the billing dimensions first, build a cost model from real logs, and then choose a model while weighing quality, latency, and compliance requirements. On the engineering side, stable prefixes, trimmed context, output limits, tiered routing, and result caching are the most common and fastest-acting ways to reduce cost per task.