Lab

Tokenisation

Tokenisation: Cutting text into the pieces a language model reads, using a fixed vocabulary learned from a large sample of text.

A language model never sees letters or words. Before it reads anything, a tokeniser cuts the text into pieces called tokens and swaps each piece for a number from a fixed list, the vocabulary.

Most tokenisers today start from bytes, so any text at all can be encoded, and then add longer pieces for byte sequences that turn up often. The usual way to find those pieces is byte pair encoding: count every pair of neighbouring tokens in a training text, merge the most frequent pair into a new token, and repeat until the vocabulary reaches the size you want. Common words end up as single tokens; rarer words are built from several smaller pieces.

Real tokenisers first split text into chunks, such as words with their leading space, runs of digits and punctuation, and never merge across those chunks. That is why a word with its leading space is often one token.

Once a tokeniser is trained, its vocabulary is frozen, and the model is trained on text cut up by that tokeniser. A model and its tokeniser always travel together: swap the tokeniser and the model’s numbers no longer line up with the text.

Tokenisation matters in practice because a model pays for every token it reads. Text that splits into more pieces, as many languages other than English do, costs more memory and time and fills the context sooner. The explainer lets you train a small tokeniser by hand and watch which pieces it learns.

See it working

Explore it in How text becomes tokens

See your text cut into the numbered pieces a language model actually reads, and why that shapes cost, fairness and odd mistakes.

Open the explainer