LLM API Bill Optimization: Four Paths to Different Costs at the Same Unit Price

    This article explores why identical unit prices for Large Language Model (LLM) APIs often result in significantly different monthly bills. By analyzing the fundamental composition of API billing—including input, output, and caching tokens—it provides reusable calculation methods and Python code. The article further examines key billing rules such as offline batch APIs, subscription plus overage models, and time/region-based pricing, offering practical guidelines for optimizing API costs.

    1. Same Unit Price, Different Bills

    Pay-as-you-go unit prices for the same model are often identical across different service channels. Many teams assume it doesn’t matter which provider they choose, only to find their monthly bills differ by up to 50% for the exact same usage volume. The reason is that the unit price is merely one input factor for the bill. The actual cost is determined jointly by billing rules, invocation methods, request structure, and time distribution.

    This article focuses on the billing mechanism itself, offering reusable calculation methods and a selection process. The numbers used in this article are hypothetical parameters for demonstration; please refer to the official pricing pages of each service for actual prices.

    2. Basic Composition of Billing

    Most LLM API costs can be broken down into four types of usage:

    • Normal Input Tokens: The part of the prompt that misses the cache.
    • Cache Write Tokens: The part of the prompt written to the cache for the first time. Some services differentiate write unit prices based on cache validity periods.
    • Cache Hit Tokens: The part of the prompt reused when a prefix is repeated; the unit price is usually significantly lower than normal input.
    • Output Tokens: The content generated by the model, usually with the highest unit price.

    The general formula for monthly cost is:

    Monthly Cost = Σ(Normal Input Volume × Input Unit Price) + Σ(Cache Write Volume × Write Unit Price) + Σ(Cache Hit Volume × Hit Unit Price) + Σ(Output Volume × Output Unit Price)

    Representing this formula in code makes it easy to batch-test different schemes:

    1. from dataclasses import dataclass
    2. @dataclass
    3. class Price:
    4. input: float # Cost / Million Tokens
    5. cache_write: float # Cost / Million Tokens
    6. cache_hit: float # Cost / Million Tokens
    7. output: float # Cost / Million Tokens
    8. @dataclass
    9. class Usage:
    10. input_tokens: int
    11. cache_write_tokens: int
    12. cache_hit_tokens: int
    13. output_tokens: int
    14. def usage_cost(usage: Usage, price: Price, discount: float = 1.0) -> float:
    15. """discount is the discount coefficient applied to the standard unit price by rules like batch or subscription, 1.0 means no discount."""
    16. million = 1_000_000
    17. base = (
    18. usage.input_tokens * price.input
    19. + usage.cache_write_tokens * price.cache_write
    20. + usage.cache_hit_tokens * price.cache_hit
    21. + usage.output_tokens * price.output
    22. ) / million
    23. return base * discount

    Assuming a model’s unit prices are: input 10, cache write 12.5, cache hit 1, output 50 per million tokens. In a given month, the usage is 200 million input, 50 million cache write, 300 million cache hit, and 40 million output tokens. The costs are:

    • Normal Input: 200M × 10 / 1M = 2000
    • Cache Write: 50M × 12.5 / 1M = 625
    • Cache Hit: 300M × 1 / 1M = 300
    • Output: 40M × 50 / 1M = 2000

    Total: 4925. If an offline rule provides a 0.6 discount coefficient, the cost drops to 2955. With the exact same usage, just because of different billing rules, the bill differs by nearly 40%.

    3. Main Rules Affecting Bill Differences

    3.1 Offline Batch APIs

    Tasks such as data annotation, batch summarization, and nightly reports usually do not require immediate responses. These tasks are suitable for asynchronous batch APIs. Providers schedule batch tasks on idle computing power, often offering discounts lower than real-time calls.

    To determine if a task can be batchified, check three conditions: whether results can be returned within a few hours, whether retries can be handled by batch, and whether centralized processing of results is acceptable. Tasks meeting at least two of these criteria are worth evaluating for a batch solution.

    Things to confirm before use include: whether the discount covers all models, whether there are limits on single-batch file size and concurrency, how long results are retained, and whether failed tasks are billed as normal usage.

    3.2 Subscription Plus Overage Pay-as-you-go

    Subscription plans charge a fixed monthly fee that includes a certain quota, with overages billed on a pay-as-you-go basis. This combination is more beneficial for stable, high-volume businesses, provided the usage actually reaches the break-even point.

    Let the monthly subscription fee be F, the included quota be Q, the overage unit price be p, the pay-as-you-go unit price be p₀, and the monthly usage be U. The costs for both methods are:

    • Pay-as-you-go: U × p₀
    • Subscription plus overage: F + max(0, U − Q) × p

    The table below uses hypothetical parameters to demonstrate the difference (usage unit: million tokens; assuming F = 1000, Q = 100, p₀ = 10, p = 8):

    Monthly Usage (Million Tokens) Pay-as-you-go Subscription + Overage Conclusion
    80 800 1000 Pay-as-you-go is cheaper
    200 2000 1800 Subscription saves 10%
    300 3000 2600 Subscription saves ~13%

    It is clear that subscriptions only have an advantage when usage exceeds a certain threshold. The threshold can be derived from F + (U − Q) × p = U × p₀. Teams should perform this calculation based on logs from the past three months before purchasing, rather than choosing based on intuition.

    Note that changes in usage structure will alter the effective unit price. If usage is input-heavy in summer and output-heavy in winter, a fixed blended unit price will have different applicability across different months.

    3.3 Time and Region

    Some services offer price discounts for off-peak hours and set different unit prices across regions. For tasks that can be delayed, shifting scheduling to low-price periods is a cost-reduction method that almost does not affect quality.

    Region selection requires comprehensive consideration of three points: price differences, network latency, and data residency requirements. Cross-region calls increase latency; if the business is sensitive to response time, simply choosing the cheapest region may not be worth the trade-off. When personal information is involved…