LLM API Pricing and Token Cost Optimization in Practice

    This article explains how large language model APIs price input, output, and cached input tokens, why those rates differ, and how to compare model versions on cost and capability. It also covers a simple cost estimate, routing tasks across model tiers, and engineering steps that raise cache hits and cut token spend.

    1. Background: Token Billing Is Now a Core Cost of LLM Applications

    As large language models move into production at scale, API spend has shifted from a minor line item to a core cost alongside compute and labor. Most cloud and model providers bill by tokens, with separate rates for input tokens, output tokens, and cached input tokens. Understanding how that billing model is designed is the starting point for any cost plan.

    Recent model releases show a clear pattern: newer versions often improve capability while lowering both input and output prices, and cached input tokens are discounted even more aggressively—sometimes by more than half. That reflects more mature inference infrastructure, especially wider reuse of KV cache, which lowers marginal serving cost and leaves room to cut prices.

    2. Why the Three Token Types Are Priced Differently

    2.1 Input Tokens: The “Read” Cost of a Request

    Input tokens are everything sent with a request: system prompts, conversation history, retrieved document chunks, and so on. They are usually priced well below output tokens, often on the order of 1:5. Output is autoregressive: each new token requires a full forward pass, so generation is far more compute-heavy than encoding the input once.

    2.2 Output Tokens: The Compute-Heavy “Write” Cost

    Output tokens are billed by how many are generated, and they dominate cost in long-form generation and code completion. In a typical price structure, if input is a few dollars per million tokens, output is often about five times that. For applications that need long model output, shortening the response often saves more than compressing the input.

    2.3 Cached Input Tokens: The Most Overlooked Cost Lever

    Cached input tokens are the prefix of a request that matches a prefix already cached on the server. System prompts, few-shot examples, and long document context are highly repeated across calls, so the server can reuse computed KV cache and cut inference cost sharply. That saving is usually passed through at a very low rate—often about one-tenth of the standard input price, or less. For high-volume apps with a stable prompt prefix, enabling prefix caching can reduce cost by an order of magnitude.

    3. How to Compare Prices When Models Are Updated

    When a new model generation ships, compare more than the headline unit price:

    1. Compare each price dimension. Measure the change in input, output, and cached-input rates separately. If input and output each fall 20% while cached input falls 60%, the provider has invested more in caching, which favors workloads with highly repeated prefixes.
    2. Normalize cost on real traffic. Build typical requests from production traffic and estimate combined cost per thousand calls instead of comparing unit prices alone.
    3. Weigh capability against price. Use benchmark results to see how much task quality you get per unit of spend, and avoid paying for capability the product does not use.

    A simple cost estimate looks like this:

    1. def estimate_cost(calls, input_tokens, output_tokens,
    2. cached_ratio, p_in, p_out, p_cache):
    3. cached = input_tokens * cached_ratio
    4. fresh = input_tokens - cached
    5. return calls * (fresh / 1e6 * p_in
    6. + cached / 1e6 * p_cache
    7. + output_tokens / 1e6 * p_out)
    8. # Example: monthly cost when 80% of the prefix hits cache
    9. cost = estimate_cost(
    10. calls=1_000_000, input_tokens=2000, output_tokens=500,
    11. cached_ratio=0.8, p_in=8, p_out=40, p_cache=0.4
    12. )

    4. Match Model Tiers to the Workload

    Mature model offerings usually span several lines: a high-capability model for hard reasoning, a lightweight model for high-frequency calls, and tiers in between. On common price ladders, a lightweight model may cost about a quarter of the flagship. A practical approach is model routing:

    • Hard tasks (multi-step reasoning, agent planning, code architecture) go to the high-capability model.
    • Simple tasks (classification, extraction, format conversion, short summaries) go to the lightweight model.
    • Unclear cases can start on the lightweight model, with a confidence threshold that escalates low-confidence results to the stronger model.
    1. def route(prompt):
    2. if classify_complexity(prompt) > THRESHOLD:
    3. return premium_model
    4. return lite_model

    This split usually keeps overall quality while cutting combined cost by about 30% to 60%.

    5. Engineering Checklist for Lower Token Cost

    1. Maximize cache hits. Put static content—system prompts, tool definitions, and similar material—at the start of the prompt and keep it byte-for-byte identical. Place dynamic content at the end.
    2. Limit context length. Summarize conversation history and filter retrieved content by relevance so unrelated tokens never enter the request.
    3. Constrain output length. Use explicit format requirements and structured output (for example, a JSON schema) to cut redundant generation.
    4. Batch and process asynchronously. Non-real-time work can use batch APIs, which often come with an extra discount.
    5. Monitor cost. Record token use by business module and by user, set budget alerts, and catch abnormal call patterns early.

    6. Conclusion

    LLM API pricing is still moving quickly: unit prices keep falling, cache rates are dropping further, and product tiers are becoming clearer. Rather than treating the bill as fixed, developers can design around the billing structure—cache reuse, model routing, and context engineering. Putting cost into the technical design is what makes capability and spend sustainable together.