NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Middle+0 views

How does BPE (Byte Pair Encoding) work?

Practice

Asked at

AI answer

BPE (Byte Pair Encoding) is an algorithm for building a subword tokenizer. During training, text is first split into initial units—often characters or bytes. The algorithm then repeatedly finds the most frequent adjacent pair of units in the corpus and merges it into a new unit. The resulting sequence of merges forms the subword vocabulary.

When tokenizing new text, these merges are applied in a specified order. Frequent words are often represented by a single token or a few larger pieces, while rare words can be split into smaller pieces. This reduces the vocabulary size compared with the “one token per word” approach and makes it possible to represent unfamiliar words. In the byte-based variant, the initial units are bytes, so arbitrary text can be encoded without a separate token for every character; in practice, the details of the specific implementation and preprocessing matter.