Instruction Format › Multi-Turn Conversation Format

Michael BrenndoerferDecember 18, 20256 min read
Language AI HandbookAll chapters
Part 36 of 67Instruction Tuning
Chapter 269Instruction Format › Multi-Turn Conversation Format
Reading section 5 of 14 · Multi-Turn Conversation Format · 6 minSection 5 of 14 · 6 min

Book contents

Section 5 of 14 · 6 min read

Instruction Format › Multi-Turn Conversation FormatOne section · Sections in this chapter5 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Real conversations involve multiple exchanges between the user and the assistant. A single question and answer might suffice for simple tasks, but complex interactions require context to build over time. A multi-turn format must track the full conversation history while clearly marking each turn's boundaries and roles. This creates a cumulative context where each response can draw on everything that came before. This makes possible the model to resolve pronouns, follow up on previous statements, and maintain a coherent narrative across many exchanges.

Understanding multi-turn format is necessary because it determines how the model encodes conversational memory. The model does not have explicit memory; it has context. All prior turns are re-encoded on every generation step, and the model's responses are conditioned on the entire visible history. The format's job is to make that history unambiguously parseable so the model can correctly identify which content came from which speaker at each turn.

A simple multi-turn structure looks like:

User: What is the capital of France? Assistant: The capital of France is Paris. User: What's the population? Assistant: Paris has a population of approximately 2.2 million in the city proper, or about 12 million in the greater metropolitan area.

The model processes the entire history when generating each new response. For the second question, it sees both the original question about France's capital, the assistant's response, and the follow-up question about population. This context allows it to resolve the ambiguous reference "What's the population?" to mean Paris's population. Without the conversation history, the model would have no way to know what "the population" refers to. Multi-turn formats let natural, contextual conversation instead of isolated question-answering.

Multi-turn training requires careful attention to what receives gradient updates. Typically, you only compute loss on the assistant turns, not user turns or the system message. Standard causal attention masking ensures only assistant tokens contribute to the loss, preventing the model from learning to predict user inputs. User turns give context that shapes the assistant's response, but they are not targets for prediction. This selective loss computation is what makes the model specialized for response generation rather than conversation modeling in the general sense.

The training loss objective for a multi-turn example can be written as follows. Let x=(x1,x2,…,xT)\mathbf{x} = (x_1, x_2, \ldots, x_T) denote the full token sequence including system, user, and assistant tokens, and let A⊂{1,…,T}\mathcal{A} \subset \{1, \ldots, T\} denote the set of positions corresponding to assistant response tokens. The instruction-tuning loss is:

L=−∑t∈Alog⁡P(xt∣x1,…,xt−1;θ)\mathcal{L} = -\sum_{t \in \mathcal{A}} \log P(x_t \mid x_1, \ldots, x_{t-1}; \theta)

where:

  • xtx_t is the token at position tt
  • x1,…,xt−1x_1, \ldots, x_{t-1} is the full causal context up to position tt
  • θ\theta are the model parameters
  • A\mathcal{A} is the set of assistant-turn token positions

The key insight is that A\mathcal{A} does not include all positions, only those belonging to assistant turns. Non-assistant positions are still present in the input and influence the attention computation through the conditioning context x1,…,xt−1x_1, \ldots, x_{t-1}, but they do not contribute to the gradient signal. This means the model learns to generate good assistant responses while freely using system and user context to condition those responses.

The conversation format must handle several edge cases that arise in real-world applications. What happens if a user message is empty? What if there are consecutive user messages without assistant responses? How should the model handle extremely long conversation histories that exceed context length? Different formats and training procedures handle these cases differently, and your choice depends on your application's requirements. Some applications might truncate long histories, while others might summarize earlier turns, and still others might refuse to continue conversations that exceed certain lengths. None of these strategies is universally correct; each involves tradeoffs between context richness and computational feasibility.

Out[5]:
Visualization
Stacked area chart showing cumulative token counts over ten conversation turns with a red dashed horizontal line showing the context limit.
Cumulative context growth over ten conversation turns illustrating token consumption. Total token count increases linearly as exchanges accumulate, with assistant responses typically consuming more context than user prompts. The red dashed line marks the context limit, showing how rapidly long conversations approach token boundaries.

Packing Multiple Examples Into a Single Sequence

An important efficiency technique for multi-turn training is sequence packing, where multiple independent conversations are concatenated into a single long sequence to maximize GPU utilization. Instead of padding short conversations to the maximum sequence length (wasting compute on padding tokens), you fit as many conversations as possible into each sequence up to the context limit.

Packing requires careful attention masking. Each packed conversation must attend only to its own tokens, not to tokens from other conversations in the same sequence. This is achieved through block-diagonal attention masks, where each conversation forms a diagonal block that sees only its own tokens. Without these masks, the model would "leak" context from one conversation into another, corrupting the training signal. Most modern training frameworks support packing with block-diagonal attention, and the speedup over naive padding can be substantial, often two to four times on datasets with variable-length conversations.