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.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: define special tokens as reserved structural vocabulary entries, name the categories (sentinels, PAD, role markers, tool-use markers), explain why the model treats them as boundaries via training, cite chat-template tokens for two model families, and flag the prompt injection vector.
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.
- Mistral chat models use `[INST]` and `[/INST]` to wrap user instructions; Mistral tool-use models add additional tokens for function calls.
- HuggingFace transformers exposes tokenizer.apply_chat_template() to insert the correct special tokens for any chat-tuned model.
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?
QHow does prompt injection via special tokens work in practice?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes 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.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.