Highlights

Contents

Part IX
09

Chat Templates

Chat roles need a format the model recognizes. The template supplies that learned structure. Trace generation prefixes, thinking blocks, tools, and padding without substituting another checkpoint’s conventions.

YOU ARE HEREmessageschat templatetokenizertoken IDsconfig.jsonmodel.safetensorsint8 / int4 weightsOUR DECODER, REBUILT FROM THE CONFIGprefilldecodeattention kernelKV cacheparity checkvs referenceLM head → logitssamplingdetokenizereply textnext token

#Section 48Why Chat Models Need Templates

Qwen3-0.6B comes in two releases. Qwen3-0.6B-Base is the pretrained model: it continues text. Give it “What is the capital of France?” and it might continue with another quiz question, because lists of questions are common on the web. Qwen3-0.6B, the one this chapter uses, was post-trained on conversations: it answers. That is the difference between a base model and an instruction or chat model, and the post-training conversations all had one specific shape.

The model has no notion of “messages”. Conversations are serialized into one token sequence: every message is written out in order, and the only thing marking where one speaker stops and the next begins is a set of control tokens placed at the role boundaries. The model learned during post-training to read those markers: text after <|im_start|>user is a request, text after <|im_start|>assistant is its own voice, <|im_end|> closes a turn. A chat template is the function that produces this serialization, and it lives in the checkpoint’s tokenizer_config.json as a Jinja program, so every library renders conversations the same way the model was trained.

MESSAGES IN, ONE TOKEN SEQUENCE OUTsystemYou are a helpful assistant.userWhat is the capital of France?assistantParis.template<|im_start|>system…<|im_end|><|im_start|>user…<|im_end|><|im_start|>assistantParis.<|im_end|>dark: control tokenscolour: contentthe model only ever sees the strip on the right
Figure 4.61Three messages become one token sequence. The model never sees message objects, only the control tokens that mark where each speaker starts and stops.
PICTURE THIS: THE SAME LINES, WITH AND WITHOUT THE SCRIPT FORMATformatted: the actor knows when to speakHAMLETWho is there?GUARDNay, answer me.HAMLETLong live the king!GUARD…names stripped: whose line is next?Who is there?Nay, answer me.Long live the king!…role markers are the names in capitals
Figure 4.62The theatre script of the analogy. The same four lines read clearly with speakers named in capitals, and become ambiguous without them.

#Section 49Conversation Roles

Qwen3’s template knows four roles. A system message holds standing instructions for the whole conversation. A user message is the person’s turn. An assistant message is the model’s turn, which may contain reasoning, a reply and tool calls. A tool message carries a tool result back to the model after a tool call. The roles are written as ordinary words after <|im_start|>: system (token 8948), user (872), assistant (77091).

Role in the message list Rendered as
system <|im_start|>system\n … <|im_end|>\n, first only
user <|im_start|>user\n … <|im_end|>\n
assistant (in history) <|im_start|>assistant\n reply <|im_end|>\n, thinking removed
tool inside a user turn: <tool_response>\n … \n</tool_response>

Two rows surprise people. Tool results are not given their own role marker in Qwen3: the template wraps them in <tool_response> tags inside a user turn, and several consecutive tool results share one user turn. And past assistant messages lose their reasoning. A multi-turn conversation of a system message, a question, an assistant reply that contained <think>\nEasy.\n</think>\n\nParis., and a follow-up question renders to 42 tokens, in which the past reply is just Paris.. The template strips the thinking from every assistant turn before the latest user message, because the model was trained that way and because old reasoning would waste context (Section 53).

#Section 50Chat Template Structure

Every Qwen3 conversation has the same skeleton, which Part VIII printed token by token.

Beginning of conversation. No BOS token. The first token is <|im_start|>, opening either the system turn or, with no system message, the first user turn. Qwen3’s template adds no default system prompt; Qwen2.5’s added “You are Qwen, created by Alibaba Cloud. You are a helpful assistant.” when none was given, which is one of the differences between the two families’ templates.

Role markers are <|im_start|> followed by the role name and a newline. Message boundaries are the newline after <|im_end|>, so each turn is followed by token 198 before the next <|im_start|>. End-of-turn markers are <|im_end|>, token 151645, at the end of every message.

The assistant-generation prompt is what makes the model speak. With add_generation_prompt=True, the template appends <|im_start|>assistant\n after the last message, and stops. The model’s next token is therefore the first token of its reply. Without it, the sequence ends after the user’s <|im_end|>\n, and the model has to guess what comes next; it usually writes <|im_start|>assistant itself, but not reliably. Our running prompt is 26 tokens with the generation prompt and 23 without.

EOS behavior closes the loop. The model ends its reply by generating <|im_end|>, 151645, the same marker that ends every other turn. That is why eos_token_id in config.json is 151645 and why generation must stop on it. generation_config.json lists 151643 as a second stop token, for the rarer case where the model emits <|endoftext|>.

