Velamuvi AIAcademy
Tokenization

Lesson 1 of 5 · Tokens

What Is a Token?

Tokens explained: why models use subwords, vocabularies and token IDs, why spaces and capitals matter, and how encoding and decoding work, live.

  • 6 min read
  • Intermediate
  • Reviewed
In this lesson you will learn
  • What tokens, vocabularies and token IDs are
  • Why subword tokens beat characters and whole words
  • Why spaces, capital letters and digits change how text is split
  • How text is encoded into IDs and decoded back

The model's alphabet

A language model reads and writes a fixed set of pieces of text, its vocabulary. Every piece has a number. Turning text into those numbers is the first thing that happens to every prompt, and turning numbers back into text is the last.

Try it. This uses the real tokenizer of a recent OpenAI model family; other model families use different but similar tokenizers.

Loading the tokenizer…
0 tokens · 13 words · 86 characters
Switch on Show token IDs to see the numbers the model actually receives.

Three ways to split text

The program below splits the same sentence three ways.

Try it yourselfPython
sentence = "unhappiness is unusual"

# 1. Characters: tiny vocabulary, but very many tokens
characters = list(sentence)

# 2. Whole words: few tokens, but every possible word needs its own entry
words = sentence.split()

# 3. Subwords: a small vocabulary of common pieces, matched longest-first
vocabulary = ["un", "happi", "ness", "is", "usual", " "]

def subword_tokenize(text):
    tokens = []
    i = 0
    while i < len(text):
        match = text[i]                      # fall back to a single character
        for piece in vocabulary:
            if text.startswith(piece, i) and len(piece) > len(match):
                match = piece
        tokens.append(match)
        i += len(match)
    return tokens

print(len(characters), "character tokens")
print(len(words), "word tokens:", words)
subwords = subword_tokenize(sentence)
print(len(subwords), "subword tokens:", subwords)
ApproachVocabulary sizeTokens per textProblem
CharactersTiny (a few hundred)Very manyLong sequences: slow, and the model sees less text at once
Whole wordsEnormousFewEvery new word, name, typo or form needs its own entry
Subwords100,000 to 200,000ModerateA good balance; rare words are split into known pieces

Subword tokenizers can represent any text: common words are single tokens, and anything unusual is assembled from smaller pieces, down to single characters or even bytes if necessary.

Details that surprise people

Spaces belong to tokens. Most tokenizers attach the space to the start of the following word, so " hello" (with a space) and "hello" are different tokens with different IDs. In the picture above, "unbelievable" at the very start is split into three pieces, while " unbelievable" after a space is a single token.

Capital letters matter. "Hello", "hello" and "HELLO" are different tokens, and all-capitals text often splits into more pieces, because it is rarer in training data.

Numbers are chunked. Digits are usually grouped into chunks of up to three: "1234567" becomes "123", "456", "7". The model never sees the number as one value, which is one reason exact arithmetic is hard for it.

Encoding and decoding

When a model generates, it produces one token ID at a time, and the app decodes each to show you text. Because a token can be part of a word, or even part of a multi-byte character, streaming apps sometimes wait for the next token before displaying a character correctly.

Common mistakes

Practice exercise

A model outputs token IDs, not text. Decode these IDs back into text, as a tokenizer does.

Your solutionPython
vocabulary = {"un": 0, "happi": 1, "ness": 2, " ": 3, "is": 4, "usual": 5}
ids = [4, 3, 0, 1, 2]

# Decode the IDs back into text: find the piece for each ID and join them.
# Expected output: is unhappiness
Knowledge check

Why are “hello” and “ hello” (with a leading space) usually different tokens?

Summary

  • Models work with tokens from a fixed vocabulary, each with a token ID.
  • Subword tokens balance vocabulary size and sequence length, and can represent any text.
  • Spaces, capitals and digits change how text splits: " hello" and "hello" differ, numbers are chunked.
  • Encoding turns text into IDs; decoding turns IDs back into text.
  • Every model family has its own tokenizer.

Next: byte-pair encoding, the algorithm that builds most vocabularies.

Finished reading? Tick it off to track your progress.