How tokenization works: BPE, WordPiece and special tokens

Before an LLM can read your text, a tokenizer cuts it into tokens and swaps each one for a number (its token ID) from a fixed vocabulary. Modern models use subword tokenization: common words stay whole, while rare words are split into familiar pieces, so the model never meets a word it can't handle.

Everyday analogy

Lego bricks. You don't need a special brick for every object: a few thousand standard bricks can build anything. Subword tokens are the standard bricks of language.

Example

"unbelievable" might become un + believ + able, three IDs such as 521, 7381 and 412. A brand-new word like "aifromzero" still works: it is simply split into smaller pieces the model already knows.

unbelievabletokenizer (BPE)un521token IDbeliev7381token IDable412token ID
A rare word is split into familiar subword pieces, and each piece becomes a number the model can read.

Why not just use whole words?

  • The vocabulary would need millions of entries, and new words appear every day.
  • Characters alone are too small: sentences become very long and slow to process.
  • Subwords are the middle path: a vocabulary of roughly 30,000 to 200,000 pieces covers almost any text, in many languages.

Common tokenization methods

MethodUsed byKey idea
BPE (byte pair encoding)GPT models and many othersStart from characters or bytes and repeatedly merge the most frequent pair
WordPieceBERTLike BPE, but merges the pair that most improves the likelihood of the text
SentencePieceT5, Llama 2 and many multilingual modelsWorks on raw text, treating spaces as normal symbols, so it suits any language
UnigramOften inside SentencePieceStarts with many pieces and keeps the set that best explains the text

Special tokens

TokenPurpose
<BOS> / <EOS>Beginning and end of a sequence; the model stops writing when it produces end-of-sequence
<PAD>Padding, so texts of different lengths can be processed together
<UNK>Unknown token (rare with subword tokenizers)
[CLS] / [SEP]BERT's classification and separator tokens
Chat markersHidden tokens that mark where the system prompt, your message and the reply start

Why it matters to you: tokens decide cost, speed and limits. Many languages other than English need more tokens for the same sentence, so they cost more and fill the context window faster.

Try it yourself

About 10 min

See subword tokens for yourself

  1. Open a free online tokenizer (search OpenAI tokenizer).
  2. Paste: unbelievable, internationalization, your own name and a sentence in Hindi or Arabic.
  3. Note how each is split and count the tokens.

What you should see: Common words stay whole, rare words split into pieces, and non-English text usually needs more tokens.

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot). Never paste private or confidential information.

Build it for real · guided projectBuild your own custom AI assistantAbout 60 min · No coding needed · step by step
Test what you learned

Take a quick quiz, or look up any word in the glossary.