Chat templates and special tokens
Show how system, user and assistant turns are flattened into one token sequence using special role tokens. Explain why the model only ever continues that sequence and why a mismatched template degrades behavior.
Builds on: Supervised fine-tuning on demonstrations
What the animation says
Where do the roles live?
A language model does exactly one thing: given a single sequence of tokens, it predicts the next one. A chat looks richer than that. There's a system prompt, user messages, and assistant replies, each with a role. So where do the roles live? How does the model know when its reply starts, and when to stop? And why does a good model act strangely when you use the wrong format?
Flatten every turn
The answer: a chat template writes the whole conversation as one document. Each turn is wrapped in special tokens: a start token, the role name, a newline, the text, an end token, a newline. Every turn has that same shape, and the next turn always opens with a start token again. Here, the system turn is eight tokens, the user turn nine. To get a reply, the app appends one open header: start token, assistant, newline. Three more tokens, twenty in total. What comes next?
Continue, stop, and the loss
It writes four, then the end token. The app watches for that end token, and stops. So the model never really chats. It continues a document in the exact format fine-tuning drilled into it. Anything you put after the header gets continued too, like the first words of a reply. That's called a prefill. During fine-tuning on a document like this, the loss averages minus log probability, but only over the assistant's positions. Tap the positions that get loss.
Reserved tokens, and plain text
These special tokens are reserved ids. Each has its own row in the embedding matrix and its own column in the output matrix. Their values are learned mainly during fine-tuning, and only template code inserts them. If a user types the same characters, a tokenizer set up not to parse specials splits them into ordinary pieces: different ids, no boundary. Now a mistake. Same model, but the app sends plain text: User, the question, then Assistant, with no special tokens. What most likely happens?
Off-format falls back
It answers, then may write the next User line itself. Without special tokens, it looks like a web transcript. Nothing pushes it to emit the end token, so the stop rule may never fire. Off-format input is unlike the fine-tuning data, so behavior slides back toward plain document continuation. So remember: a chat is one token sequence. Turns are wrapped in role tokens, and it ends with an open assistant header. The model continues it until it emits its end token, and it only learned that exact format.
Flatten every turn into one document
The rendered prompt and the SFT loss
Flattening a three-turn chat
When the template doesn't match
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses