Topic 1 of 4 · 3 min read
Roles and chat templates
Goal: Turn a short conversation into the single sequence a model actually reads and point out the role markers.
A chat is not a conversation to the model. It is one long piece of text, with each message wrapped in special marker tokens that say who is speaking.
Check yourself · 1 of 3
What does a chat template do before the model sees a conversation?
Why it matters
In ChatGPT, Claude, Gemini or Copilot you see bubbles. The model sees none of that. Knowing what it does see explains odd behaviour: why a pasted line can act like a command, why a long chat gets slower and costlier, and why the same model can behave differently in two apps.
How it works
Every message has a role:
- System. Rules and background set by the app or developer. The user usually cannot see it.
- User. What you type.
- Assistant. What the model wrote earlier in this chat.
- Tool. Results sent back from a search, a file read or another tool the model asked to use.
Before each call, the app turns this list of messages into one sequence using a chat template. The template adds special tokens, which are reserved symbols that mark where a message starts and ends and which role it belongs to. The exact markers differ between model families, so a template made for one open-weight model does not fit another. Finally the template adds an opening marker for the assistant, and the model continues from there by predicting one token at a time.
So "the assistant's reply" is just the model continuing a document that looks like a transcript.
Before and after
Weak mental model: "I told the assistant to be brief, so it is a setting it holds."
Better: "My instruction is a few tokens in a transcript. It only counts while it is still in that transcript, and it competes with every other token there."
A simplified view of what the model reads (real markers differ by model):
<|system|> You are a helpful travel assistant. <|end|>
<|user|> Plan a day in Lisbon. <|end|>
<|assistant|>
What goes wrong
- People think roles are enforced by the software. They are a convention the model learned in training. A model can still treat text in the user turn as more important than the system turn, or the reverse.
- People copy a prompt from one tool to another and forget the template is different. Chat-tuned models use their own markers, and a wrong template gives weaker answers.
- People paste text that contains fake role markers. If the app does not strip them, they can confuse the structure.
Optional code branch
Developers usually pass a list of messages and let a library apply the template:
messages = [
{"role": "system", "content": "You are a travel assistant."},
{"role": "user", "content": "Plan a day in Lisbon."},
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
print(text) # one string with role markers, ending where the assistant starts
Examples
One chat, one sequence
A user asks "Plan a day in Lisbon" with a system line "Be brief". The app joins both with markers and ends with the assistant marker. The model's job is only to continue from that point, so both lines are just context it predicts from.
Wrong template
A team runs an open-weight chat model through a plain text API and pastes the question with no markers. Answers drift and ignore the system line. Applying the model's own chat template fixes it, because the model was trained on that exact layout.
Key points
- The model reads one sequence of tokens, not separate chat bubbles.
- Roles (system, user, assistant, tool) are labels added by a chat template using special tokens.
- Different model families use different templates, so markers are not interchangeable.
- Roles are a learned habit of the model, not a hard security wall.
- The assistant's reply is the model continuing the transcript from the assistant marker.
Common mistakes
- Thinking the system message is a locked setting rather than text in the sequence.
- Reusing one model's prompt markers with a different model.
- Assuming the assistant 'sees' who said what in the way a person does.
- Forgetting that tool results are also messages in the same sequence.
Check yourself · 2 of 3
Which role holds a web search result that the model asked an app to fetch?
Check yourself · 3 of 3
A developer moves a prompt from one open-weight model to another and answers get worse. What is the most likely first thing to check?
Practice items, not counted towards your mastery.
Start here
- Chat templates (Hugging Face Transformers docs) (opens in a new tab)DocsFree
Shows how a list of role-tagged messages is rendered into the single text a model reads, with special tokens.
- Working with messages (Claude docs) (opens in a new tab)DocsFree
Shows that each API call carries the full conversation and that the API keeps no state for you.
- System prompts release notes (Claude docs) (opens in a new tab)DocsFree
Anthropic publishes the real system prompts used in its apps, so you can read what a hidden prompt looks like.