LLM API Pricing: Cost Accounting and Model Selection for Enterprises

    Learn how to estimate LLM API costs from token-based billing, identify hidden cost drivers, and choose models by cost per successful task. The article also covers practical engineering methods for routing, caching, output control, and usage monitoring.

    1. What Price Changes Signal for the Industry

    Over recent months, several model providers have adjusted their API pricing. Some products now cost noticeably less than comparable flagship models. Others kept their prices but emphasize that each token can accomplish more work. A few products sit above the low-price tier but offer discounts. For technical teams, price changes are a reason to revisit the cost model. A lower unit price does not automatically lower total cost. Final spend depends on call volume, the token consumption structure, and the task success rate.

    2. Understanding the Basic Structure of LLM Billing

    Most pay-as-you-go model services measure usage in millions of tokens and price input and output separately.

    • Input tokens: Everything sent to the model, including the system prompt, conversation history, and retrieved document passages.
    • Output tokens: The response the model generates. Output tokens are commonly priced higher than input tokens.
    • Cache hits: Some services charge a lower rate when the same prefix is reused.
    • Context length tiers: Some services set different price bands based on the context length of each request.

    Understanding these four billing items is a prerequisite for estimating cost. Looking only at the list price per million tokens makes it easy to overlook the ratio between input and output.

    3. Building an Estimation Model for Per-Request and Monthly Cost

    Per-request cost breaks into three parts: input tokens not served from cache, input tokens served from cache, and output tokens. The function below implements this logic. Fill in the price parameters from the relevant provider’s documentation.

    1. def estimate_request_cost(
    2. input_tokens: int, # Total input tokens for this request (including cached tokens)
    3. output_tokens: int, # Output tokens for this request
    4. cached_tokens: int, # Number of input tokens served from cache
    5. price_in: float, # CNY per million input tokens
    6. price_out: float, # CNY per million output tokens
    7. price_cache: float, # CNY per million cached input tokens
    8. ) -> float:
    9. uncached = max(input_tokens - cached_tokens, 0)
    10. cost = (
    11. uncached * price_in
    12. + cached_tokens * price_cache
    13. + output_tokens * price_out
    14. ) / 1_000_000
    15. return cost

    Worked example. Assume a business handles 100,000 requests per day, with an average of 1,500 input tokens per request (excluding cache) and 400 output tokens. The input price is 2 CNY per million tokens, and the output price is 8 CNY per million tokens. These prices illustrate the calculation method only. They do not represent any actual quote.

    • Daily input volume: 100,000 × 1,500 = 150 million tokens (150 M tokens). Input cost: 300 CNY.
    • Daily output volume: 100,000 × 400 = 40 million tokens (40 M tokens). Output cost: 320 CNY.
    • Total daily cost: 620 CNY. Over 30 days, the monthly cost is about 18,600 CNY.

    Sensitivity analysis. If constraining the response format reduces average output from 400 to 300 tokens, output cost falls to 240 CNY. Daily cost becomes 540 CNY, and monthly cost becomes about 16,200 CNY, a reduction of about 13%. When output prices are high, limiting output length is often the fastest way to cut costs.

    4. Identifying Hidden Cost Variables

    An estimation model reflects only nominal usage. The following factors can push actual spending away from the forecast.

    1. Runaway output length: Unconstrained responses inflate output costs. Defining the response format, setting a maximum output length, and preferring structured output can keep this spending under control.
    2. Context bloat: In multi-turn conversations, if every turn resends the full history, input tokens grow with each round. Common mitigations include history summarization and sliding windows.
    3. Retries and failed requests: Retries caused by timeouts or format validation failures are billed again. Track the retry rate and include it in cost estimates.
    4. Reasoning token consumption: Some models generate internal reasoning before producing an answer. Whether those tokens are billed should follow the provider’s public documentation. Measure them separately in estimates.
    5. Rate limiting and queuing: When concurrency exceeds the quota, requests queue and end-to-end latency rises. Higher latency hurts user experience and can trigger extra retries and manual intervention.

    5. From Unit Price to Cost per Successful Task

    A model with a low unit price is not necessarily cheap. A more useful metric is cost per successful task:

    Cost per successful task = Total cost ÷ Number of successfully completed tasks

    Worked example. Candidate model A costs 1 CNY per call and has a 60% task success rate, so its cost per successful task is about 1.67 CNY. Candidate model B costs 1.5 CNY per call and has a 95% success rate, so its cost per successful task is about 1.58 CNY. Although B has the higher unit price, its cost per successful task is lower.

    Follow these steps when selecting a model:

    1. Sample real business traffic to build an evaluation set that covers common requests and edge cases.
    2. Measure each candidate’s quality, latency, and token consumption on the evaluation set.
    3. Calculate cost per successful task, including the overhead from retries and failures.
    4. Add manual review, operations, and migration costs to produce a total cost of ownership comparison.

    6. Practical Engineering Methods

    1. Route Tasks by Difficulty

    Simple tasks, such as classification, extraction, and format conversion, can go to a lightweight model. Complex reasoning and long-document analysis go to a more capable model. Routing logic can be implemented as follows:

    1. def route(task) -> str:
    2. if task.complexity == "low":
    3. return "light-model"
    4. if task.requires_long_reasoning:
    5. return "large-model"
    6. return "medium-model"

    Adjust routing rules based on evaluation results rather than fixing them based on experience alone.

    2. Use Caching to Reduce Repeated Input

    Place fixed content, such as system prompts, tool descriptions, and examples, at the start of the request, and keep that content stable to improve the cache hit rate. Dynamic content, such as user questions and real-time data, should come after the prefix.

    3. Constrain Output

    Specify the output structure in the prompt, set a maximum output length, and prefer structured fields such as JSON. This reduces output tokens, makes responses easier for programs to parse, and lowers retries caused by parsing failures.

    4. Set Up Usage Monitoring and Budget Alerts

    Record input, output, and cache-hit token counts by feature module, user, or tenant. Connect these records to a monitoring and alerting system, and set daily and monthly budget thresholds. When the unit cost of a feature rises unexpectedly, you can locate the cause quickly.

    5. Keep the Ability to Switch Providers

    Wrap model calls in a unified adapter layer so that business code does not depend directly on one provider’s API. Switching models then requires only a configuration change, followed by regression testing on the evaluation set and a gradual rollout.

    7. Risks to Watch

    • Price volatility: Prices change frequently, so cost models should be parameterized. After each price change, re-estimate costs and update budgets.
    • Data compliance: Inputs may contain personal information or trade secrets. Mask sensitive data according to internal policy, and confirm the provider’s data retention and training-use policies.
    • Quality regression: Validate any model change on the evaluation set before switching. Do not decide based on unit price alone. If a cheaper model increases rework, total cost can rise.

    8. Conclusion

    Lower prices benefit users, but engineering design determines the actual cost level. Build the billing model first, select models by cost per successful task, and keep optimizing through tiered routing, caching, output control, and usage monitoring. This approach keeps costs predictable as the business scales.