Concepts / Instruction Hierarchy in Foundation Models

Instruction Hierarchy in Foundation Models

A prompt template is a fixed layout that merges a system prompt and a user prompt into the single input the model reads.

  • Programming

Why Formatting Can Silently Change Output

When people first build on a language model, they often assume that the message format is a detail the model will sort out on its own. The reasoning goes like this: you have some instructions, you have a question, and as long as both reach the model, the model will work out which part is which. Under that belief, the exact spacing, line breaks, and special markers around the text look like cosmetic choices.

The source material shows why that belief is risky. Template mismatches often fail silently, and even a small formatting difference, such as an extra new line, can change the model's behavior. Nothing crashes and no error message appears. The model simply receives a slightly different sequence of characters and responds differently. This article follows one request from the moment its pieces are written down to the moment the model reads them, so you can see exactly where that sequence is built and where it can go wrong.

Tracing One Request Into a Single Input

A foundation model that accepts a system prompt and a user prompt does not read them as two separate objects. The source describes the model combining the two prompts into a single prompt, typically by following a template. This step happens before the model does any work, which is why it is easy to overlook. It determines the exact sequence of characters the model actually reads.

A prompt template is a fixed layout with empty slots. The slots are filled with the content of a particular request, and the result is one complete piece of text. The template decides where each piece goes, which markers separate them, and what text surrounds them. The model then receives the combined result as its single input. The diagram below traces that path from the two prompts to the one input.

System promptChat templatefixed layout with markersCombined promptone complete piece of textModel inputsingle sequence the modelreadsUser promptfills system slotfills user slotlays pieces out in ordersent as one input
How do separate system and user text pieces flow together and become one single sequence the model reads, and in what order do they appear?
Worked example

Merging a System Prompt and a User Prompt

An application has the system prompt 'Answer in one short sentence' and the user prompt 'What does a template do?'. Show how these become one model input using the Llama 2 layout described in the source.

  1. 1
    Identify the two pieces

    The system piece is 'Answer in one short sentence'. The user piece is 'What does a template do?'. Neither piece contains any markers yet.

  2. 2
    Start the layout

    The Llama 2 template begins with a begin marker. Nothing from either prompt appears before this marker.

  3. 3
    Open the instruction block and the system block

    The template then opens an instruction block and, inside it, a system block. These are fixed markers, so they are copied exactly rather than filled.

  4. 4
    Insert the system prompt

    The system text 'Answer in one short sentence' goes into the system slot, between the system block markers.

  5. 5
    Close the system block and insert the user message

    The system block is closed, and the user text 'What does a template do?' is placed after it.

  6. 6
    Close the instruction block

    The final marker closes the instruction block. The completed string is what the model receives as its single input.

Result

One complete string in which the system text appears first, followed by the user text, with every marker from the template in its fixed position. The model reads this whole string, not two separate prompts.

Chat Template Versus Prompt Template

The word template is used for two related but distinct things, and confusing them is a common source of errors. The first is the chat template that a model's developers define for their model. It describes how that model expects its input to be structured, and it can usually be found in the model's documentation. The second is the prompt template an application developer writes to fill in their own prompts with specific data, such as a product name or a document. Any application developer can define the second kind. Only the model's developers define the first.

AspectChat templatePrompt template
Who defines itThe model's developersThe application developer
PurposeDescribes how the model expects its input to be structuredFills in the developer's own prompts with specific data
Where it is foundUsually in the model's documentationWritten inside the application itself
Example slotsRole markers, system text, user text, end-of-turn markersA product name, a document, or other request data
Can it changeMay change between versions of the modelChanges whenever the application's author changes it

The two kinds of template share the word but belong to different layers of the system.

In the real world

Consider a support application. The developer writes a prompt template with a slot for the customer's question and a slot for an order number. Those are the application's own slots. The model's chat template, which the model's developers defined, wraps the filled-in text in the markers that model expects. The application controls the content of the slots. The model's developers control the fixed markers around them.

Reading a Template's Fixed Layout

The source uses Llama 2's chat model as its first example. Its template places the system prompt inside special bracket markers and then places the user message after them, followed by a closing marker. Read from left to right, the template starts with a begin marker, opens an instruction block, opens a system block, inserts the system prompt, closes the system block, inserts the user message, and closes the instruction block. The diagram below labels each part in that order.

Begin markerstarts the sequenceInstruction openopens instruction blockSystem openopens system blockSystem promptfilled from the applicationSystem closecloses system blockUser messagefilled from the userInstruction closecloses instruction blockthenthenthenthenthenthen
What exact parts does the final merged string contain, and where does each piece sit relative to the others?

Two of these parts are fixed and two are slots. The markers are the same for every request, and the system prompt and user message are the only parts that change. This is what makes a template a fixed layout with empty slots: the layout never moves, and only the contents of the slots differ between requests.

Templates are not permanent. For the Llama 3 chat model, the provider changed the layout. The same underlying idea, a system part followed by a user part, now looks quite different. The new layout uses header markers that name each role, end-of-turn markers that close each part, and a final header that prompts the model to begin its reply. A string that was correct for one version may be wrong for the other, even though the content is identical.

The markers themselves are not ordinary words. The source explains that each span between a less-than-bar delimiter and a bar-greater-than delimiter is treated as a single token by the model. A marker such as the start-header marker is therefore handled as one unit, not as a sequence of letters that might be rewritten or split by accident. Changing even a small part of one of these markers produces a different input from the one the model was trained to expect.

