Skip to content
StackDecode
LLM Library
FoundationsAnimated

Tokens and Tokenizers

Text is cut into sub-word pieces before the model sees it.

The idea

A tokenizer maps text to integer ids using a learned vocabulary (often byte-pair encoding). Common words are one token; rare words split into several.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

l o w _×5l o w e r _×2n e w e s t _×6w i d e s t _×3
  • single character
  • merged token

Byte-pair encoding builds a vocabulary by repeatedly merging the most common neighbouring pair of symbols. Training words (with counts): low×5, lower×2, newest×6, widest×3. "_" marks the end of a word.

1/13

This is the algorithm behind the tokenisers of most modern language models.

Step by step

  1. 1Start from characters or bytes
  2. 2Repeatedly merge the most frequent pairs
  3. 3Keep a vocabulary of 30k to 250k pieces
  4. 4Encode new text by applying the merges

Where it is used

Every LLM. Token counts set cost and context limits.

Watch out for

Spelling, arithmetic and non-English text can behave oddly because of how words are split.