Tokenization
Токенизация
Tokenization turns text into a sequence of units called tokens. A token can be a whole word, part of a word, or punctuation, and language models usually receive these units as vocabulary IDs.
Also known as: токен
What counts as a token?
Take the sentence Wait, really? A search tokenizer might keep just Wait and really, while a language model's tokenizer might keep the punctuation and attach the space before really to the next token. Both are valid: a token is a unit defined by the tokenizer, not necessarily a word. Some tokenizers work with words, some with characters, and others with pieces built from characters or bytes.
How a language model gets from text to numbers
The tokenizer applies its splitting rules and looks up the resulting pieces in a vocabulary. Each vocabulary entry has an integer ID. The model uses those IDs to look up embedding vectors. The IDs are labels, not scores: two neighboring IDs do not necessarily represent similar pieces of text. Depending on the model and settings, the tokenizer may also add special IDs, such as one marking the beginning of a sequence.
Many models use subword tokenization. A common method, Byte Pair Encoding (BPE), learns merge rules by repeatedly combining frequent adjacent units in a training corpus. A common word may end up as one token, while a rare word can be represented by several smaller pieces. In byte-level BPE, the base vocabulary covers individual bytes, so even a character that has no token of its own can be encoded.
Worked example
Suppose a toy tokenizer preserves punctuation and allows a token to start with a space. One possible encoding is:
Text: "Wait, really?"
Pieces: ["Wait", ",", " really", "?"]
IDs: [27, 4, 81, 9]
Those IDs are invented. The leading space in " really" is part of the token, so "really" at the start of a line might have a different ID. In this example, looking up the IDs and joining the pieces recovers the string exactly. That isn't a universal guarantee: a tokenizer that lowercases text or discards whitespace can't always recover the original.
Why it matters in practice
Search systems tokenize documents and queries so that the same terms can match. For LLM applications, token counts determine how much fits in the context window, and many APIs bill by input and output tokens. A word-count rule of thumb can be misleading for code, punctuation-heavy text, or a different language. When the limit matters, count with the target model's tokenizer and account for any message formatting or special tokens added to the input.
In interviews
A common question is why not just split on spaces. That misses punctuation boundaries and doesn't work reliably for languages that don't consistently put spaces between words. Another question is whether you can swap tokenizers on a pretrained model. Generally, no: even if the new tokenizer has the same vocabulary size, it can assign different pieces to the IDs that the model's embedding table was trained to represent.