BOS, EOS, PAD: why do tokenizers reserve these ids instead of using normal text?
Reserved ids prevent ambiguity: BOS, EOS, and PAD occupy positions normal tokenization never produces, so they can never collide with real content.
Imagine the tokenizer's vocabulary is mostly little pieces of words, but a few entries are not words at all. They are special markers, like invisible punctuation only the model can see: 'this is where the conversation starts', 'this is where this person stops talking', 'ignore this position, it is just padding'. These markers have their own numbered ids, but they never appear in ordinary text. The tokenizer inserts them when it knows it should, and the model learned during training that when it sees these ids, it should change behavior: stop generating, switch speaker, or skip the position. If a user accidentally types one of these marker strings as text, things can go wrong, which is why production systems sanitize input.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Most of a tokenizer's vocabulary is content: sub-word fragments the model uses to represent ordinary text. A small fraction is special: reserved ids carrying structural meaning. These special tokens are how the model knows when a conversation starts and ends, which speaker is talking, where a tool call begins, and when to switch output modes.
This deep dive defines special tokens precisely, walks the standard categories, explains how the model learned to interpret them, and covers the prompt injection risk that comes with letting user input touch them.
Special tokens defined precisely
Special tokens are reserved vocabulary entries carrying structural meaning rather than text content. They occupy ids in the same numbered vocabulary as content tokens. The embedding matrix has a row for each; the lm_head has a column. From a tensor perspective they are indistinguishable from any other token.
The distinction is semantic. Content tokens represent text fragments: 'token', 'ization', ' the'. When the model emits one, it is generating part of an answer. Special tokens represent boundaries or control signals: 'this sequence is starting', 'I am done generating', 'a tool call begins'. When the model emits one, the runtime reacts: stopping the sampling loop, parsing a tool call, switching roles.
The ids are arbitrary. cl100k_base assigns <|endoftext|> to id 100257 by convention. Any id would have worked with the same training. What matters is consistency between training and inference.
Special tokens are typically defined when the tokenizer is created. Adding new ones later requires extending the embedding matrix and lm_head, and the model has no learned behavior for them until fine-tuning teaches it.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- OpenAI's cl100k_base reserves ids like 100257 for `<|endoftext|>` and 100264 for `<|im_start|>`; the chat API applies the template to insert these around each message.
- Llama 3 and Llama 4 use `<|begin_of_text|>`, `<|start_header_id|>`, `<|end_header_id|>`, and `<|eot_id|>` as chat template tokens; exact formatting matters for quality.
What an interviewer would ask next. Try answering before peeking at the approach.
QThe model just emits ids. What actually stops generation when it produces EOS?
The sampling loop in the runtime checks each emitted id against a configured stop token set. On match, the loop exits. The model would happily emit more tokens after EOS; the runtime breaks. This is why misconfigured stop sets cause runaway generation.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Letting raw user input pass through the tokenizer with add_special_tokens=True, allowing a user to inject role markers like `<|im_start|>` and steer the model past the system prompt.
60 second bullets to scan on the way to the call.
Define a special token as a reserved vocabulary entry carrying structural rather than content meaning.
Name the standard categories: sentinels (BOS, EOS), PAD, role markers, tool-use markers.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.