Sub topic 1
What a token is
Prerequisites
This chapter assumes you can read basic Python and have called an LLM through a chat or completion API at least once. You should also be comfortable with simple arithmetic and unit conversion, such as converting "per million" prices to a per-call cost. You do not need to know how a model works internally.
Why tokens matter
A language model does not read characters or words. It reads tokens, which are the units of text the model takes as input and produces as output. A token is usually a piece of a word, a whole short word, a punctuation mark, or a run of whitespace.
Tokens are the unit behind three things you will deal with in every LLM project:
- Limits. Each model accepts a maximum number of tokens in a single request and can produce a maximum number of tokens in a reply.
- Cost. Providers bill by the token, not by the word or the request.
- Speed. A model generates its reply one token at a time, so longer replies take longer.
If you reason in words or characters, your estimates will be off. If you reason in tokens, they will be close.
Tokens are not words
Take the sentence "Tokens are not the same as words." It has seven words. A tokenizer, the component that turns text into tokens, may split it into a different number of pieces. A word such as "tokenization" may become two or three tokens, while a common word such as "the" is usually one token. A leading space is typically attached to the token that follows it, so " the" and "the" can be different tokens.
You can see this directly with the open-source tiktoken library. Install it with pip install tiktoken, then run:
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
text = "Tokens are not the same as words."
token_ids = enc.encode(text)
print(len(text.split()), "words")
print(len(token_ids), "tokens")
print([enc.decode([t]) for t in token_ids])The first line prints the word count, the second prints the token count, and the third prints the text of each token. The two counts differ, and several of the printed pieces begin with a space. That is the key observation of this chapter: the word count and the token count are separate numbers, and the token count is the one that matters to the model and the bill.
Each token also has an integer ID, which is what enc.encode returns. The model never sees your letters. It sees a list of integers such as [...], one per token, and looks up a learned representation for each one.
Where tokens come from
A tokenizer has a fixed vocabulary, a list of the pieces it knows, typically tens of thousands to a few hundred thousand entries. The vocabulary is built before the model is trained, by looking at a large body of text and keeping the pieces that appear most often. Common words end up as single entries. Rare words are built from smaller pieces. Anything the vocabulary cannot cover can be spelled out from smaller units, down to individual bytes in many tokenizers, so any input can be encoded.
The next chapter covers this process in more detail. For now, the practical consequences are enough:
- Frequent English words are cheap: often one token each.
- Rare words, names, and technical terms split into several tokens.
- Numbers, code, and punctuation often use more tokens per character than plain prose.
- Text in languages that are less represented in the vocabulary usually needs more tokens for the same meaning.
A rough conversion for English
For ordinary English prose, a common rule of thumb is that one token is about four characters, or about three quarters of a word. So 1,000 tokens is roughly 750 words.
| Quantity | Rough English equivalent |
|---|---|
| 1 token | about 4 characters, or 0.75 words |
| 100 tokens | about 75 words |
| 1,000 tokens | about 750 words, one or two pages |
| 1,000,000 tokens | about 750,000 words |
Treat these as planning figures only. They are averages for English prose. Source code, JSON, tables, and non-English text can differ a lot, often towards more tokens per word. When a number matters, count it with a real tokenizer (the topic of a later chapter).
The tokenizer belongs to the model
Different model families use different tokenizers. The same sentence can produce different token counts, and different token IDs, under different vocabularies. This leads to a few rules:
- A count from one tokenizer is exact only for models that use that tokenizer.
- A count from a different tokenizer is an estimate. It is usually close, but it can be off by a noticeable margin for code or non-English text.
- Limits and prices are always expressed in the counts of the provider's own tokenizer, so when they disagree with your local count, the provider's number wins.
import tiktoken
text = "def add(a, b):\n return a + b\n"
for name in ["cl100k_base", "o200k_base"]:
enc = tiktoken.get_encoding(name)
print(name, len(enc.encode(text)), "tokens")Running this prints one line per encoding. The counts may match or differ by a few tokens, depending on how each vocabulary handles whitespace and code. The point is that "how many tokens is this text" has no single answer. It has one answer per tokenizer.
Input tokens and output tokens
Every call has two sides.
- Input tokens are everything you send: the system prompt, the conversation so far, any documents you paste in, tool definitions, and the new user message.
- Output tokens are everything the model generates in reply.
The two are counted separately because they are limited and priced separately. Output tokens usually cost more per token than input tokens, because generating each one requires a separate step of computation, while input can be processed in parallel. Later chapters cover the limits and the prices in detail.
Common errors
These mistakes come up repeatedly when people first budget for LLM calls.
- Counting words instead of tokens. A 500-word document is closer to 650 to 700 tokens in English prose, and more in code or other languages.
- Forgetting the hidden parts of the input. The system prompt, tool or function definitions, and earlier turns all count as input tokens, even though the user never types them again.
- Using the wrong tokenizer. A local count from a different tokenizer is only an estimate. Do not use it to make exact billing claims.
- Assuming whitespace is free. Spaces, newlines, and indentation become tokens, or become part of tokens. Heavily indented code or large tables of padded text cost more than they look.
- Assuming the same text always costs the same. The same text can cost different amounts on different models, because the tokenizers differ.
A quick worked example
Suppose you plan to send a 3,000-word English report to a model and ask for a 300-word summary.
- Estimate input: 3,000 words divided by 0.75 gives about 4,000 tokens.
- Estimate output: 300 words divided by 0.75 gives about 400 tokens.
- Add some for the instructions you wrap around the report, perhaps 100 tokens.
The request is then about 4,100 input tokens and 400 output tokens. This is an estimate, accurate to maybe 10 to 20 percent for plain English. It is good enough to tell you whether the job is cheap or expensive, and whether it is nowhere near a model's limit or close to it. When the estimate lands near a limit or a budget line, count with the real tokenizer or read the usage numbers the API returns.
Key points
- A token is the unit of text a model reads and writes: a word, part of a word, punctuation, or whitespace.
- Token counts differ from word and character counts. For English prose, one token is about four characters.
- Each model family has its own tokenizer, so counts are exact only for the matching one.
- Input tokens and output tokens are counted separately.
- Limits, cost, and speed are all measured in tokens, which is why the rest of this tutorial builds on this unit.
Exercise
A colleague says, "Our prompt is 2,000 words, so it will use 2,000 tokens, and it will cost the same on every model." Give two reasons this statement is unreliable, and give a rough token estimate for the prompt if it is English prose.
Show answer
First, words and tokens are different units. English prose averages about 0.75 words per token, so 2,000 words is about 2,000 / 0.75, roughly 2,700 tokens, not 2,000. Second, each model family uses its own tokenizer, so the same text can produce different token counts and therefore different costs on different models. Prices per token also differ between models, which is a further reason the cost will not be the same everywhere.
Exercise
You run the tiktoken example on the sentence "Tokens are not the same as words." and see that the word count is smaller than the token count. Name two features of text that make the token count higher than the word count.
Show answer
Punctuation such as the final period becomes its own token, and longer or less common words such as "Tokens" can be split into more than one piece. Leading spaces, numbers, and code symbols can also add tokens.