A chat template wraps every turn with BOS and EOS tokens. Why does the model need them?
BOS anchors the input at the position-zero distribution the model was trained on; EOS is the termination signal the runtime checks to stop decoding. Omitting either breaks quality or causes runaway generation.
Think of a stage actor and a stage manager. The actor needs a curtain raising to know the scene begins, otherwise they start mid-sentence and the audience misses the setup. That curtain is the BOS token. When the scene ends, the actor bows, and that bow tells the stage manager to bring the lights down. That bow is the EOS token. The chat template is the stagehand who raises and lowers the curtain on cue, so the actor never handles the ropes. If you raise the curtain twice or forget it entirely, the performance looks wrong in a way that is hard to pin down but easy to notice.
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.
3 min: define BOS and EOS, name the Llama 3 versus older SentencePiece literal ids, explain why position zero matters, describe how the runtime uses EOS plus `<|eot_id|>` to stop decoding, and close on letting apply_chat_template own the insertion.
Real products, models, and research that use this idea.
- HuggingFace transformers AutoTokenizer applies the model's chat template (Llama 3, Mistral, Qwen) and inserts BOS plus role markers automatically.
- vLLM and SGLang both read tokenizer.eos_token_id plus additional stop ids from the generation config when serving Llama 3 instruct.
- Anthropic Claude and OpenAI GPT-5.5 hide BOS and EOS behind the chat API; the equivalent boundaries are the role fields on each message.
- Ollama and llama.cpp surface a chat_template field in GGUF metadata so the same BOS and EOS handling carries across runtimes.
What an interviewer would ask next. Try answering before peeking at the approach.
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.
Calling apply_chat_template and then manually prepending BOS again. The template already inserts it, so the model sees two BOS tokens and operates out of distribution.
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.