AI Dojo Open on the typewriterThe typewriter

Explainer

Chat template

1 min read
Assumes: you know what a token and fine-tuning are.
First published 29 October 2026. Last edited 16 November 2026.

The recipe that turns a list of chat messages into the exact text a model reads.

A chat model does not read a list of messages. A chat template turns them into one string, with special tokens that mark who is speaking and where each turn ends. Each model ships with its own template, and two models built on the same base can differ: Hugging Face shows one using [INST] markers and another using <|user|>, and warns that with the wrong control tokens these models perform drastically worse (huggingface.co). Train with the same template the model will see in use. During training, the prompt, the answer and those markers have to fit together inside the configured maximum length, which can be shorter than the model's own context maximum. Past that limit, extra tokens are cut only if truncation is on; otherwise the example may be rejected or must be shortened, split or filtered.

Going deeper

Three documented failure modes: the wrong control tokens; special tokens added twice when templated text is tokenized again; and a missing generation prompt at inference, which can make the model continue the user's message (huggingface.co). A misaligned end-of-sequence token stops replies ending cleanly (huggingface.co). The cheapest check is to decode one prepared training example and read it.

In the long read: What a training example looks like. Every related term is in Fine-tuning, every concept.