सामग्री पर जाएँ

लेसन 10 · Prompting and Chat Formats

Llama Chat Templates and Special Tokens

See how role messages become one token stream in Llama 3 and Llama 4, what end-of-turn tokens do and why you should use the official chat template.

You write a list of messages with roles. The model reads one long stream of tokens. A chat template converts the first into the second by adding special tokens. If the format is wrong, the model may ramble, repeat itself, or write the user's side of the chat. This lesson shows the formats, which differ between generations.

Roles

  • system: standing instructions for the whole conversation.
  • user: what the person (or your program) asks.
  • assistant: what the model replied.
  • tool output: results from your functions. Meta's Llama 4 documentation calls this role ipython. Some libraries and APIs call it tool and convert it for you.

The Llama 3 style

Meta's Llama 3.x prompt format wraps each message with a role header and ends it with an end-of-turn token:

TEXT
<|begin_of_text|><|start_header_id|>system<|end_header_id|>

You are a brief assistant.<|eot_id|><|start_header_id|>user<|end_header_id|>

What is a hash map?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

The text stops right after the assistant header. That tells the model it is now the assistant's turn. The model writes its reply and ends with <|eot_id|>. Blank lines matter, and official templates may add details such as a date line.

Another token, <|eom_id|> (end of message), appears in some tool-calling formats. It means "I am waiting for a tool result", while <|eot_id|> means "my turn is finished".

The Llama 4 style

Llama 4 uses different markers:

TEXT
<|begin_of_text|><|header_start|>system<|header_end|>

You are a brief assistant.<|eot|><|header_start|>user<|header_end|>

What is a hash map?<|eot|><|header_start|>assistant<|header_end|>

It also has image-related tokens that the inference software fills in. A template for one generation does not work for another.

Build a template by hand (to learn it)

Python Stand-in: render messages in the Llama 3 style

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Without the generation prompt, the text ends after the user's turn, and the model may not behave as if it should answer.

Use the real template in real code

Do not copy strings into your application. The tokenizer files ship with the template:

Python
from transformers import AutoTokenizer

tok = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B-Instruct")
text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
print(text)

Chat APIs such as Ollama's /api/chat apply a template for you. Send messages, not hand-built strings. Hand-building is mainly for raw completion endpoints and for debugging.

Printing the rendered text once is a great debugging step. It can reveal a missing system message, a doubled begin token or the wrong format. A double <|begin_of_text|> can happen when you render text with the template and then let the tokenizer add special tokens again.

Stop tokens

The runtime must stop at the end-of-turn token. If it is not configured, the model may produce a fake next user message and answer it. When you see that, check the template and stop settings first.

Injection through special tokens

User text could contain strings that look like markers, such as <|eot_id|>. Many tokenizers and servers, depending on their settings (and often by default), turn them into real special tokens, which lets a user fake a new system message. Check how your stack tokenizes user input, and strip or escape marker-like strings in untrusted text.

Python Neutralize marker-like text in user input

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is one layer only. It does not stop prompt injection written in plain words. See prompt injection attacks, and read more about templates in vocabulary, special tokens and chat templates.

A debugging routine

When answers look strange, follow the same steps every time.

  1. Render the final text that the model receives and read it slowly.
  2. Check that it starts with exactly one begin token.
  3. Check that every turn has a role header and an end-of-turn token.
  4. Check that the text ends with an open assistant header when you expect a reply.
  5. Check that the runtime treats the end-of-turn token as a stop.
  6. Confirm that the template belongs to the same model generation as the weights.

Often the problem is visible at step one. A missing system message can mean your application dropped it while trimming history.

Templates and fine-tuning

A model learns the format it is trained on. If you fine-tune with a hand-made format and serve with the official one, or the other way around, quality can drop sharply even though nothing crashes. Use one template in data preparation, training, testing and serving, and store its version together with the model.

Templates and tools

Tool definitions are also written into the prompt by the template or the server. That is why tool support depends on both the model and the software in front of it. If a server does not know how to write your tools for your model, the model never sees them in the form it expects. Check the rendered prompt when tool calls fail, and read the tool calling lesson.

Special tokens are single units

A special token is one ID in the vocabulary, not a row of ordinary characters. The tokenizer decides whether the text <|eot_id|> becomes that single ID or a sequence of normal pieces. Libraries have options that control this, and a server may behave differently from a notebook. Test the behavior on your own stack instead of assuming it.

Summing up the two views

Keep two views in mind. Your code sees a clean list of messages, which is easy to build, store and test. The model sees one string of tokens with markers. The template is the bridge, and most format bugs live on it.

Keep a sample. Save one correctly rendered prompt as a test, so a library upgrade that changes the format is noticed.

Common mistakes

Do not reuse a Llama 3 template for Llama 4, or the reverse. Also do not fine-tune with one format and serve with another.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does the final assistant header in a rendered prompt do?

    It marks that the next tokens belong to the assistant.
  2. 2. What is the safest way to format prompts for a Transformers model?

    The shipped template matches the model's expected format.
  3. 3. A bot keeps inventing user messages and answering them. What should you check?

    A missing end-of-turn stop lets generation run into a new turn.
  4. 4. In Meta's Llama 3.x format, what does <|eom_id|> indicate?

    It ends a message while the turn continues after the tool output.

Summary

  • Chat templates turn role messages into one marked token stream.
  • Llama 3.x and Llama 4 use different markers.
  • Use the official template or a chat API, and configure stop tokens.
  • Treat marker-like text from users as untrusted.

Next: Prompting Llama Well

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।