0% completed
What Is a Token and Why Do Models Not Read Words?
On This Page
Part 1: Why Models Do Not Use Whole Words
Part 2: Why Models Do Not Use Single Characters
Part 3: How the List of Tokens Is Built
Part 4: How Large a Token Is
Part 5: Where Token Counts Appear
Common Wrong Beliefs
What This Means for Your Work
Try It Yourself
Check Your Understanding
Key Takeaways
Short answer: A token is a small piece of text, often a whole word and sometimes part of a word. Models use tokens because the pieces of a fixed list can be joined to write (spell) any text, including new words, typing mistakes, and code. A list of whole words would never be complete, and single characters would make every text several times longer. Providers count limits and prices in tokens.
Look at the middle row. The question has 6 words and 29 characters, and one widely used tokenizer cuts it into 8 tokens.
This lesson uses one question for the whole explanation:
What does a load balancer do?
A model calculates with numbers, so a separate program (the tokenizer) first cuts the text into pieces and replaces each piece with a number. What Happens Inside an LLM When I Ask a Question? shows where this step happens in a request. The parts below explain why the pieces are not whole words and not single characters.
Part 1: Why Models Do Not Use Whole Words
The simplest design gives every word its own number. That design fails because a list of words is never complete.
New words appear all the time: product names, library names, and the function names in your own code. People also make typing mistakes, such as "laod" for "load". A model with a list of whole words has to replace every unlisted word with one mark that means "unknown word". The model then has no information about the meaning of that word. The list also becomes very large, because "balance", "balanced", "balancer", and "balancing" each need a separate entry.
Part 2: Why Models Do Not Use Single Characters
The opposite design gives every character its own number. The list is then small and complete, because it can spell any text, but every text becomes much longer. The question has 29 characters when the spaces and the question mark are counted. A model that works on characters receives 29 pieces for this question, and the tokenizer in the picture produces only 8.
Length matters for two reasons. First, a model produces its answer one piece at a time, and each piece needs one run of the model (a pass). An answer written in single characters therefore needs several times as many passes. Second, a single letter such as "l" has much less meaning than a word such as "load". Models that work on characters usually give worse results.
Part 3: How the List of Tokens Is Built
Tokens are the middle choice. The list of tokens (the vocabulary) holds single characters, frequent parts of words, and frequent whole words. Nobody writes this list by hand, because a program builds it from a very large amount of text before the model is trained.
A common method is called byte pair encoding. The program starts with the smallest pieces only, which are single characters or single bytes. It counts which two neighboring pieces appear together most often in the text. It joins that pair into one new piece and adds the new piece to the list. The program repeats these steps until the list reaches a chosen size, and the list does not change after that (it is fixed). The tokenizer in this lesson has a list of about 200,000 tokens.
The counting explains why some words are one token and others are several. The word "load" is frequent, so the program joined its letters step by step until the whole word became one token. The word "balancer" is less frequent, and it remains two tokens, "bal" and "ancer". Other methods exist, and they also produce pieces with a size between a character and a word.
Notice that the tokenizer rejects nothing. The typing mistake "laod" becomes two tokens, and the 28-letter word "antidisestablishmentarianism" becomes six. The smallest pieces always stay in the list, so the tokenizer can spell any text. This includes a word that did not exist when the list was built.
Part 4: How Large a Token Is
For ordinary English text, a rough estimate is that one token is about four characters, or about three quarters of a word. The question matches the estimate, because its 6 words became 8 tokens, and 6 divided by 8 is three quarters. The estimate is much less accurate for other kinds of text.
Notice the two counts on the right of each row. The English question has 29 characters and 8 tokens, and the line of code has 38 characters and 14 tokens.
- Code has many symbols. Brackets and operators usually become separate tokens. The line
if (count >= 3) { return servers[0]; }has only 8 words when it is split at the spaces, but it becomes 14 tokens. - Numbers are cut into short groups of digits. In this tokenizer, the text "29,700,531" becomes 5 tokens: "29", a comma, "700", a comma, and "531".
- Other languages can need more tokens for the same meaning. The Turkish question in the picture has 4 words and becomes 10 tokens. This happens because Turkish builds long words from many endings, and the list holds few of those long words. The difference depends on the language, and the Spanish version of the question is also 8 tokens.
Part 5: Where Token Counts Appear
Different model families use different tokenizers, so the same text can give a different count for each model. The documentation of one provider gives an example. It states that the tokenizer of its newer models produces about 30 percent more tokens than the tokenizer of its older models for the same text. A count is therefore valid only for the model that it was measured for.
Notice that the two columns do not mention words or characters. The largest amount of text that a model accepts in one request (its context window) is a number of tokens. What Is a Context Window and What Happens When It Is Full? explains that limit. A price list is also written per token, usually per million tokens.
There are two ways to get a real count. Most large providers offer a counting tool or an API call that you use before a request. It returns the number of input tokens without running the model. After a request, the API response reports the input tokens and the output tokens that the request used (the usage numbers). A count made before the request can differ slightly from the usage numbers, so the usage numbers are the ones to record.
For the question in this lesson, the usage numbers report more than 8 input tokens. The reason is that the input also holds the instructions of the application and a few tokens that mark each message.
Common Wrong Beliefs
| Belief | What actually happens |
|---|---|
| A token is the same thing as a word. | A token is often a word, and it is also often a part of a word, a symbol, or a group of digits. The question in this lesson has 6 words and 8 tokens. |
| A model cannot process a word that is missing from its list. | No text is rejected. The tokenizer spells an unlisted word with several smaller tokens, as it does with "laod". |
| Every model counts a text as the same number of tokens. | Different model families use different tokenizers. The same text can give clearly different counts, so a count belongs to one model. |
| The model receives the letters of each word. | The model receives one ID number for each token, and the word "load" arrives as a single number. Why Can an LLM Write Code but Fail at Counting Letters? shows the effects of this. |
What This Means for Your Work
- Estimate in tokens first, and then measure. For ordinary English text, divide the number of characters by four to get a rough count. Use the provider's counting tool before you depend on the number.
- Do not estimate code, logs, or JSON from a word count. One line with few spaces can hold many tokens. In a small test for this lesson, Python source files needed about two tokens per word, and ordinary English text needed about 1.3.
- Count again when you change the model. A prompt that fits the limit of one model can be over the limit of another, because the tokenizers differ.
- Test text in other languages with real samples. The rough estimate comes from English, and other languages can need more tokens for the same content.
Try It Yourself
The program below is a toy tokenizer, which means a very small one that only shows the idea. A real tokenizer has a list of about 200,000 tokens that a program built from text. This toy has a short list that was written by hand, and it always takes the longest piece that matches. Run it and compare the output with the first diagram.
The first run prints 6 words, 29 characters, and 8 tokens. Now change "load" to "laod" in TEXT and run the program again. The count becomes 9 tokens. The list has no piece for the misspelled word, so the program spells it with " la" and "od". The real tokenizer in this lesson cuts the misspelled question into the same 9 pieces.
Check Your Understanding
A log line contains the invented identifier xq7Zvbn_Kd92, which did not exist when the tokenizer's list of tokens was built. What does the tokenizer do with it?
Key Takeaways
- A token is a piece of text from a fixed list. The piece is often a whole word, and it can also be part of a word, a symbol, or a few digits.
- The list can spell any text. Frequent words are single tokens, and rare words, typing mistakes, and new names are spelled with several smaller tokens.
- The size of a token depends on the text and the model. About four characters is a rough estimate for English. Code, numbers, and many other languages need more tokens.
- Limits and prices are counted in tokens. Measure with the provider's counting tool or with the usage numbers in the API response.
The next lesson, How Can I Reduce My Token Usage?, shows what uses the tokens in a real application and how to send fewer of them.
Reading Progress
0%
On This Page
Part 1: Why Models Do Not Use Whole Words
Part 2: Why Models Do Not Use Single Characters
Part 3: How the List of Tokens Is Built
Part 4: How Large a Token Is
Part 5: Where Token Counts Appear
Common Wrong Beliefs
What This Means for Your Work
Try It Yourself
Check Your Understanding
Key Takeaways