liveBook · Manning

Byte pair encoding

As explained in Generative AI in Action

Byte-pair encoding is a reversible, lossless compression and subword-tokenization algorithm that repeatedly merges frequent symbol sequences.

Byte-pair encoding (BPE) repeatedly merges frequent symbol sequences into reversible, lossless subword tokens. It produces shorter sequences, handles text absent from tokenizer training data, and helps generalize recurring language patterns such as the English “-ing” ending. section 2.8.3

BPE matters operationally because tokenization affects:

  • Context: overly short input may lack useful context, while overly long input may reduce response coherence. section 2.8.3
  • Cost and performance: smaller token sequences and efficient tokenizers support enterprise model decisions. section 2.8.3
  • Interoperability: tokenizers are designed for corresponding language models and their pretraining or fine-tuning workflows. section 2.8.3

Counting tokens with tiktoken

Use tiktoken to select an encoding, encode a prompt, and count the resulting tokens. The example prompt “I have a white dog named Champ” produces seven tokens with cl100k_base.

How Generative AI in Action covers it

How deep it goes
Defines compression and tokenization behavior with examples, but stops short of explaining the merge-training procedure or mathematical internals.
Examples
Demonstrates English “-ing” subwords, token counting for a dog-related prompt, and reversible encoding and decoding.
What it assumes
Assumes basic awareness of language-model inputs and outputs, while teaching token counts, reversible encoding, and model-specific encodings.
What you can do after
Apply it via tiktoken to count, encode, and decode model-compatible token sequences.
How it connects
Connects tokenization with prompt engineering, text generation, text classification, machine translation, pretraining, and fine-tuning.

Also explained in

Compare how 11 books explain byte pair encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.