Tokens
How many words does a model read in "Srihitha lives in San Francisco"? Five, you might say. The model sees eight pieces, and the name Srihitha alone is three of them. Models don't read words; they read tokens.
This lesson shows what tokens are, using a real tokenizer, and why they decide what you pay and how much a model can read.
What a Token Is
A token is a small piece of text that a model reads or writes as one unit. A token can be a whole word, part of a word, a punctuation mark, or a word with a space in front of it. A tokenizer splits text into tokens and turns each one into a number the model can work with.
Common words are usually one token; rare words and names are split into pieces. "France" is one token, but "Srihitha" becomes Sri, h and itha, because the tokenizer learned its pieces from common text. As a rough guide, one token is about three-quarters of an English word, but it varies.
Counting Tokens in Python
The tiktoken package is an open-source tokenizer. Its o200k_base encoding is used by several OpenAI models; other providers use different tokenizers, so counts differ slightly between models.
import tiktoken # install first: pip install tiktoken
encoder = tiktoken.get_encoding("o200k_base") # a tokenizer used by recent OpenAI models
for text in ["The capital of France is Paris.", "Srihitha lives in San Francisco.", "unbelievably"]:
ids = encoder.encode(text)
pieces = [encoder.decode([i]) for i in ids]
print(len(ids), "tokens:", pieces)7 tokens: ['The', ' capital', ' of', ' France', ' is', ' Paris', '.']
8 tokens: ['Sri', 'h', 'itha', ' lives', ' in', ' San', ' Francisco', '.']
3 tokens: ['un', 'bel', 'ievably']Why Tokens Matter
| Why | What it means for you |
|---|---|
| Cost | Model APIs charge per token, usually separately for input (your prompt) and output (the reply) |
| Limits | A model's context window, from "Context and Context Windows", is measured in tokens |
| Speed | Longer outputs take longer, because the model generates one token at a time |
| Languages | The same message can need more tokens in some languages, so it can cost more |
Common Mistakes
Assuming one word equals one token. Names, rare words and long words often split into several.
Forgetting that output tokens cost too. A long reply can cost more than the prompt.
Comparing token counts across different models directly. Each tokenizer splits text a little differently.
Interview Corner
What is a token?
The basic unit of text an LLM reads and writes: a whole word, part of a word, punctuation or a space. A tokenizer splits text into tokens and maps each to a number.
Why do tokens matter in practice?
They set cost and limits. APIs charge per input and output token, and context windows are measured in tokens, so long prompts and replies cost more and can hit the limit.
Practice
Exercise: count tokens. Use a tokenizer tool, or the Python example, to count the tokens in three sentences: one ordinary English sentence, one with your full name, and one in another language you know. Which used the most tokens per word, and why?
Check your understanding:
- Why is "Srihitha" three tokens while "France" is one?
- Name two things tokens affect.
- Why do token counts differ between models?
Key Takeaways
Quick reference
| Term | Meaning |
|---|---|
| Token | The basic unit of text for an LLM |
| Tokenizer | The tool that splits text into tokens and numbers |
| Input tokens | The tokens in your prompt |
| Output tokens | The tokens in the model's reply |
Was this tutorial helpful?