How many positions does playing take up? A tokenizer might keep the word whole or split it into play and ing. That's one position or two for exactly the same text. The split matters because the model does work for each position.
The model receives token IDs
In one lineThe network never reads strings. The tokenizer turns text into integer IDs, which only select embeddings; all later computation is on vectors.
The tokenizer hands the model a list of integers. Each integer points to a row in the embedding table. The network works with the vector in that row, not the numerical value of the ID.
Step 1: how text is split into tokens
In one lineThe tokenizer decides how long the sequence is; the embedding table decides which vector each ID starts from.
The segmentation and IDs below are illustrative, not output from a particular tokenizer:
"unbelievable!" → ["un", "believ", "able", "!"] → [431, 9821, 612, 5]
The model uses an embedding table to look each ID up as a vector:
$$x_t = E[\text{token\_id}_t], \qquad E \in \mathbb{R}^{|V| \times d}$$
$|V|$ is the vocabulary size and $d$ is the model dimension. The tokenizer decides how long the sequence is; the embedding decides which continuous vector each discrete symbol starts from.
Step 2: why not just split on words
In one lineA word list never ends, and characters make sequences long. Subwords split the difference: frequent pieces stay whole, rare words break up.
The intuition is reasonable, but a real-world word list has no end: new names, spelling variants, code, emoji, and other languages cannot all be listed. Making every character a token can produce longer sequences; characters absent from the base alphabet can still be unknown inputs.
A subword tokenizer is the compromise between the two: frequent fragments stay whole, and rare words are split into smaller units.
| Granularity | Advantage | Cost |
|---|---|---|
| word | short sequences, intuitive semantics | vocabulary explosion, severe unknown-word problem |
| character | combines known characters without listing every word | unseen characters still need handling; sequences can be longer |
| byte | a complete byte alphabet can represent any UTF-8 text | a base unit is not necessarily a character; sequences can be longer |
| subword | vocabulary size and sequence length reasonably balanced | segmentation depends on the corpus; boundaries do not always match human intuition |
Step 3: how BPE merges common fragments
In one lineBPE knows no roots or grammar; it keeps merging the most frequent adjacent pair. Merges are learned in order and applied in that order.
Byte Pair Encoding knows nothing about word roots and nothing about grammar. It just keeps asking: which two adjacent symbols appear together most often? Then it glues them together.
l o w </w>
l o w e r </w>
most frequent pair: (l, o) → lo
next round: (lo, w) → low
Each word occurs once, so both (l, o) and (o, w) have a count of two. Break the tie in favor of (l, o). Now lo sits next to w in both words, so the next merge produces low. Training saves that ordered list of merges. When encoding a new word, the tokenizer follows the learned order rather than counting pairs again. BPE for subword tokenization, §3.2
For two worked merges and the differences between BPE, WordPiece, Unigram, and byte fallback, continue to tokenizer algorithms. This page first completes the input pipeline.
Deeper: why token boundaries affect model behavior
If the same concept is 1 token in one language and 5 tokens in another, the latter takes up more context, more attention compute, and more prediction steps. How digits, whitespace, and code are split also changes the difficulty of arithmetic and copying tasks.
So the tokenizer is not inconsequential preprocessing; it defines the basic unit of the model's prediction problem.
Four objects that are easy to confuse
In one lineVocabulary, merge rules, normalizer and special tokens each do one job. PAD only fills out a batch; EOS is the signal to stop.
- Vocabulary: the static mapping between tokens and IDs.
- Merge rules / model: how raw symbols are combined into tokens.
- Normalizer / pre-tokenizer: how case, Unicode, whitespace, and the like are handled before text reaches the model.
- Special tokens: BOS, EOS, PAD, and the role boundaries in chat templates.
decode(encode(text)) should normally recover the text, but normalization can make the round trip not byte-for-byte reversible. PAD exists only to align sequences within a batch and should not be treated as content by the model; EOS is the real signal that generation should stop.
How a conversation becomes a string of IDs the model can read
In one lineA chat template writes messages in the training format before tokenizing. Role markers are just tokens, so the template must match the checkpoint.
1. Chat templates serialize role structure
An application may represent a conversation as
[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "How are you?"}
]
The Transformer does not process this message list directly. A chat template first serializes the messages into the format seen during training. A ChatML-style rendering might be
<|im_start|>system
You are a helpful assistant<|im_end|>
<|im_start|>user
How are you?<|im_end|>
<|im_start|>assistant
At inference time the final assistant marker has no answer after it; generation begins
there. Other checkpoints may use [INST]...[/INST], header tokens, or entirely
different formats. The chat template must match the checkpoint and tokenizer rather
than being copied across model families.
2. Special tokens are still vocabulary symbols
To the model, system, user, assistant, and message boundaries eventually become token
IDs. If <|im_start|> is registered as a special token, it normally maps to one ID;
otherwise it may be split into ordinary pieces. Whether a role name occupies one token
or several also depends on the vocabulary.
The architecture contains no hard-coded rule that says “system has highest priority.” The model learns the usual effect of these symbols from repeated combinations of role markers, message content, and target responses in its training data.
Do not copy numeric IDs from a screenshot. Special-token strings, IDs, stop sets, and embeddings are part of a specific tokenizer/checkpoint protocol and must be loaded again when the model changes.
3. The full boundary: messages → template → IDs → embeddings
The complete input pipeline is
$$ \boxed{ \text{structured messages} \xrightarrow{\text{chat template}} \text{serialized token text} \xrightarrow{\text{tokenizer}} \text{token IDs} \xrightarrow{\text{embedding table}} \text{vectors} } $$
Libraries often wrap template rendering and tokenization in one function, but their conceptual responsibilities remain distinct. The template determines the ordering of roles and message boundaries; the tokenizer selects vocabulary units; the embedding table turns integers into model-width vectors.
A chat template does not alter the attention equation. It gives an ordinary token sequence a learnable grammatical structure.
Avoid duplicating special tokens in actual code. In Transformers v5.20.0 (checked October 2026), if you render with apply_chat_template(tokenize=False) and tokenize separately, use add_special_tokens=False; tokenize=True combines the two steps and, because return_dict now defaults to True in v5, returns a dict with input_ids rather than a bare list of ids. Official guidance
Connecting the output shapes to the neural network
In one lineThe tokenizer hands over IDs shaped (B, T); the embedding lookup makes them (B, T, d), where the network takes over.
Suppose a batch holds 4 texts, the padded length is 12, and the model dimension is 768:
token_ids (B, T) = (4, 12)
attention_mask (B, T) = (4, 12)
embeddings (B, T, d) = (4, 12, 768)
The tokenizer ends at (B, T); the neural network begins at (B, T, d).
Experiment: compare different tokenizations
In one lineTrain a tiny BPE in plain Python: change the corpus and the same sentence splits differently; unseen characters become <unk>.
Run ../code/tokenizer_from_scratch.py. It trains a miniature BPE using only the Python standard library, with no extra dependencies. Change the training corpus and you can watch directly why the same sentence gets split into completely different tokens under another corpus.
This teaching implementation collapses whitespace, maps unseen characters to <unk>, and has no byte fallback. Its decode_tokens method reconstructs token strings, not a lossless round trip through IDs. Try repeated spaces and a character absent from the training corpus to see the distinction.
Self-check
In one lineThree questions: can you explain how a new word is split, why a new tokenizer changes inference cost, and where IDs become vectors?
- How can a new name be split into known pieces? What extra mechanism is needed if even its characters are unknown?
- Why can the same sentence cost a different amount of inference when the tokenizer changes?
- At which step does
(B, T)become(B, T, d)?
Where to read next
After the lookup, each position has a vector, but it knows nothing about its neighbors yet. For a model that carries information forward one word piece at a time, see RNNs and LSTMs. To stay on the Transformer route, follow “Next core note” below.
Quick learning: what a tokenizer does at the model boundary
In one lineThe template formats messages, the tokenizer assigns IDs, and the embedding table supplies vectors. A new checkpoint may need a different template.
What does each step do: chat template, tokenizer, embedding lookup?
Reference answer: The template writes down who said what in the format the model saw in training. The tokenizer turns that text into IDs; the embedding lookup turns those IDs into vectors. Special tokens marking message boundaries go through the same lookup. The Transformer doesn't receive message objects or have separate rules built into its architecture for system, user, and assistant.
Deep dive: why can tokenizers and chat templates not be swapped freely across models?
Whether a special string is a single token, which ID it maps to, and how the start and end of an assistant turn are written are all part of the model's training distribution. A template that does not match the vocabulary splits structural markers into pieces or maps them to the wrong IDs; even when the tensor shapes are fine, the protocol the model sees has already changed.