# Byte-pair encoding (BPE)

> Canonical: https://skillalmanac.com/glossary/byte-pair-encoding | Last verified: 2026-11-25 | Index status: index-eligible

Byte-pair encoding (BPE) is a method for building a subword vocabulary: start from single characters or bytes, then repeatedly merge the most frequent adjacent pair into one new unit until the vocabulary reaches a target size.

Also called: BPE, byte pair encoding, byte-level BPE.

## In plain language

Byte-pair encoding is a way of deciding how to chop text into pieces before a language model reads it. Common words tend to end up as one piece, while rare words are split into smaller parts the model has seen before, so it can still handle a name or word it did not see in training. A rough comparison is a set of letter tiles where frequent combinations get their own single tile. The catch is that the pieces follow the training text, not meaning, so the same sentence can cost a different number of tokens in different models or languages. That matters when you budget a context window or estimate cost.

## Technical definition

BPE was applied to subword segmentation in neural machine translation by Sennrich, Haddow and Birch (arXiv 1508.07909), who describe it as a compression-based technique that lets a fixed vocabulary represent rare and unknown words as sequences of subword units. The Hugging Face documentation describes the training procedure: a pre-tokenizer splits text into words and counts them, a base vocabulary is made from the characters, and BPE iteratively merges the most frequent adjacent pair, adding each merge to the vocabulary until the target size is reached. Byte-level BPE uses the 256 byte values as the base vocabulary, so any text can be tokenized without an unknown token; the documentation says GPT-2 uses it. The documentation also describes BPE as deterministic, based on merge rules.

## How it shows up when you use agents

You do not usually see BPE directly. It shows up as the unit your agent is billed and limited in. When Claude Code, Codex or Cursor reports tokens used, those tokens are pieces from a tokenizer's vocabulary, and for many models that vocabulary was built with BPE or a close relative. The [tokenizer](/glossary/tokenizer) page covers the whole component; this page covers one algorithm inside it.

Practical consequences. The same file can be a different number of [tokens](/glossary/token) in a different model, because each model family has its own vocabulary. Code, non-English text and unusual identifiers can split into more pieces than plain English prose, so a count made by dividing characters by a fixed number can be off. Odd model behaviour on spelling or character counting can come from the model seeing pieces, not letters.

What breaks when this is misunderstood: people budget a [context window](/glossary/context-window) by word count, or reuse a token count from one model for another, and then hit limits or get surprise costs. Next action: measure with the tokenizer or token-counting tool of the model you will actually run, and read the usage report after a run.

## Common confusions

- **BPE versus tokenizer.** BPE is one algorithm for learning a vocabulary. A tokenizer is the full component that applies a vocabulary to text. Hugging Face lists BPE, Unigram and WordPiece as three subword algorithms.
- **BPE versus WordPiece.** Both merge pairs from the bottom up. Per the Hugging Face documentation, BPE merges the most frequent pair, while WordPiece merges pairs that maximize the likelihood of the training data.
- **Byte-level versus character-based BPE.** Byte-level BPE starts from 256 byte values, so any text can be represented. A character-based base vocabulary maps characters outside it to an unknown token.
- **Myth: a token is a word.** A token is a piece from the vocabulary. Common words stay intact as single tokens, and rare words are split into several.

## Worked examples in this catalogue

### [What Is an LLM? The Facts Agent Users Need](https://skillalmanac.com/learn/what-is-an-llm-for-agent-users)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A guide to how an LLM reads and predicts tokens, with a note that the same text produces different token counts on different models.

Why it exercises the concept: BPE is one common way a tokenizer's vocabulary is built. The guide shows the practical effect: token counts, and so cost and context use, depend on the tokenizer.

### [context-window-packer](https://skillalmanac.com/skills/skill-almanac/context-window-packer)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Chooses which files fit one prompt's token budget and records what was kept, shrunk or dropped, and why.

Why it exercises the concept: Fitting a token budget needs a token count, and that count comes from the model's tokenizer. The skill is an adjacent use, not a BPE implementation.

### [agent-session-cost-review](https://skillalmanac.com/skills/skill-almanac/agent-session-cost-review)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Finds where a coding-agent session spent its tokens, by parsing the saved session transcript.

Why it exercises the concept: Tokens are the unit a BPE vocabulary produces and the unit costs are measured in, so a per-session token breakdown is where its effect shows up.

## Related terms

- [Tokenizer](https://skillalmanac.com/glossary/tokenizer.md)
- [Token](https://skillalmanac.com/glossary/token.md)
- [Large language model (LLM)](https://skillalmanac.com/glossary/large-language-model.md)
- [Transformer](https://skillalmanac.com/glossary/transformer.md)
- [Context window](https://skillalmanac.com/glossary/context-window.md)

## Sources

- [Neural Machine Translation of Rare Words with Subword Units](https://arxiv.org/abs/1508.07909), arXiv (Sennrich, Haddow and Birch) (accessed 2026-11-24)
- [Tokenization algorithms](https://huggingface.co/docs/transformers/en/tokenizer_summary), Hugging Face (Transformers docs) (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
