LLM API Cost Accounting: From Unit Prices to Per-Task Cost Breakdowns
A practical guide to estimating LLM API costs. It explains how token-based billing works, how to convert unit prices into per-task costs, how caching and multi-turn or agent workflows affect spending, and how to compare pay-as-you-go billing with subscription plans.
1. Why Rethink LLM Pricing
LLM services are priced differently from traditional software licenses. Traditional software is charged per seat or per license, so its costs are relatively fixed. LLM services are billed per usage, so costs grow with business volume. When teams plan budgets, looking only at unit prices can easily lead to underestimating or overestimating actual spending.
This article starts from a general pricing structure. It explains how to convert unit prices into task costs, how to compare pay-as-you-go billing with subscription plans, and how to choose a model based on cost-effectiveness rather than a single price. The prices in this article are illustrative. For actual figures, refer to the provider’s published price list.
2. Billing Units: Tokens and Billing Dimensions
Most LLM services use tokens as the unit of measurement. A token is the smallest processing unit into which a model splits text. A Chinese character typically corresponds to one to two tokens, and an English word typically corresponds to one to two tokens. The exact splitting rules depend on each model’s tokenizer, so the same text may produce different token counts on different models.
Common billing dimensions fall into three categories:
- Input tokens: the system prompt, conversation history, retrieval results, and user question in a request.
- Output tokens: the text the model generates in its response. Output prices are usually higher than input prices because generation requires decoding one token at a time.
- Cached input tokens: the request prefix matches a previous request, so the service reuses intermediate results it has already computed. These tokens are usually billed at a lower price than regular input tokens.
Understanding these three dimensions matters. The same number of tokens can cost several times more or less depending on whether it counts as input, output, or cached.
3. From Unit Prices to Per-Task Cost
A unit price alone does not answer the key question. Businesses need to know how much it costs to complete a task. A task may consist of a single request, or it may include multiple conversation turns, tool calls, and retries after failures.
The cost of a single request can be calculated with the following formula:
Single request cost = input tokens × input price + output tokens × output price + cached tokens × cached price
Unit prices are usually expressed as price per million tokens, so all units must be consistent during calculation. In the code example below, the prices are placeholders. Replace them with the values from the official price list.
PRICE_PER_M = {"input": 0.0, # Input price (CNY per million tokens). Replace with the value from the official price list."output": 0.0, # Output price (CNY per million tokens)"cached": 0.0, # Cached input price (CNY per million tokens)}def request_cost(input_tokens, output_tokens, cached_tokens, price=PRICE_PER_M):"""Calculate the cost of a single request in CNY.cached_tokens is a subset of input_tokens. Subtract it from the input first to avoid double counting."""uncached = input_tokens - cached_tokensif uncached < 0:raise ValueError("The number of cached tokens cannot exceed the total number of input tokens")return (uncached * price["input"]+ cached_tokens * price["cached"]+ output_tokens * price["output"]) / 1_000_000
A common mistake is treating cached tokens as extra tokens on top of the input. Cached tokens are part of the input tokens, not an addition to them. Adding the two together double-counts the cost.
Amplification in Multi-Turn Conversations and Agent Workflows
In multi-turn conversations, each request usually includes the full history from earlier turns. The input size for turn n grows cumulatively with n, so the total input across a session grows faster than linearly. In tool-calling scenarios, a single task may trigger several model calls, and each call carries the full context. When estimating task cost, count the entire chain of calls rather than only the final response.
4. How Caching Affects Cost
A cache hit requires the request prefix to stay identical. The service typically matches requests from the beginning, segment by segment. Once the content at some position differs, everything after that point cannot be reused.
Based on this, engineers can take the following steps:
- Place stable content at the beginning, including system instructions, role settings, fixed tool definitions, and reference documents.
- Place variable content at the end, including the user’s current question and dynamic retrieval results.
- Keep timestamps, random IDs, and results whose order changes between calls out of the fixed prefix, because they break matching.
- Keep request templates consistent across the same business use case, and log the cache hit rate to verify that the design works.
Cache hit rate is one of the most direct metrics for cost optimization. As the hit rate rises, input costs usually fall. The size of the reduction depends on the share of the prefix within the total input and on the price gap between cached and regular input tokens.
5. Comparing Pay-as-You-Go and Subscription Plans
Many services offer both pay-as-you-go billing and subscription plans. A subscription plan usually charges a fixed monthly fee and includes a set quota. To compare the two, do not look only at the monthly fee or the quota figure. Convert the quota into the number of tasks it can complete.
The calculation is:
Equivalent task count = equivalent monetary value of the quota included in the plan ÷ cost per task
The same plan can yield very different equivalent task counts across models. Unit prices differ between models, and completing the same task consumes different numbers of tokens on each one.
Quota rules vary by provider. Some count requests, some count tokens, some count equivalent monetary value, and some apply rate limits over time windows. Before comparing plans, confirm how each one measures its quota, then estimate with real task samples.
