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. You should also be comfortable with simple arithmetic and unit conversion, such as dollars per million. You do not need to know how a model works internally.
Tokens are the unit a model reads
A language model does not read characters or words. It reads tokens, which are chunks of text drawn from a fixed vocabulary. A token can be a whole word, part of a word, a single punctuation mark, or a space attached to a word. Each token maps to an integer ID, and the model works only with those IDs.
Tokens matter because they are the unit for three limits you will meet in later chapters:
- The context window is measured in tokens.
- The maximum output length is measured in tokens.
- Providers bill by tokens.
Your first practical skill is knowing how many tokens a piece of text uses. Word counts and character counts are only rough guides.
A first look at tokens
The open-source tiktoken library lets you see the split directly. Install it with pip install tiktoken, then run this:
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 text has 7 words. The token count is usually higher, because the tokenizer splits "Tokens" into smaller pieces and treats the final period as its own token. The printed list shows the text of each token. Notice that a space is usually attached to the start of the word that follows it.
The exact split depends on the encoding. cl100k_base is one specific vocabulary. Other model families use different tokenizers, so the same sentence can produce a different count elsewhere. A count from tiktoken is exact only for models that use that encoding.
Why counts differ from words
Tokenizers are built so that common strings get a single token and rare strings are broken into pieces. This gives some rules of thumb for English prose:
- A token is about four characters, or roughly three quarters of a word.
- 100 tokens is about 75 words.
- Common words such as "the" are one token.
- Rare words, names, and typos split into several tokens.
These ratios shift for other kinds of text.
| Text type | Typical effect on token count |
|---|---|
| Plain English prose | Close to the rules of thumb above |
| Source code | Often more tokens, because of symbols and indentation |
| Numbers and IDs | Often one token per few digits |
| Non-English text | Often more tokens per word, depending on the language and tokenizer |
Where tokens sit in a call
Both directions of a call are measured in tokens. Your prompt is tokenized into input tokens. The model then generates output tokens one at a time.
Input tokens and output tokens are counted separately. Later chapters show that they are often priced differently and are limited differently.
Common mistakes
- Estimating from words. A prompt of 1,000 words is not 1,000 tokens. It is usually closer to 1,300 for English prose.
- Using the wrong tokenizer. A count from one encoding can be off for a model from a different family.
- Forgetting hidden text. System prompts, tool definitions, and prior messages all count as input tokens, even though you did not type them in this turn.
Exercise
A prompt is 600 English words. Using the rule that 100 tokens is about 75 words, roughly how many tokens is it, and why might the real count from your model's tokenizer differ?
Show answer
600 words is about 800 tokens (600 / 75 x 100). The real count can differ because the rule is only an approximation for English prose, and the exact count depends on the model's tokenizer and on the content, such as code, numbers, or non-English text.
Exercise
Why can you not rely on len(text.split()) to check whether a prompt fits a model's limit?
Show answer
It counts words, but limits and billing use tokens. A token count is usually higher than a word count, and the exact number depends on the model's tokenizer.