Why the System Prompt Seems Special

Model providers often say that well-crafted system prompts improve results. Anthropic's documentation, for example, says that assigning a role or personality through a system prompt helps the model stay in character more effectively across a conversation. That sounds as though system prompts must be handled in a special way. The source explains that this is not the mechanism. Under the hood, the system prompt and the user prompt are concatenated into one final prompt before the model sees them. From the model's side, both are processed the same way.

What do you think happens?

Before reading on: when the model receives a system prompt and a user prompt, does it process the system text differently from the user text?

  • Yes, the system text is processed in a separate, privileged way

  • No, both are processed the same way once they are combined into one prompt

Reveal answer
Answer:

No, both are processed the same way once they are combined into one prompt

The system prompt and user prompt are concatenated into one final prompt, and from the model's side both are processed the same way. The system prompt is not handled through a separate channel.

If the system prompt is processed no differently, the apparent boost must come from something else, and the source offers two likely explanations. The first is position: the system prompt sits at the front of the final prompt, and a model may simply handle instructions that appear first more reliably. The second is training: a model may have been post-trained to pay closer attention to system prompts. The source points to work on an instruction hierarchy, which trains models to prioritize privileged instructions, and notes that this training also helps defend against prompt attacks.

Where a Formatting Failure Hides

When a model's output seems wrong, it is tempting to blame the model or the wording of the question. The source's guidance points somewhere more specific. Because the failure is silent, the most useful place to look is the step where the prompt is assembled, and the exact text that results from it. The pipeline below shows the order of steps between the application's code and the model. The highlighted step is the one whose output you should inspect first.

Application codeholds the request dataPrompt template fillslots filled with requestdataChat template appliedmarkers and layout addedFinal stringexact characters the modelreadsModel reads inputprocesses the stringpasses datawrapped in layoutproducessent as input
At which step between the developer's code and the model does the prompt get altered or malformed, so I know where to inspect first?

The final string is the most reliable place to inspect, because it is the only version that matches what the model actually reads. Earlier stages can look correct while the final string is still malformed, for example when a layout was built against the wrong version of a template. Comparing the printed final text with the template documentation is a simple habit that catches many of these problems.

Mistakes That Hide in the Final String

The consequences of a mismatch are easy to underestimate. The source reports that using the wrong template can produce puzzling performance problems. The mistakes below are the ones most worth watching for.

  • Common mistake

    Assuming the model will work out which text is instructions and which is the question

    What it looks like

    Joining the system text and the user text with a plain space and sending the result

    Why it fails

    The template decides where each piece goes and which markers separate them. Without the markers, the model receives a different sequence than the one it was trained to expect.

    Do this instead

    Build the final string from the model's chat template so every marker is in its expected position.

  • Common mistake

    Treating an extra new line as harmless

    What it looks like

    A layout where one additional line break sits between two markers

    Why it fails

    The source notes that even a minor slip, such as an extra new line, can cause the model to change its behavior significantly.

    Do this instead

    Compare whitespace exactly against the template documentation, including line breaks around every marker.

  • Common mistake

    Using a layout from one version of a model with another version

    What it looks like

    Using the Llama 2 layout after switching to the Llama 3 chat model

    Why it fails

    Templates may change between versions, so a string that was correct before can be wrong now.

    Do this instead

    Confirm the template version in the model's documentation for the exact model you are calling.

  • Common mistake

    Confusing the application's prompt template with the model's chat template

    What it looks like

    Editing the product-name slot and assuming that also changes the markers around it

    Why it fails

    The application controls the slot contents. The model's developers define the fixed markers that surround them.

    Do this instead

    Keep the two layers separate in your head and check each one against its own source.

  • Common mistake

    Editing a marker by hand, for example retyping it from memory, which can change its characters

Checking the Final Prompt Before Sending

Because the failure is silent, the practical defense is to inspect the exact text that will reach the model. The checks below follow the order in which the source's habits can be applied. Run them on the assembled final string, not on the pieces you started with.

  1. 1
    Print the final string that will be sent, not the individual system and user texts.
  2. 2
    Confirm which template belongs to the exact model version you are calling.
  3. 3
    Check that the begin marker, the instruction and system blocks, and the closing markers appear in the order the template defines.
  4. 4
    Confirm that the system text sits where the template places it, and that the user text sits after it.
  5. 5
    Check the whitespace around every marker, including any extra new lines.
  6. 6
    Compare the printed text against the template documentation, and only send the request when they match.
MEDIUM

An application builds a final string for a model and the output looks wrong. You print the final string and find an extra line break between the system text and the first marker that follows it. Using the checklist above, explain which check would have caught this, and why the earlier stages of the pipeline could look correct even though the final string is malformed.

Hints
  • Ask which check compares the final string against the template documentation.
  • Think about whether the system text and user text looked correct when you inspected them alone.

Key Takeaways

  • A prompt template is a fixed layout with empty slots; it merges the system prompt and user prompt into one string, which is the single input the model reads.
  • A model's chat template is defined by the model's developers and may change between versions, while a prompt template is written by application developers to fill in request data.
  • The model processes system and user text the same way; any apparent benefit from system prompts likely comes from their position at the start and from post-training.
  • Template mismatches fail silently, and even a small slip such as an extra new line can change behavior, so the most likely point of failure is the final assembled string.
  • Before sending, print the exact final string and check its order, markers, and whitespace against the template documentation for the specific model version.

Related concepts

More concepts starting with I