ONE TURN, THEN THE GENERATION PROMPT<|im_start|>user\nWhat is the capital of France?<|im_end|>\nrole markercontent: 7 tokensend of turnboundary<|im_start|>assistant\nthe model writes heregeneration prompt (add_generation_prompt=True)… ends with <|im_end|> = EOS 151645
Figure 4.63One Qwen3 turn, annotated. A role marker opens it, the content follows a newline, an end-of-turn token closes it, and a newline separates it from the next; the generation prompt is an assistant turn opened and left empty.

The template that produces all this is short. Stripped of the tool and thinking branches, its core is:

text
{%- for message in messages %}
    {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
{%- endfor %}
{%- if add_generation_prompt %}
    {{- '<|im_start|>assistant\n' }}
{%- endif %}

#Section 51From Messages to Tokens

A chat request goes through four steps before the model sees it.

  1. A structured message list: [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is the capital of France?"}].
  2. Template rendering: the Jinja template turns the list into one string, <|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\nWhat is the capital of France?<|im_end|>\n<|im_start|>assistant\n.
  3. Tokenization: the string becomes 26 IDs, with the control strings matched as special tokens (Part VIII).
  4. The final model input: three tensors.
python
text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
input_ids = tok(text, return_tensors="pt").input_ids     # (1, 26)
attention_mask = torch.ones_like(input_ids)               # (1, 26), all real tokens
position_ids = attention_mask.cumsum(-1) - 1              # (1, 26): 0, 1, ..., 25

The attention mask marks which positions are real tokens (1) and which are padding (0). For one prompt it is all ones. The position IDs number the real tokens from 0, and they drive RoPE.

Generation batches commonly pad on the left, aligning the final real positions. Derive position IDs from the attention mask so padding does not shift a short prompt’s rotary positions.

MESSAGES → TEXT → IDS, MASK, POSITIONS; AND A LEFT-PADDED BATCHmessageslist of dictsrenderJinja templatetokenizespecials matchedtensorsids, mask, positionsbatch row 2: the 15-token prompt, padded on the left to 26idsPPPPPPPPPPPmask00000000000111111111111111pos111111111110123456789101112131411 pad tokens (151643), maskedreal tokens: positions 0 to 14positions = running count of mask − 1, so RoPE sees the real token orderpad left, so every row ends together
Figure 4.64From messages to model input, and what left padding does in a batch. The short prompt is padded on the left, masked there, and its real tokens still get positions starting at 0.

#Section 52System Prompts

Placement. The system message is rendered first, in its own turn, and only the first message may be a system message in the conventional sense; a later system message is rendered as an ordinary turn with the role name system. When tools are provided, Qwen3’s template appends the tool definitions to the system turn (Section 54).

Persistent instructions. A system prompt is not stored anywhere special. It is just tokens at the start of the sequence, which every later token can attend to. “Persistent” means only that the application sends it with every request and the model was trained to give it weight.

Interaction with later user messages. Because it is just context, a later user message can contradict it, and what wins is a matter of training, not mechanism. Qwen3 documents one deliberate interaction: with thinking enabled, a user can write /think or /no_think in a message to switch reasoning on or off for that turn, and the model follows the most recent instruction.

A shared system prefix can reuse its KV states when its tokens and all computation dependencies are identical. Prefix caching avoids repeated processing but still retains cache memory.

PREFIX CACHING: SAME SYSTEM PROMPT, COMPUTED ONCEsystem + tools: 138 tokensKV: 15,826,944 bytes, oncerequest A: "Weather in Paris?"request B: "Is it raining in Oslo?"request C: "Forecast for Lima"only the orange part is prefilled per requestshared rows,read by all
Figure 4.65Prefix caching. Requests that start with the same system prompt share its KV cache rows; only the part after the shared prefix is prefilled per request.

#Section 53Thinking / Reasoning Formats

Qwen3 is a hybrid reasoning model: it can write out reasoning before answering, or answer directly. The reasoning is ordinary generated text wrapped in two added tokens, <think> (151667) and </think> (151668). Visible vs hidden reasoning markers is then a question for the application, not the model: the model always generates the reasoning tokens, and the application decides whether to show them, hide them or log them.

Thinking control tokens structure the output. In thinking mode the model’s reply looks like <think>\n reasoning \n</think>\n\n answer <|im_end|>. To separate them, find the last </think> by ID and split there; Qwen’s own example code searches for the last occurrence of 151668. Do not use skip_special_tokens to hide the reasoning: it does not remove these tokens, because they are not flagged as special (Section 44).

Disabling thinking changes the generation prompt by supplying an already-closed empty think block. The switch changes the input format, not the learned weights.

THE END OF THE PROMPT IN EACH MODE (REAL QWEN3 TOKENS)enable_thinking = True: 26 tokens…<|im_start|>assistant\nmodel writes <think> reasoning </think> answerenable_thinking = False: 30 tokens…<|im_start|>assistant\n<think>\n\n</think>\n\n4 tokens pre-filled: 151667, 271, 151668, 271answer directly
Figure 4.66The two generation prompts. With thinking on, the assistant turn is opened and left empty; with thinking off, the template pre-fills an empty think block, four tokens, so the model starts directly on the answer.

Why the template must match model training. Each of these behaviours, the empty think block, the stripping of old reasoning from history, the soft /no_think switch, works only because the model saw exactly that format during post-training. Render the same conversation another way, for instance keeping old reasoning in the history, and the input is now unlike anything the model learned from. It will still produce text. It will just be somewhat worse, in ways no error message will ever report.

#Section 54Tool-Call Formatting

A tool call is the model writing a structured request that your code executes. Qwen3’s template sets it up in four pieces, and here is a real one, rendered from a single weather tool and a three-message conversation, 190 tokens in all.

text
<|im_start|>system
# Tools

You may call one or more functions to assist with the user query.

You are provided with function signatures within <tools></tools> XML tags:
<tools>
{"type": "function", "function": {"name": "get_weather", "description": "Current weather for a city", "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}}
</tools>

For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call><|im_end|>
<|im_start|>user
Weather in Paris?<|im_end|>
<|im_start|>assistant
<tool_call>
{"name": "get_weather", "arguments": {"city": "Paris"}}
</tool_call><|im_end|>
<|im_start|>user
<tool_response>
{"temp_c": 18}
</tool_response><|im_end|>
<|im_start|>assistant

Tool schemas in the prompt: each tool’s JSON schema goes inside <tools></tools> in the system turn, one per line, followed by instructions on how to call them. That block costs 138 tokens for this one small function, before the user has said anything. Tool-call control tokens: the model answers with <tool_call> (151657), a JSON object, and </tool_call> (151658), then ends its turn with <|im_end|>. Note that in the instructions block, the same <tool_call> tokens appear as examples; they are real control tokens there too.

Structured arguments are JSON: {"name": "get_weather", "arguments": {"city": "Paris"}}. Your code parses it, validates it against the schema (the model can and does produce invalid JSON or wrong argument types sometimes), runs the function, and appends a message with role tool. Tool results are rendered inside a user turn as <tool_response>\n{"temp_c": 18}\n</tool_response>. Returning control to the assistant is then just the generation prompt again: the template appends <|im_start|>assistant\n, and the model, now able to read the tool result, writes its answer.

ONE TOOL CALL, ROUND TRIPmodel<tool_call>{"name": "get_weather", "arguments": {"city": "Paris"}}</tool_call>your code: parse,validate, run<|im_start|>user<tool_response>{"temp_c": 18}</tool_response>1 generates2 ends turn3 role "tool"4 + <|im_start|>assistantthe modelreads the resultand answers
Figure 4.67The tool-call loop. The model writes a call between tool_call tokens and ends its turn; your code runs the function and appends the result as a tool message; the template wraps it and reopens the assistant’s turn.

#Section 55Chat-Template Failure Modes

Template bugs never crash. They produce a prompt the model was not trained on, and the model does its best. The common ones:

Failure What happens How to catch it
Missing role markers (raw text to a chat model) the model continues text instead of answering, or answers in an odd register always render through the template
Double BOS/EOS tokens extra start or end tokens the model never saw at the start of a prompt inspect the first and last few IDs
Incorrect assistant prefix (no generation prompt, or “Assistant:” as plain text) the model may continue the user’s message or write its own role marker check the prompt ends with <|im_start|>assistant\n
Wrong special tokens (another family’s markers) the markers are tokenized as ordinary text pieces check that control strings became single IDs
Wrong template for checkpoint defaults, thinking handling and tool format differ from training use the template shipped with the exact checkpoint
Context corruption (old thinking kept, a turn cut in half by truncation, tool results unwrapped) gradual quality loss, odd behaviour late in conversations truncate whole turns, re-render the history through the template

Two of these need a word more. Double BOS is mostly a problem for models that use one, such as Llama 3, whose template writes <|begin_of_text|> and whose tokenizer, called with add_special_tokens=True on the rendered text, adds a second. Qwen3 has no BOS, so the equivalent mistake is adding <|endoftext|> at the start, which the model has only seen as a document separator. Wrong special tokens are easy to spot once you look at IDs: Llama 3’s end-of-turn marker <|eot_id|> is not in Qwen3’s vocabulary, so in a Qwen prompt it becomes a handful of ordinary text tokens, and the model has no reason to treat it as the end of anything.

FOUR TEMPLATE MISTAKES AS THE MODEL SEES THEMno role markersWhat is the capital of France?model may continue the textno generation prompt…?<|im_end|>\nwho speaks next?another family's marker…?<|eot_id|><|eot_id|> is just text hereendoftext as BOS<|endoftext|><|im_start|>system…a document break at the start
Figure 4.68Four template mistakes, shown as the token strips they produce. Each is a valid sequence the model will happily read; none is one it was trained on.