ModelRefs / Byte-Pair Encoding (BPE) — AI Glossary

Byte-Pair Encoding (BPE) — AI Glossary

A subword tokenization algorithm that merges frequent character pairs iteratively to build a vocabulary of variable-length tokens.

Overview

BPE (Sennrich et al. 2016) starts with a character vocabulary and merges the most frequent adjacent pair at each step until vocabulary size V is reached. Used by GPT-2/3/4 (via tiktoken) and RoBERTa. Balances vocabulary coverage with OOV handling and is efficiently implemented with Rust-based tokenizers.

Reference details

Topicarchitecture
Also known asBPE, byte pair encoding
Last reviewed2026-06-24

Example: The first merge, counted by hand

Corpus: “banana” ×3 and “bandana” ×4, split to characters. Count every adjacent pair. “an” appears twice in banana (2×3 = 6) and twice in bandana (2×4 = 8) — 14. “na” appears twice in banana (6) but once in bandana (4) — 10. “ba” appears once in each — 7. “an” wins, so merge rule #1 is a+n → “an”, and both words are rewritten (b·an·an·a, b·an·d·an·a) before the next round counts pairs again. Repeat until the vocabulary reaches its target size. That ordered list of merges is the tokenizer — which is why a vocabulary cannot be edited after training without invalidating the learned embeddings.

Commonly confused with

BPE is the merge algorithm, not the tokenizer product. tiktoken and Hugging Face tokenizers both implement BPE but over different pre-tokenization rules and byte handling, so the same text yields different token counts under each. “Which tokenizer” is a more precise question than “which algorithm” when you are costing a prompt.

When to use it

Reach for it when:

  • Training a model from scratch on text where subword coverage matters more than morphological correctness
  • You need deterministic, reversible tokenization with no out-of-vocabulary path
  • Estimating cost or context fit — count with the target model's own vocabulary, not a proxy

Reach for something else when:

  • Reasoning about non-Latin scripts by character count: one CJK character often costs several tokens
  • Assuming token counts transfer between providers — they do not, even for identical text
  • Domain vocabularies where whole-word terms fragment badly; an adapted vocabulary may be worth the retraining cost

Primary source

Continue your research

Use these connected ModelRefs sections to compare alternatives, inspect implementation paths, and review the evidence and governance boundaries relevant to Byte-Pair Encoding (BPE) — AI Glossary.

Frequently asked questions

What is Byte-Pair Encoding (BPE)?

A subword tokenization algorithm that merges frequent character pairs iteratively to build a vocabulary of variable-length tokens.

Is Byte-Pair Encoding (BPE) the same as BPE?

Yes — BPE, byte pair encoding are common aliases for Byte-Pair Encoding (BPE).

What concepts are related to Byte-Pair Encoding (BPE)?

Closely related concepts include sentencepiece, tiktoken, vocabulary size, tokenizer.