Knowledge Sprint SeriesMT24 Learning

Phase 8 · Sprint 22/90 · Tokenization & ParsingSennrich · Schuster · Kudo · Radford · Petrov

How Tokenization Works

A model never reads my words. It reads the numbers a tokenizer hands it.

In short. Turning text into numbers, three ways to cut text (words, characters, subwords), the UTF-8 bytes underneath, and why the same text costs more tokens in some languages.

Sprint 22 of 90 infographic: How Tokenization Works. A model never reads my words. It reads the numbers a tokenizer hands it. The full text of the image is on this page.
Full resolution2160 × 2700 PNGPhase summary

The part of the image you are reading lights up. Tap a part to jump to its text.

Fig. 1 in words

One phrase, three cuts · toy vocabulary of 32 pieces · same data as code 05

  1. 1 Chars

    • 23 sorts
    • u n b e l i e v a b l y ␣ t o k e n i z e r s
  2. 2 Words

    • 2 sorts
    • unbelievably, struck out: [UNK]
    • tokenizers, struck out: [UNK]
  3. 3 Pieces

    • 7 sorts
    • un | believ | ably | token | ize | r | s
    • r in a dashed box
  4. 4 IDs

    • un 0 · believ 1 · ably 3 · token 4 · ize 5 · r 24 · s 6

01 Text to Numbers Encode

  • A tokenizer cuts text into pieces from a fixed vocabulary
  • Each piece has one integer ID. The model sees only the IDs
  • Decoding turns the IDs back into text

SentencePiece marks each space with ▁, so decoding returns the text (Kudo & Richardson 2018).

02 Three Ways to Cut Cuts

  • Words: a huge vocabulary. An unseen word becomes [UNK]
  • Characters: nothing unknown, but one word can take 10 or more tokens
  • Subwords: frequent words whole, rare ones in parts

Subwords for rare words: Sennrich et al. (2016). WordPiece: Schuster & Nakajima (2012).

03 Bytes Underneath UTF-8

  • UTF-8 uses 1 to 4 bytes per character. ASCII uses one
  • é and س take 2 bytes. € takes 3
  • GPT-2 starts from 256 bytes, not 130 000+ symbols, and ends with 50 257 tokens

With bytes as the floor, any string can be encoded (RFC 3629; Radford et al. 2019).

04 Same Text, Other Cost Cost

  • Translated, one text can need up to 15 times more tokens
  • At byte or character level: still over 4 times for some pairs
  • More tokens: higher price, slower replies, less context

Petrov et al. (2023). In UTF-8 “salam” is 5 bytes and سلام is 8.

05 Try It Yourself PYTHON 3.11.15

sprint22_how_tokenization_works.py

text = "unbelievably tokenizers"
seen = {"unbelievable", "tokens"}
subs = ["un", "believ", "able", "ably",
        "token", "ize", "s"]
abc = list("abcdefghijklmnopqrstuvwxyz")
vocab = {p: i for i, p in
         enumerate(dict.fromkeys(subs + abc))}
def split(w):        # longest match first
    out, i = [], 0
    while i < len(w):
        j = len(w)
        while w[i:j] not in vocab: j -= 1
        out.append(w[i:j]); i = j
    return out
words = text.split()
toks = [t for w in words for t in split(w)]
print("chars", len(text))
print("words", [w if w in seen else "[UNK]"
                for w in words])
print("pieces", "|".join(toks))
print("ids", [vocab[t] for t in toks])
print("round trip", " ".join(
    "".join(split(w)) for w in words) == text)
print("bytes", [len(c.encode()) for c in "aéس€"])
print(len("salam".encode()), len("سلام".encode()))

Output, as printed on the image

chars 23
words ['[UNK]', '[UNK]']
pieces un|believ|ably|token|ize|r|s
ids [0, 1, 3, 4, 5, 24, 6]
round trip True
bytes [1, 2, 2, 3]
5 8

Run it. sprint22_how_tokenization_works.py · versions, data and steps

Check: 12 + 1 + 10 = 23 · 23 ÷ 7 ≈ 3.3 chars a piece · ⁨سلام⁩ 4 × 2 = 8 bytes

06 General Notes Practice

  • Budget a model’s context in tokens, not words
  • Change the vocabulary and the IDs change: the tokenizer is part of the model
  • Test decode(encode(x)) == x on accents and Arabic

My toy split() knows only a–z: Arabic input loops forever. Byte-level tokenizers fall back to bytes.

07 Mini Project Hands-on

  • Add “izer”: token|izer|s, IDs shift to [0, 1, 3, 4, 6, 7]
  • Give split() a byte fallback, then run an Arabic sentence
  • Write decode() and check the round trip on 20 sentences

Count chars, bytes and pieces for one sentence in Arabic and in English.

R Trusted Sources

  1. Sennrich et al. (2016). Neural Machine Translation of Rare Words… ACL
  2. Schuster & Nakajima (2012). Japanese and Korean Voice Search. ICASSP
  3. Song et al. (2021). Fast WordPiece Tokenization. EMNLP, 2089–2103
  4. Kudo & Richardson (2018). SentencePiece. EMNLP Demos, 66–71
  5. Radford et al. (2019). Language Models are Unsupervised… OpenAI
  6. Yergeau (2003). RFC 3629: UTF-8, a transformation format… IETF
  7. Petrov et al. (2023). Language Model Tokenizers Introduce… NeurIPS
  8. Hugging Face LLM Course, ch. 2: Tokenizers

Cite this phase

Tahri, M. (2026). MT24 Learning — Knowledge Sprint Series, Phase 8: Tokenization & Parsing (Version 1.0) [Educational resource]. Zenodo. https://doi.org/10.5281/zenodo.23213765

Version DOI 10.5281/zenodo.23213765 · all versions 10.5281/zenodo.23213764 · first published 2 October 2026