Representing language / 2 of 7

Tokenization: text into units

8 minOpening your notebook…

What you’ll learn

  • Explain the main idea of “Tokenization: text into units” in your own words.
  • Trace a concrete example and identify the assumptions behind it.
  • Apply the principle in a practice problem and recognize a common failure.

The idea, intuitively

A tokenizer converts text into units from a vocabulary. Tokens may be words, subwords, bytes, or characters. Token counts vary by tokenizer and language, so a word count is not a reliable context or cost estimate.

Language varies by speaker, setting, and purpose. A text representation is useful only in relation to a task. Keep the original source when possible so you can inspect what preprocessing discarded. English performance does not establish Indonesian performance, especially for slang, dialect, or mixed-language inputs.

Under the hood

Subword methods balance a finite vocabulary with the ability to represent unfamiliar strings. Token IDs are vocabulary indices, not semantic measurements. Production inputs must use the tokenizer paired with the model.

A useful caution

A tokenizer simulation must not be used as an exact billing or context estimate.

A worked example

A rare compound word may split into several pieces. The educational tokenizer below uses simple rules and does not reproduce any model tokenizer.

Try “AI belajar!”. The word-and-punctuation rule gives [“AI”, “belajar”, “!”]. The three-code-point rule gives [“AI”, “ ”, “bel”, “aja”, “r”, “!”]. The count changes from 3 to 6 because the second rule both keeps the space and splits the longer word.

How to use this way of thinking

Trace text through normalization, tokenization, representation, and prediction. Include language variation in evaluation and inspect examples that share words but differ in meaning. For generated language, assess faithfulness and task success separately from grammatical fluency.

See the mechanism

Educational simulationRuns in your browser with simplified rules and example settings.
Try this

Type “AI belajar!” and compare all three rules. Which one keeps the space? Count the pieces before looking at the result.

Opening the experiment…

Try it yourself

Compare word and character tokenization on “AI belajar”. What changes?

Key takeaways

  • An index in a tokenizer vocabulary
  • A tokenizer simulation must not be used as an exact billing or context estimate.
  • Check the assumptions with a small example before applying this idea to a larger system.

Check your understanding

0/2 checked

Use feedback to learn. Quiz scores and lesson completion are tracked separately.

1What is a token ID?
2True or false: The same text can have different token counts under different tokenizers.
Go to lesson completion

Further reading

Original papers and reference material for this school. External resources may be in English and require internet access.

Hugging Face NLP course (opens a new tab)Attention Is All You Need (opens a new tab)

Finish this lesson

Mark this lesson complete when you’re ready to continue.

Start with a question

Introduction to AIAI FoundationsA brief history of AIAI FoundationsTypes of AIAI FoundationsAI, ML, and deep learningAI FoundationsUnderstanding generative AIAI FoundationsEthics, limitations, and responsibilityAI FoundationsLinear algebra for learning systemsMathematics for AIVectors, matrices, and similarityMathematics for AICalculus: measuring changeMathematics for AIDerivatives and gradientsMathematics for AIProbability and statisticsMathematics for AIOptimization and objectivesMathematics for AI
K to open · Escape to close