LLM Token Billing Explained: Principles and Cost Optimization Practices
This article explains how large language models tokenize text, why input and output tokens are priced separately, and how to estimate request costs with a simple formula. It also covers hidden token consumption from system prompts, conversation history, RAG, retries, and structured output, followed by practical engineering techniques for reducing costs through prompt trimming, output limits, context governance, model routing, batching, observability, and result caching. Finally, it outlines budgeting and governance practices for treating tokens as a managed cloud resource.
1. What a Token Actually Is: The Model’s Smallest Text Unit
A token is the basic unit a large language model uses to process text. It is not the same as a character or a word. Instead, it is a subword fragment produced by a tokenizer based on statistical patterns in the training corpus. A common pattern: an English word may be split into 1–3 tokens, a common Chinese character usually maps to roughly one token, and punctuation, spaces, and line breaks also consume tokens. Symbols and indentation in code are counted as well.
A simplified view of the counting logic:
raw text -> tokenizer splits -> token sequence -> count and billing"今天天气不错" -> ["今天", "天气", "不错"] -> 3 tokens
Three points are worth emphasizing. First, the token count is determined by the specific tokenizer, and you cannot simply convert between model families by ratio. Second, context window limits and maximum output length per request are both measured in tokens. Third, tokens are bidirectional: both the prompt you send and the response the model generates are counted.
2. Why Input and Output Are Priced Separately
Major LLM providers use a highly consistent billing model: metered by token, with input (prompt) and output (completion) priced separately, and the output rate is usually significantly higher than the input rate. The reason lies in computational characteristics. The input phase can be processed in parallel, with a single forward pass covering the entire prompt. The output phase is autoregressive decoding, generating one token at a time while continuously reading the growing context and key-value cache, which puts more pressure on memory bandwidth. Pricing the two workloads separately reflects actual resource consumption and makes costs more predictable.
In addition, many platforms offer discounted pricing for cache hits: when a request prefix closely matches previous requests, the system can reuse already-computed intermediate results, and the input rate for the matched portion drops substantially; otherwise standard pricing applies. When budgeting, using the “cache miss” case as the baseline is the safer approach.
3. Doing the Math: A Standard Way to Estimate Cost
Let the input rate be p_in (per 1,000 tokens), the output rate be p_out, the input token count be N_in, and the output token count be N_out. The cost of a single request is:
Cost = N_in / 1000 * p_in + N_out / 1000 * p_out
Take a Chinese question of about 500 characters and a Chinese answer of about 1,000 characters. A rough estimate is N_in ≈ 700 and N_out ≈ 1,400 (Chinese tokenization and punctuation inflate the count). If a platform charges 0.5 per 1,000 input tokens and 2 per 1,000 output tokens, then:
input: 0.7 * 0.5 = 0.35output: 1.4 * 2 = 2.80total: about 3.15 per request
The conclusion is straightforward: output accounts for the vast majority of the cost. With 10,000 similar requests per day, the monthly cost reaches the order of a million. Always confirm real prices on the pricing page of the cloud provider you choose, and do not reuse numbers from someone else’s screenshot.
4. Hidden Consumption That Is Easy to Miss
- System prompts: carried on every request; long prompts become costly under high concurrency.
- Multi-turn conversation history: context accumulates with each turn, and token count grows roughly quadratically.
- Retrieval-augmented generation (RAG): retrieved passages are spliced into the prompt in full, making them a major source of long contexts.
- Retries and failed requests: timeout retries are billed too, so budget some redundancy.
- Structured output and chain of thought: a longer intermediate process means more output tokens.
5. Engineering Practices for Cost Optimization
- Trim prompts: remove redundant examples and turn stable long instructions into a reusable prefix to improve cache hit rates.
- Control output length: set explicit limits on length or fields, and use stop sequences to terminate early when needed.
- Govern context: summarize and compress history; in RAG scenarios, rerank first and then truncate, keeping only highly relevant passages.
- Layered routing: send simple tasks to smaller models or a rules engine, and escalate complex tasks to a large model.
- Batching and asynchronous processing: merge requests for offline jobs, avoid peak hours, and improve throughput and cost efficiency.
- Observability: use logging services and monitoring alerts to track token consumption by business dimension, and set quotas and budget alerts.
- Cache semantic results: cache final answers for frequent identical questions to eliminate repeated inference cost outright.
6. Budgeting and Governance Advice for Enterprises
In practice on cloud platforms such as Baidu AI Cloud, treat tokens as a resource on par with CPU and storage in cost management: set a monthly token quota for each business line, account for input and output separately, and use monitoring alerts to warn as thresholds approach; put prompt templates, knowledge base slices, and model parameters under version control, and for any change, run a small-traffic canary first and compare cost per request; for cost-sensitive paths, prefer combinations of general-purpose product categories, such as object storage for holding raw knowledge base content, a message queue for peak shaving, a container platform for hosting inference services, and logging and monitoring systems for metering and auditing. This keeps quality intact while making the bill explainable, attributable, and optimizable.
Summary: understanding the tokenization nature of tokens and the separate pricing of input and output is the first step to controlling LLM costs. Formalizing the estimation formula, making consumption visible, and engineering the optimization are what allow a stable balance between quality and budget.
