What is an AI chat template?

A chat template is the rule converting a list of messages split into system, user and assistant roles into one text sequence with the special tokens and format the model saw during fine-tuning; each instruct model has its own template.

Unlike a prompt template, which is the application's text pattern for placing, say, a question and context, a chat template sets the role markers and separators with which that text reaches the model. It is usually distributed with the model's tokeniser.

Topic
Prompts, context, reasoning and generation
Also known as
Chat format

How an AI chat template works

  1. The application prepares the conversation as a list of role and content pairs.
  2. The template adds model-specific role markers and separator tokens around each message.
  3. Finally it adds the marker where the assistant's reply begins; the model generates from there.
  4. In templates that support tool use, tool definitions and tool results are formatted with their own roles.

In more detail

Jinja2 template structure and add_generation_prompt

Modern open-weight models implement chat templates using Jinja2 expressions stored in `tokenizer_config.json` or a dedicated `chat_template.jinja` file. These templates handle conditional logic such as injecting the `add_generation_prompt` marker, rendering JSON tool schemas and stripping or preserving reasoning blocks across multi-turn conversations.

The risk of special token injection

If user messages contain literal special tokens (such as `<|im_end|>` or role headers) and the inference pipeline encodes them as control tokens rather than plain text, attackers can spoof system or assistant turns. Production servers should sanitise or disable special-token parsing on untrusted user strings.

Reasoning tags, the developer role and multimodal blocks

Chat templates commonly support additional roles and content blocks beyond `system`, `user` and `assistant`, including `developer` roles, structured `tool` calls/responses, `...` reasoning spans and multimodal placeholders for images or audio. Using an outdated template drops reasoning toggles or breaks multimodal indexing.

Commonly confused terms

TermHow it differs
TokeniserA tokeniser converts text into token IDs and back; a chat template (typically written in Jinja2 inside the tokeniser config) formats structured role and tool messages into text right before tokenisation.
System promptThe system prompt is the content assigned to the system (or developer) role to steer model behaviour, whereas the chat template defines the exact delimiters that isolate that role from user turns.

Why an AI chat template matters for on-premises AI

Instruct models saw a specific format in training, so input with a wrong or missing template lowers quality and can blur roles and weaken the system prompt. Inference library documentation recommends storing the template with the model and applying it automatically. Chat-tuned open model families are trained for dialogue, so the correct template is the condition for benefiting from that training. When switching models in-house, remember the template changes too.

ON-PREMISES EXAMPLE

After upgrading the model behind its in-house assistant, a public body sees answers degrade; it finds the inference server still uses the old model's chat template, enables the new one and reruns its evaluation set.

Common questions

What happens with the wrong chat template?

The model does not get input in its training format; quality drops, roles can blur and the system prompt can weaken.

Where is the chat template defined?

Usually in the model's tokeniser configuration; inference servers read and apply it to messages automatically.

Sources

In the glossary: Glossary, A to D

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.