LLM Tokens Explained: What a Token Is and How Tokenization Works
This article explains what tokens are in large language models, how subword tokenization works, why tokens differ from words and characters, how Chinese tokenization behaves, and how token limits affect context windows, cost, and response speed. It also offers practical guidance for estimating and managing token usage.
Starting with an Error Message
Many people first encounter tokens when calling a model API and receiving an error such as “maximum token limit exceeded.” At that point, it is common to have a vague impression that a token is like an arcade coin: you put one in, and the model works. That analogy is not accurate. A token is the basic unit the model uses to process text. It determines how much content the model “sees” and how much input and output a single request can contain.
To understand the error, it helps to first understand how a model reads text.
Step 1: The Model Does Not Read Character by Character
Take a simple sentence such as “The weather is nice today.” When people read it, they naturally understand it in phrases such as “the weather,” “is nice,” and “today”; they do not split it into individual characters. A model processes text in a similar conceptual way, but the splitting rules are determined by data and algorithms, not by a dictionary.
Before processing text, the model splits the input into small pieces and then encodes and computes those pieces one by one. These pieces are tokens. A token is the model’s input unit and also the unit used for billing, length limits, and usage statistics.
Step 2: A Token Is Not the Same as a Word
The most common beginner mistake is to equate a token with a word. In practice, the two often do not match.
For English, consider these examples:
catis usually one token.catsmay be split into two tokens:catands.tokenizationis likely to be split into two tokens:tokenandization.
The same word can also be split differently in different contexts. Capitalization, leading or trailing spaces, and punctuation can all affect the split. Therefore, you cannot estimate token count reliably from word count alone.
Step 3: Splitting Is Based on High-Frequency Combinations
A common method used by models is subword tokenization. Byte Pair Encoding (BPE) is one of the more common algorithms in this category. Its core idea can be summarized as follows:
- Count the most basic character sequences from a corpus.
- Repeatedly find the most frequent adjacent pair of pieces and merge them into a new unit.
- Add the merged units to the vocabulary until the vocabulary reaches a preset size.
In the resulting vocabulary, high-frequency roots, affixes, and common words are kept as whole units, while rare words are broken into shorter pieces. For example, combinations that appear frequently in English, such as ing, tion, pre, and un, often become independent tokens.
The simplified code below demonstrates a “longest match” splitting approach. The code is illustrative only; real model vocabularies and splitting rules are much more complex.
# Illustrative code: greedy longest-match splitting against a fixed vocabularyvocab = {"token", "ization", "cat", "s", "ing", "tion", "pre", "un"}def greedy_split(word: str, vocab: set) -> list:tokens = []i = 0while i < len(word):match = None# Try matching from the longest possible piecefor j in range(len(word), i, -1):if word[i:j] in vocab:match = word[i:j]breakif match is None:# Fall back to a single character when no vocabulary piece matchesmatch = word[i]tokens.append(match)i += len(match)return tokensprint(greedy_split("tokenization", vocab)) # ['token', 'ization']print(greedy_split("cats", vocab)) # ['cat', 's']
As this example shows, the more effectively the vocabulary covers common combinations, the fewer tokens the same text requires.
Step 4: Why Not Use Characters or Whole Words Directly?
A natural question is: if the model ultimately processes text, why not use characters directly, or use whole words directly? Both approaches have clear drawbacks.
Splitting by character makes sequences too long. For an English sentence, the number of characters is often several times the number of words. The context length a model can handle is limited. The longer the sequence, the less content can fit within the same length limit.
Splitting by whole word makes the vocabulary too large and incomplete. Languages contain a huge number of words, and new words, proper nouns, and spelling variants keep appearing. If every word had its own separate entry, the vocabulary would be extremely large, and any word not included in the vocabulary could not be handled.
Subword tokenization strikes a balance between the two: common content is represented with fewer tokens, rare content can be composed from known pieces, and the vocabulary size remains manageable.
Single letters also carry limited meaning. Letters such as l, o, v, and e carry almost no semantic meaning when they appear alone. The model needs to learn meaning from larger combinations. Therefore, the appropriate intermediate granularity is usually meaningful pieces rather than single characters.
Step 5: How Chinese Tokenization Differs
Chinese does not have natural spaces between words, so its tokenization rules differ noticeably from English. In many common models, one Chinese character roughly corresponds to one token. A short sentence may therefore require more than a dozen tokens.
However, this is only an empirical estimate, not an absolute rule. High-frequency phrases are sometimes merged into a single token, depending on the corpus and vocabulary used during model training. Rare characters, punctuation, special symbols, and emoji may also consume more tokens.
Therefore, for Chinese scenarios, estimates should be based on actual tokenizer results rather than relying entirely on the “one character, one token” rule of thumb.
Step 6: Tokens and Context Length Limits
A model’s context length limit is usually expressed as a number of tokens. The input content of a request and the model’s output content generally share this limit. The error “maximum token limit exceeded” usually means that the input plus the expected output exceeds the total length supported by the model.
With this in mind, you can optimize in several ways:
- Estimate first. Before sending a request, use the corresponding model’s tokenizer to count input tokens, avoiding rework after hitting the limit.
- Trim the input. Remove repeated paragraphs, irrelevant background, and redundant formatting symbols, while keeping the information that is genuinely useful for the task.
- Control the output. Specify the output length clearly and reserve enough space for the model’s response.
- Process in chunks. Split long documents into blocks, process them one by one, and then combine the results, keeping necessary context connections between blocks.
An illustrative budget calculation is shown below:
# Illustrative code: reserve output space based on the context limitdef max_input_tokens(context_limit: int, expected_output: int, safety_margin: int = 64) -> int:"""Return the maximum number of tokens available for input in this request."""return context_limit - expected_output - safety_margin
Here, safety_margin accounts for counting errors and formatting overhead. The specific value should be adjusted according to actual conditions.
Step 7: How Tokens Relate to Cost and Speed
Token count also directly affects calling cost and response time. Most billing methods measure input and output by token count, and longer input usually means longer processing time. This means that reducing ineffective tokens can not only avoid limit errors but also lower costs and improve response speed.
It is important to note that different providers have different billing standards, unit prices, and limits. Specific values should be based on the official documentation of the platform you use. This article does not make assumptions about any specific prices.
Practical Recommendations
- Use a consistent tokenizer for estimates. The same model’s tokenizer should be used for both counting and calling, avoiding discrepancies between two standards.
- Pay attention to language differences. Chinese-English mixed text, code, tables, and special symbols differ significantly in token density and should be evaluated separately.
- Manage tokens as a budget. Set quotas separately for system prompts, user input, conversation history, and model output.
- Handle limit errors gracefully. Programs should catch such errors and prompt users to reduce content, or automatically truncate or split the content.
Summary
A token is the basic unit a model uses to process text. It sits between characters and whole words and is formed by splitting text according to high-frequency combinations. Understanding tokenization logic helps explain limit errors, estimate input size, and make more reasonable trade-offs among cost, speed, and output quality. Mastering this concept is fundamental to using large language model APIs correctly.
