liveBook · Manning

Tokenizer

As explained in How Large Language Models Work

A tokenizer is a software component or algorithm that converts text into tokens according to a vocabulary and tokenization procedure.

A tokenizer converts text into tokens and then represents those tokens as numbers an LLM can process. Tokens may be whole words, word pieces, or subwords. Its vocabulary is the collection of available tokens, built by scanning training data and assigning each discovered token a unique identifier. section 2.2.1 section 2.6

A typical tokenization pipeline includes:

  • Normalizing text into a standard representation, which can include changing case or standardizing Unicode byte encodings. section 2.6
  • Segmenting the normalized text into words or subwords that can be processed independently. section 2.2.3
  • Using byte pair encoding (BPE) to repeatedly find frequent character combinations, replace them with tokens, and add them to the vocabulary. section 2.2.3
  • Balancing vocabulary size: more tokens can represent text efficiently, but increase computation, memory, and storage requirements. section 2.2.1

Why token counts behave unexpectedly

BPE may tokenize “running” as one token while splitting the slightly shorter “runnin” into “run” and “nin.” It favors frequent strings rather than counting characters, so sentence length does not reliably predict token count. The same implementation detail can affect tests, model evaluations, costs, and processing time. section 2.2.4

Figure 2.5 A simplified byte pair encoding algorithm for creating tokens: first, find the most frequent pair of characters “ng.” Next, replace all instances of “ng” with a placeholder token “T,” and add “ng” to the vocabulary. Repeat the process until no common byte pairs remain.
figure

Use the BPE diagram to follow how frequent character pairs become vocabulary entries and how repeated merging produces word and subword tokens. section 2.2.3

Figure 2.8 A comparison of how two LLMs learn to perform arithmetic computations over time. Time is shown on the x-axis. The upper curve is a typical BPE tokenizer, while the lower curve is the same tokenizer modified to use tokens that represent individual digits. The y-axis describes the ability of the LLM to perform accurately, where a smaller number means fewer errors. The bottom line is that LLMs that use digit-level tokenization can learn how to do math better and faster.
figure

Compare ordinary BPE with digit-level tokenization: representing each digit separately improves the learning and accuracy of arithmetic computations. section 2.3.2

Tokenization is not neutral. Homoglyphs can make visibly identical characters carry different encodings, creating confusion or security risks. Languages represented with finer-grained subwords may consume more tokens, increasing latency and usage charges. Programming code can still benefit from shared tokens for indentation, identifiers, operators, syntax, and comments. section 2.2.4 section 2.3.3 section 6.1

How How Large Language Models Work covers it

How deep it goes
Traces normalization, segmentation, BPE vocabulary construction, numeric encoding, and practical limitations without detailing model internals.
Examples
Uses running versus runnin, loquacious, digit-separated numbers, homoglyphs, multilingual text, and Python–Java code comparisons.
What you can do after
Apply the OpenAI tokenizer to inspect token boundaries, compare casing and phrasing, and anticipate counting, testing, cost, and arithmetic effects.
How it connects
Links tokenization with vocabulary, normalization, segmentation, byte pair encoding, Unicode, arithmetic, language equity, and programming languages.

Also explained in

Compare how 16 books explain tokenizer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of How Large Language Models Work. The section links point to the parts of the book it draws on.