Tokenisation

Tokenisation is the bridge from human language to the integer sequences that transformers can process.

The Big Question

A transformer never sees words directly. It performs matrix multiplication on numbers. So before it can process a sentence, language must be translated into a sequence of integer IDs.

How can a transformer process human language when computers only understand numbers?

Tokenisation is that translation process. It decides how text is split into units and assigns each unit an integer ID from a fixed vocabulary.

Why This Is Necessary

Humans see I want a burger. and immediately notice words. A computer initially stores characters as bytes, such as hexadecimal values or binary digits. Nothing in those raw bytes tells the model that burger is a meaningful unit.

Before attention, embeddings, or gradient descent can happen, text needs a numerical representation.

Core Intuition

Tokenisation is a compromise. If tokens are too small, sequences become long and expensive. If tokens are too large, the vocabulary explodes and rare words become impossible to handle.

Attempt 1: Characters

Character-level tokenisation gives every character an ID:

cat[3,1,20]\text{cat}\rightarrow[3,1,20]

This always works, but it makes sequences much longer. A word like internationalisation becomes more than twenty tokens. Since attention compares tokens with tokens, longer sequences quickly become expensive.

Attempt 2: Words

Word-level tokenisation is shorter:

I love burgers[15,203,1042]\text{I love burgers}\rightarrow[15,203,1042]

But word vocabularies explode. Names, slang, code, URLs, scientific terms, new words, and many languages create too many possibilities. A pure word-level tokenizer does not gracefully handle a word it has never seen.

Attempt 3: Subwords

Modern LLMs usually use subword tokenisation. Frequent words can stay whole, while rare words split into reusable pieces:

cheeseburger[cheese,burger]\text{cheeseburger}\rightarrow[\text{cheese},\text{burger}]
microbiology[micro,bio,logy]\text{microbiology}\rightarrow[\text{micro},\text{bio},\text{logy}]

Subwords balance coverage and efficiency. They can represent new text without needing a vocabulary entry for every possible word.

Interactive Demo

Token boundary lab

Character

28 tokens
Ispacewouldspacelikespaceaspaceche

Always works, but sequences get long.

Word

5 tokens
Iwouldlikeacheeseburger.

Short, but unseen words can fail.

Subword

7 tokens
I would like a cheeseburger.

Balances efficiency and coverage.

I would like a cheeseburger.

Smaller vocabularies force shorter pieces. Larger vocabularies can store frequent chunks as single tokens.

Subword token IDs
tokenID
I17
would524
like812
a29
cheese1442
burger1824
.18

BPE merge intuition

burger

BPE starts with small pieces and repeatedly merges frequent neighbouring pieces.

Attention matrix cells
49
Toy cost estimate
$0.00001

Token count affects context length, attention cost, and API pricing. Tokenisation is a modeling decision, not just text cleanup.

Try made-up words, punctuation, and emoji. Notice how subword tokenisation avoids the unknown-word problem while keeping sequences shorter than character tokenisation.

Mathematics

What Is A Token?

A token is one unit chosen by a tokenizer. It might be a word, a word piece, a punctuation mark, whitespace, a number, or an emoji.

Modern tokenizers often include leading spaces inside tokens. For example, New can be a different token from New. This surprises beginners, but it helps the tokenizer represent common text patterns efficiently.

Vocabulary

A tokenizer has a fixed vocabulary of size VV. Each token receives exactly one integer ID:

token id{0,1,,V1}\text{token id}\in\{0,1,\ldots,V-1\}

For example:

TokenID
the0
burger1824
ing624
tion942
.18

The IDs Are Arbitrary

The sentence I like burgers. might become:

[17,  524,  1824,  9][17,\;524,\;1824,\;9]

These numbers are identifiers, not quantities. Token 1824 is not larger, more important, or more meaningful than token 17. It is just a label.

Byte Pair Encoding Intuition

Byte Pair Encoding, or BPE, starts with very small pieces and repeatedly merges frequent neighboring pieces. If er appears often, it may become one token. If burger appears often, it may eventually become one token too.

Rare words do not need to become full tokens. They can remain combinations of smaller pieces.

Why Token Count Matters

If a prompt has nn tokens, full attention compares token pairs, giving roughly:

n2 attention scoresn^2\text{ attention scores}

More tokens also mean more context-window usage and, in many APIs, more cost. The tokenizer affects computation before the model has even started reasoning.

Why Tokenisation Is Not Enough

Tokenisation gives the model integer IDs. But an ID alone has no semantic meaning:

burger=1824,pizza=3910,dog=281\text{burger}=1824,\quad \text{pizza}=3910,\quad \text{dog}=281

The model still needs embeddings to turn these arbitrary IDs into vectors that can learn relationships. Tokenisation gives identifiers; embeddings give the model a learnable representation.

Implementation

In practice, you normally use the tokenizer that belongs to the model. Different models can use different vocabularies and split the same text differently.

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("gpt2")

text = "I would like a cheeseburger."

tokens = tokenizer.tokenize(text)
ids = tokenizer.encode(text)

print(tokens)
print(ids)

# tokenize() returns text pieces.
# encode() returns integer IDs.

The exact output depends on the tokenizer. That is the point: tokenisation is part of the model design.

Interview Discussion

Why can transformers not operate directly on text?

Transformers operate on vectors and matrices, so text must first be converted into token IDs and then embeddings.

Why is character tokenisation inefficient?

It creates long sequences, and attention cost grows roughly quadratically with token count.

Why does word-level tokenisation struggle?

The vocabulary becomes enormous, and unseen words are hard to represent gracefully.

Why do modern LLMs use subword tokenisation?

Subwords balance efficiency and coverage by storing frequent chunks while decomposing rare text into smaller known pieces.

What is the intuition behind BPE?

Start with small pieces and repeatedly merge frequent neighboring pieces to build a useful vocabulary.

Why are token IDs arbitrary?

They are labels into a vocabulary, not meaningful numerical quantities.

Active Recall

1. What problem does tokenisation solve?

2. Why is character-level tokenisation inefficient?

3. Why does word-level tokenisation struggle with new words?

4. What is a subword token?

5. What does Byte Pair Encoding optimize for?

6. Why do token IDs have no semantic meaning?

7. Why is tokenisation alone not enough for a transformer?

Common Mistakes

  • Tokens are words: Many words split into multiple tokens, and many tokens are word pieces.
  • Token IDs contain meaning: IDs are arbitrary labels, like student ID numbers.
  • Every model uses the same tokenizer: Different models can split the same text differently.
  • Unknown words cannot be represented: Subword tokenisation breaks rare text into known pieces.

Connection To Language Modelling

We now know how raw text becomes integer IDs. The next lesson uses those IDs in the language-modelling objective: predicting the next token again and again.