Lesson 1 of 96 min
Tokenization
A model reads text as pieces, each one a number from a fixed list, and the pieces are learned: what was common became one piece.
After this lesson you can split text into tokens and say why models use pieces of words.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
See the space in a token
Install transformers and tiktoken in your Python environment. The first run downloads the public tokenizer files. In Qwen’s vocabulary, Ġ stands for a space. Decode the token for Ġlive and you get back the space and the word.
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("Qwen/Qwen3.5-0.8B-Base")
sentence = "Penguins live in the Antarctic."
ids = tok.encode(sentence, add_special_tokens=False)
print(tok.convert_ids_to_tokens(ids))
print(repr(tok.decode([ids[2]])))['P', 'enguins', 'Ġlive', 'Ġin', 'Ġthe', 'ĠAntarctic', '.'] ' live'
Look below the letters
The video starts with single letters so you can follow the merges. Byte-level tokenizers start with bytes. This penguin takes four bytes in UTF-8, and Qwen stores those bytes across three tokens. Decode them together to get the penguin back.
penguin = "🐧"
print(list(penguin.encode("utf-8")))
print(tok.tokenize(penguin))
print(tok.decode(tok.encode(penguin, add_special_tokens=False)))[240, 159, 144, 167] ['ðŁ', 'IJ', '§'] 🐧
Compare where tokenizers cut
Qwen splits this number into single digits. GPT-2 groups them. Even within one word, Qwen’s pieces can cut across meaning. Other models can use other rules. Hugging Face’s Tokenizers library supports WordPiece, used by BERT, and Unigram, one option in SentencePiece. Tokenizer algorithms
gpt2 = AutoTokenizer.from_pretrained("openai-community/gpt2")
for tokenizer in (tok, gpt2):
print(tokenizer.tokenize("1234567"))
print(tok.tokenize("unbelievably"))['1', '2', '3', '4', '5', '6', '7'] ['123', '45', '67'] ['un', 'belie', 'vably']
Count the same sentence in three languages
The columns below are Qwen and GPT-2. Qwen uses fewer tokens for this sentence in Chinese than it does in English. The Persian sentence takes more in both. The examples show which pieces each tokenizer has available for these sentences.
persian = "پنگوئنها در قطب جنوب زندگی میکنند."
chinese = "企鹅生活在南极洲。"
for text in (sentence, persian, chinese):
print(len(tok.encode(text, add_special_tokens=False)),
len(gpt2.encode(text, add_special_tokens=False)))7 8 16 45 5 20
Count tokens for an OpenAI model
Use tiktoken with the name of the model you plan to call. It can count tokens in a string locally, without an API key. The IDs belong to that model’s vocabulary. A chat request can include extra tokens for the message format. OpenAI’s tiktoken
import tiktoken
encoding = tiktoken.encoding_for_model("gpt-4o")
openai_ids = encoding.encode(sentence)
print(openai_ids)
print(len(openai_ids))[82255, 104885, 4561, 306, 290, 176692, 13] 7
Run on Python 3.13.3, transformers 5.24.0, tokenizers 0.23.3 and tiktoken 0.14.0. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
A model can write a whole essay, but counting the r's in "strawberry" is hard for it. Why?
2 of 2
A tiny list starts from single letters. Cut "papaya" into its six letters, then do one merge: join the pair seen most often, everywhere it comes up. How many pieces are left?
The ideas in this lesson
- Token
- A piece of text a model reads as one: a whole word, part of a word, or a mark like a full stop.
- Vocabulary
- The fixed list of every piece a model knows; each token reaches the model as its number in that list.
- Byte-pair encoding
- How the pieces are learned: start from single letters, and merge the pair seen most often, again and again.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
NLP and Transformers
What's really inside a model like ChatGPT?
A few simple parts, repeated many times. Hi, I'm Mohi Fayden. NLP is how computers read and write our language. Today, most of it runs on transformers. This course follows one sentence through every part, step by step. By the end, you'll train a tiny GPT of your own. It's for you if you know Python, vectors and how networks learn. Every lesson has its own page on faydenai.com, with a quiz. There's a lab every few lessons, and a project at the end.
Tokenization
Here's a mystery. A model can write a whole essay. But counting the r's in strawberry is hard for it. Why?
Keep your guess
Hold on to your guess. The answer is in what a model really reads.
Tokens
Take this sentence. Twenty-six letters, five words. How many pieces does the model read?
Seven.
A tool called the tokenizer cuts the sentence into these pieces. They're not letters, and not quite words. "Penguins" is two pieces: a capital P, then the rest. Each piece is called a token.
A number for each token
So what does the model get, for each token? A number. The model has a fixed list of every piece it knows. Each piece has its own place in that list. That list is called the vocabulary. So all the model ever gets is seven numbers. It never sees the sentence itself.
One number, no letters
Now, back to the mystery. Here's what reaches the model from the question, "How many r's are in strawberry?" Look at the tile for "strawberry". Can you see its letters?
"Strawberry" is one token. One number. No r's reach the model at all. So it has nothing to count. It can only answer from what it once read about how that word is spelled.
Why pieces
So why pieces? Think about how you read. A word you know, you take in all at once. A long new word, you read in pieces.
What if every whole word had its own number? The list would never end: names, new words, spelling mistakes, every language.
And a word that's not on the list would have no number at all.
And one number for every letter? Then this sentence is thirty-one tokens, not seven.
And a model does its work once for every token. Pieces sit in between. Common words are one piece. Anything else is spelled out of smaller pieces, down to single letters.
Even "penguins" spelled wrong still gets its pieces.
Learned, not written
So who picks the pieces? Nobody writes them. They're learned from text.
Here's a short paragraph about penguins, cut into single letters. Which two letters, side by side, come up most often?
The letters h and e, side by side.
Six times. So they're merged into one piece, everywhere. Then a space and a t.
Then those two pieces join.
Now "the", with its space, is one piece, in five places. Merge the pair you see most often, again and again.
That's called byte-pair encoding.
Keep merging
Keep going. After fifteen merges, there are far fewer pieces.
"They" is one piece now. But "Antarctic" comes up only once here, so it stays in small pieces.
A real list learns from far more text.
There, "Antarctic" comes up often enough to be one piece. And the list keeps merging until it's full.
Penguins, twice
So why is "Penguins" two pieces? You can work it out... Look closely at the tiles. The space before a word goes with that word. In the second sentence, "Penguins" has a space in front of it, and it's one piece.
In the text the list learned from, a space and then "Penguins" came up often. So they were merged into one piece.
At the very start of a text, there's no space in front. That came up less often, so it stayed two pieces. You'd cut a word where its meaning splits. A model cuts it where it saw letters together most often.
The cost of a language
Why does this matter to you? You pay for a model by the token, not by the word. And the languages a list saw most get big pieces. The rest get small ones. Here's the same sentence in Persian. More than twice as many tokens. So the same message can cost more in another language.
In code
Now in real code, with Hugging Face's transformers library. First, load the model's tokenizer. Give it the sentence, and print what comes back. What will it print?
Seven numbers, in a list. Decode them, and the seven pieces come back. One more line gives the size of its whole list. And the same call counts the tokens in any text, before you send it. The Persian sentence: sixteen.
Three ideas
So a model reads tokens, pieces of text. Each one reaches the model as its number in the vocabulary. And the pieces are learned, by merging the pair seen most often.
Pieces of text
A model reads tokens: pieces of text, each one a number. Now try it on this lesson's page, and take the quiz. Find a text with more tokens than letters.
You use this lesson in Lab 1: Score any text, token by token, after Language modeling.
Facts last checked on .
