LLM Token Counting and Cost Control: How Tokenization Drives Inference Pricing
This article explains what a token actually is in the context of large language models, how tokenizers split text into the integer sequences a model consumes, and why rough "one character, one word" estimates fail as a billing basis. It breaks down how input, output, and resent history accumulate into total usage, why agentic workflows are especially token-hungry, and offers practical techniques for measuring, budgeting, and trimming token consumption across chat and agent pipelines. A vendor-neutral budgeting pseudocode example and a deployment checklist are included.
From Dictionary Meaning to Model Metering
In everyday English, a token can mean a mark, a voucher, or something symbolic. In computing, the word came to describe login credentials, access tokens, and other “small verifiable pieces of information.” In the large language model context, the meaning narrows again: a token is the smallest discrete unit the model reads and generates, and it is also the unit most inference services use for usage-based billing.
It is neither “one Chinese character” nor “one English word.” What the model sees is a sequence of integers produced by a tokenizer. The same sentence yields a different token count under a different vocabulary and splitting rules. So before discussing cost, you have to discuss splitting.
What the Tokenizer Is Doing
Training corpora are first counted into high-frequency fragments, which are then placed into a vocabulary. A common approach starts from characters or bytes and repeatedly merges frequently adjacent fragments until the vocabulary reaches a target size. Widely used subword methods include byte pair encoding and its byte-oriented variants. During inference, the tokenizer maps the original text to token IDs; the model computes only over the IDs; and after generation, the IDs are decoded back into text.
The flow can be sketched as:
raw text → normalization (case, whitespace, special symbols) → greedy or constrained splitting against the vocabulary → token id sequencetoken id sequence → model computation → new token ids → decoded back to visible text
Splitting results are affected by four factors: whether the vocabulary covers the common characters and words of the language, whether punctuation and whitespace form standalone units, whether numbers and code get split into many pieces, and whether rare proper nouns fall back to single characters or even single bytes. Spaces, line breaks, emoji, and special markers usually consume tokens too. System prompts, tool descriptions, and prior messages all enter the sequence as well, even if the user interface does not display them.
Why “One Character, One Word” Estimation Fails
Approximating “one Chinese character, one punctuation mark, one English word” as a single token is fine for a rough verbal sense of scale, but it cannot serve as a billing basis. In English, common words often map to one or a few subwords, while long-tail words and camelCase identifiers get split apart. Chinese characters are close to “one character, one token” in many general-purpose vocabularies, but phrases, rare characters, mixed English, and code push the ratio noticeably away from 1. Punctuation is sometimes merged into an adjacent fragment and sometimes occupies its own slot.
Empirically, under the same vocabulary, a prompt written mostly in Chinese characters often consumes more tokens than a semantically equivalent English prompt, because English reuses word-level fragments more, while Chinese is more often counted per character. The gap varies with the tokenizer and text type and cannot be fixed as a specific percentage. To compare costs, measure the same text with the counting endpoint provided by the target service or with an identical local tokenizer, rather than multiplying character count by a constant.
How Input, Output, and Context Stack Up
The total usage of a single call usually includes at least:
- Input tokens: system instructions, the user’s question, retrieved material, tool definitions, and any necessary history;
- Output tokens: the content newly generated in this call, including the reasoning process if it is explicitly emitted;
- Resent history: if a multi-turn conversation sends the full transcript back each time, every earlier turn is billed again in subsequent requests.
Most services price input and output separately. The output unit price is higher because the generation stage samples step by step, running a forward pass for every token produced, whereas the input stage can encode the entire prompt in one parallel pass. Long contexts also add the overhead of attention computation rising with sequence length, and some platforms apply a higher price tier or a hard cutoff in the very-long range. These rules are the commercial policy of a specific service and cannot be derived from the concept of a “token” into a universal price list.
The order of magnitude of usage can be understood like this, which helps with budgeting rather than producing an exact bill: a very short Q&A is often tens to a couple hundred tokens; everyday dialogue with a few turns of context often reaches the hundreds; rewriting, summarization, and simple analysis easily reach hundreds to thousands; feeding in a long document, tables, and multiple pieces of material together often goes into the thousands and beyond. When an agent breaks one goal into planning, retrieval, tool calls, checking, and retries, each step is one or more model calls and often carries accumulated context, so total consumption can be an order of magnitude or more higher than a single Q&A. The exact numbers must come from the usage receipt of the actual request.
Why Agents Are Especially Token-Hungry
The cost of a single-turn conversation is roughly proportional to “question length + answer length + attached history.” An agentic flow further multiplies this by the number of steps and failed retries. Typical amplification comes from:
- Task decomposition turns one user instruction into multiple rounds of model decisions;
- Every step resends the system prompt, tool schema, and prior observations;
- Tool-returned web pages, logs, or tables are often far longer than the user’s original words;
- Without a stop condition, the model keeps “checking once more.”
So “keeping an agent that works on its own” is expensive usually not because any single answer is especially long, but because context is repeatedly read and written inside the loop. Capping the number of steps, the tool return length, and whether the full transcript is resent is more effective than changing a single prompt sentence.
Actionable Estimation and Trimming
Before going live, fix a measurement method rather than topping up by feel.
Measure first, then scale. Use representative samples (short questions, long Chinese text, mixed Chinese-English, code, tables) to count input and output tokens separately, then estimate monthly usage from daily active turns, average turns, and retry rate. Chinese-language workloads should be sampled separately, not extrapolated from English samples.
Cache or externalize the invariant parts. Keep the stable system persona, format rules, and tool descriptions short, and do not re-paste them in natural language every turn. Put retrievable material in external storage and inject only the fragments relevant to the current question, not the entire corpus.
Constrain the output shape. Require itemized output, a maximum word or token count, and no restating of the source. Output is often more expensive, and an unconstrained “be as detailed as possible” directly inflates the bill.
Manage history. A sliding window keeps only the most recent few turns; older content is compressed into a short summary. The summary itself consumes tokens but is usually cheaper than resending the full transcript repeatedly. Start a new session when the topic switches, so unrelated history is not bound into the same context.
Give the agent a budget. Set a maximum number of steps, a maximum tool-return character count, and a maximum total token count per task. On failure, return the results so far and request human confirmation instead of retrying indefinitely. Extract from tool results before sending them back to the model.
Distinguish counting conventions. Some interfaces show “user-visible character count” while the bill is by tokens; some count the system prompt as input while the interface does not display it. For reconciliation, use the usage fields returned by the server and keep request identifiers for spot-check review.
Below is vendor-neutral budgeting pseudocode that expresses only the control logic:
budget = max tokens allowed for the taskmessages = [short system prompt, current user goal]for step in 1..max_steps:if estimated(messages) > budget * 0.7:messages = compress(messages) # summarize old observations, drop raw textreply, usage = model(messages, max_output_tokens=step_cap)budget -= usage.input + usage.outputif reply.done or budget <= 0:breakobservation = tool(reply.action, max_chars=obs_cap)messages.append(truncate(observation))
The Boundary with Login Tokens and On-Chain Tokens
The same English word refers to different things across industries. In access control, a token is an authentication credential; leaking it leads to privilege escalation, so it must be kept secret, short-lived, and least-privileged. On a distributed ledger, a token usually means some digital right or unit of account, involving assets and compliance, not text splitting. In a large language model, a token is an ID for a fragment of text. Do not mix the three in architecture documents; external pricing, permission design, and ledger design should use complete terminology, and avoid reading “token cost” as a credential fee or a coin fee.
A Deployment Checklist
- Have you measured Chinese, English, and code samples with the target tokenizer, rather than estimating with “one character, one token”;
- Are input and output budgeted separately, and does a long answer have a length cap;
- Do multi-turn scenarios resend the full transcript history;
- Is retrieved content injected only as relevant fragments, and are tool returns truncated;
- Does the agent have step, retry, and total-token limits;
- Is monitoring broken down by application, user, and task type to spot abnormal loops;
- Do the docs state that “token” in this system means a splitting unit, not a login credential.
Token cost enters everyday technical spending along with call volume, much like other usage-based computing services. Managing it well does not rely on slogan-style “save words,” but on clear splitting rules, measuring real sequence lengths, and actively trimming context across the chat and agent pipeline.
