A model never sees your text. It sees token ids, and the thing that decides which ids is a tokeniser that was trained before the model was. Understanding it is unusually practical, because tokens are the unit you are billed in, the unit the context window is measured in, and the reason some perfectly ordinary strings behave strangely.
Byte pair encoding starts with individual characters and repeatedly merges the most frequent adjacent pair, recording each merge as a rule. Training produces an ordered list of those rules. Encoding replays them: find the highest-ranked rule that matches anywhere in the sequence, apply it, repeat until nothing matches. The brackets in the diagram are those merges, stacked in the order they fired, so you can read the history of how each token was assembled.
The greedy-by-rank detail matters. The algorithm does not scan left to right, it always applies the best available rule wherever it is, which is why the result depends on the whole merge table rather than on local context. It is also why token boundaries do not line up with what you think of as words: a boundary can fall mid-word, and the same substring can be split differently depending on what surrounds it. The mapping itself is exactly reversible, so decoding recovers your text precisely. It is the segmentation that is unintuitive, not the encoding.
Drag the vocabulary slider down. Rules disappear from the end of the table, common sequences stop collapsing, and the token count rises for identical text. That is the entire tradeoff a tokeniser makes: a larger vocabulary means fewer tokens per document and a larger embedding matrix to pay for it.
Try the rare word and the numbers samples. A long uncommon word fragments into pieces, which is why rare terms cost several times more than common ones of the same length. Numbers are worth watching too, though the story there has moved on: older tokenisers merged digits into arbitrary multi-digit chunks, so "1024" and "1025" could have completely unrelated segmentations, and recent models deliberately split numbers into fixed groups of digits instead. Consistent grouping was the fix, not fewer pieces.
One honest caveat. The algorithm here is the real one, but the merge table is forty-one hand-written rules rather than a trained vocabulary of a hundred thousand. That is why the characters-per-token figure comes out around two, where a production tokeniser averages closer to four on English prose. The behaviour is faithful; the compression is not.
The maths
- The merge rule
Training repeatedly finds the most frequent adjacent pair, merges it into one symbol, and records the rule. The rank numbers on the brackets are the order those rules were learned, so lower rank means more frequent. This is a greedy heuristic, not an optimisation: nothing guarantees the resulting vocabulary is the best one of its size.
- Encoding
Encoding replays the rules greedily by rank, not left to right: the best-ranked applicable rule fires wherever it sits in the string. That is why the result depends on the whole table rather than on local context.
- The vocabulary trade
Every merge rule adds one token to the vocabulary and one more row of d parameters to the embedding matrix. The slider removes rules from the end of the table, which is exactly what a smaller trained vocabulary is.
Related terms
- TokenA token is the unit of text a model reads and writes: a chunk that is usually part of a word, not a whole word or a single character. Everything is measured in tokens, including your context window and your bill.
- Input tokensInput tokens are the tokens you send in a request: the system prompt, the conversation history, loaded files, and tool definitions. You are billed for them, and they count against the context window.
- Output tokensOutput tokens are the tokens a model generates in its response, including any hidden reasoning. They are usually priced higher than input tokens, and turning up effort produces more of them.
- Context windowThe context window is the maximum amount of text, measured in tokens, that a model can consider for a single request. It is a hard ceiling, and it is the main resource you manage when working with an agent.