Chat templates explained in 2 minutes

What it is. A chat template is the built-in recipe that wraps a conversation in the special markers a model was trained to expect, before the model reads it.

An analogy. Think of a form with labelled boxes: “Instructions”, “User said”, “Assistant replies”. You type a plain question, but the model never sees it plain. The template quietly copies it into the right box, adds the instructions box (the system prompt), and leaves the “Assistant replies” box open for the model to fill in. Each model family uses its own labels, so the template has to match the model. Use the wrong one and the model can ramble, mix up who said what, or ignore its instructions.

You almost never see the template. Apps such as chat assistants and local tools apply it for you, every single time you send a message.

Hugging Face’s documentation describes chat templates as part of a model’s tokenizer: they turn a list of messages into the single string of text the model actually reads (Hugging Face). Model files in the popular GGUF format, used by tools such as llama.cpp and Ollama, can carry their template inside the file.

Why it was in the news this week. Security firm Malspark found five models on Hugging Face whose attackers had hidden instructions in the chat template. Because the template sits between you and the model, those hidden lines shaped every reply: some told users to run a command, others targeted AI agents that can use a computer’s command line (AIooze brief).

One thing to remember. A downloaded model is software, not just data. Treat any command it suggests as untrusted, and, as Malspark advises, use a reviewed chat template rather than the one bundled with a model you do not trust.


Related