Lesson 01.04 · Phase 01 · LLM Foundations for Agents

Chat templates and message roles

The model never sees a list of messages. It sees one long string with special tokens, built by a template you didn't write and should read.

Learning objectives

  • Explain how a chat template turns system, user, assistant and tool messages into a single token sequence.
  • Render llama3.2's template by hand and verify it token-for-token against a real server.
  • Find where tools and tool results go in a template, and what that means for agents.
  • Use prefill: start the assistant's reply yourself to steer the format.

Phase 3 kept running into a strange pattern: llama3.2 made one tool call and then stopped using tools. The code was fine, and the prompt was fine. Lesson 11 found the cause in a place most developers never look, the chat template, which only shows the tools inside the last user message.

Loading the full lesson…