Messages, Roles and the System Prompt

A request to a language model is not one question but a short conversation written down as a list of messages, and each message carries a role that tells the model who is speaking. Knowing what the roles mean lets you give a model a job with rules, keep a conversation going across calls, and understand why the same request can come back with different words each time.

A request is a list of messages with roles

In a chat completion request, the messages parameter is the conversation so far: a Python list of dictionaries, each with a role and a content. The model reads the whole list from top to bottom and writes the next message. The role is how it knows what kind of text each entry is, because a rule written by the application and a question typed by a stranger must not be treated the same way.

messages = [
    {"role": "system", "content": "You answer questions for a bike shop. Reply in at most three sentences."},
    {"role": "user", "content": "Do you repair electric bikes?"},
]

Three roles carry most conversations:

RoleWho writes itWhat it tells the model
systemThe developer of the applicationInstructions to follow, whatever the user later says
userThe person using the applicationA request or extra information to apply those instructions to
assistantThe model, in an earlier turnWhat it already said in this conversation

The assistant role is not only what comes back in a response. Your code also sends earlier replies into a request as assistant messages, and the model takes them as things it said before. That is how a conversation of several turns is written down, as a later section shows.

Two more roles exist in the same format. Some providers offer a developer role that does the job of system and is preferred on their newer models. They still accept system, and system is the role that works across the widest range of compatible endpoints. A tool role carries the result of a function your code ran on the model's request; it belongs to tool calling and is not needed for plain text replies.

True or false?

Only the model ever produces messages with the assistant role.

False. The model writes them first, but the application sends them back in later requests as assistant messages so the model can see what it said before.

The model reads the role of each message, not just its text, to decide how to treat it.

True. The same sentence means something different as a system instruction than as a user's request: instructions from the system role take precedence over the user role.

The system prompt holds the standing instructions

An application usually does one job: answer customer questions, summarise support tickets, translate product descriptions. The text that sets up that job is the system prompt, the content of the system message. It is written once by the developer and sent at the top of every request, while the user message changes with every person and every question.

  • System prompt same on every call
  • Customer message new on every call
  • Program builds the messages
  • Language model writes the reply
  • System prompt to Program: system
  • Customer message to Program: user
  • Program to Language model: messages

Keeping the two apart matters for a practical reason. If your code glued its instructions and the customer's text into one string, the model could not tell which sentences are the rules and which are the input. With separate roles it can: instructions given in the system role take precedence over instructions in the user role. A useful way to see it is a function and its arguments. The system prompt is the function definition, the rules and logic of the application; each user message is an argument that those rules are applied to.

Precedence is not enforcement, though. The model gives the system prompt priority because it was trained to, not because anything stops it from doing otherwise. A user who writes "ignore your instructions" usually fails, but not always, so a system prompt is a strong instruction and not a security boundary. Anything that must never happen is checked by your own code.

Providers also differ in small ways. Some take every system message in the list, wherever it sits, and join them into one block of instructions placed ahead of the conversation. Writing a single system message first works the same everywhere.

True or false?

A rule in the system prompt guarantees the model will never break it.

False. The model gives the rule priority over user messages, but it is still an instruction it follows, not code that enforces it. Rules that must hold are checked in the program.

The system prompt usually stays the same from one request to the next, while the user message changes.

True. It describes the job of the application, which does not change per question; the user message is the new input each time.

A good system prompt reads like a specification

"You are a helpful assistant" tells the model almost nothing about the job. A system prompt that gives reliable answers reads more like a short specification for a colleague. It says three things: the task (what to do with each user message), the rules (what is allowed, what is not, and what to do when the answer is unknown) and the form of the answer (how long, in what language, in what layout). Facts the model needs and cannot know, like opening hours, go in too.

support-prompt.md
You are the reply assistant of a small bike shop.

Task: answer one customer question about repairs, opening hours or orders.

Rules:
- Use only the facts below. If the answer is not in them, say that a member of staff will reply.
- Never promise a price or a delivery date.
- Reply in the language the customer wrote in.

Facts:
- Open Tuesday to Saturday, 9:00 to 18:00.
- Repairs take two to five working days.

Answer form: plain text, at most three short sentences, no greeting line.

Each line of a prompt like this changes what the application does, so it deserves the same care as code. Keep it in its own text file, read it when the program starts, and put the file under version control next to the code that uses it:

from pathlib import Path

system_prompt = Path("support-prompt.md").read_text(encoding="utf-8")

messages = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": customer_text},
]

A change to the prompt then shows up as a diff, can be reviewed before it goes out, and can be undone. Changing it the way you change code also means trying it on a few representative questions before and after, because a sentence that fixes one answer can change others. OpenAI's own guidance on text generation now recommends storing production prompts in application code for exactly these reasons: code review, tests and the normal deployment process.

The model remembers nothing between calls

Each request to the model is independent. The model sees the messages in the current request and nothing else: not the previous call, not the user's earlier questions, not its own earlier replies. If a person asks "How long does that take?" in a second call, the model has no idea what "that" is unless the earlier exchange is in the list.

So a conversation continues only when the program sends its history again. After each reply, the program appends the model's message to its list as an assistant message, appends the next user message, and sends the whole list.

  • Program
  • Language model
  • Program to Language model: system, user: repair e-bikes?
  • Language model to Program: reply: yes, we do
  • Program to Language model: system, user, assistant, user: how long?
  • Language model to Program: reply: two to five days
reply = response.choices[0].message.content

messages.append({"role": "assistant", "content": reply})
messages.append({"role": "user", "content": "How long does that take?"})

response = client.chat.completions.create(model=model, messages=messages)

This has two consequences. First, the program, not the model, owns the conversation: it decides what the model sees, so it can also drop or shorten old turns. Second, every earlier message is sent and billed again as input on every call, and the whole list has to fit within the limit on how much one request can hold. A long conversation gets more expensive with each turn.

Some providers offer APIs that keep the conversation on their servers and add the history for you. The model is just as stateless there; the provider stores the history and still counts all of it as input tokens.

True or false?

If you send only the new question, the model still answers in the context of the earlier exchange.

False. The model sees only the messages of the current request. Without the history in the list, the earlier exchange does not exist for it.

In a conversation of ten turns, the first user message is sent ten times.

True. Each call sends the full history again, so every earlier message is part of every later request and is counted as input each time.

Why the same request returns different text

A model writes its reply one token at a time, a token being a small piece of text such as a word or part of a word. At each step it looks at everything so far, the messages and the reply written up to that point, and gives every possible next token a probability. One token is chosen, added to the reply, and the step repeats until the reply is complete.

  • Read the text so far
  • Give each next token a probability
  • Pick one, with some randomness
  • Add it to the reply

The choice is usually made by sampling: drawing a token at random, weighted by the probabilities. The likeliest token wins most often, but any token with a probability above zero can be drawn. One different token early in a reply changes everything the model writes after it, because each later step reads it. That is why two identical requests can return replies that differ in wording, length or even in what they say. Model output is non-deterministic; code that uses it should not expect the exact same text twice.

A setting called temperature adjusts how strongly the draw favours the likeliest tokens. Suppose the reply so far is "Your bike will be" and the model gives four candidate next tokens the probabilities shown by the bars for temperature 1. The bars for the other two temperatures show the same four tokens after the setting has reshaped their chances.

Chance of each next token, in percent
  • ready
  • repaired
  • done
  • back
  • temperature 0.5
  • temperature 1
  • temperature 1.5

Temperature works before the draw. Behind each probability is a raw score the model computes for that token. Each score is divided by the temperature \( T \), and the results are then turned back into probabilities that add up to one:

$$p_i = \frac{e^{z_i / T}}{\sum_j e^{z_j / T}}$$

probability of token i at temperature T z is the model's raw score for each token

A temperature below 1 sharpens the distribution: the likely token becomes likelier and rare tokens almost disappear, so replies become more focused and more alike. As the temperature approaches 0, sampling approaches always taking the likeliest token. A temperature above 1 flattens it, so unusual tokens are drawn more often and replies become more varied, and eventually less coherent.

On the OpenAI Chat Completions API, temperature ranges from 0 to 2 with a default of 1; other providers use other ranges, such as 0 to 1. A low temperature narrows the choice but is not a promise of identical text. Some providers also offer a seed parameter for repeatable sampling, and they describe it as a best effort, not a guarantee.

Not every model accepts the setting. Reasoning models work through a problem in hidden steps before they write the visible reply, and on some of them the provider fixes how tokens are sampled: their documentation says to leave temperature out, and some return an error when it is set. Because which models accept it changes from provider to provider and release to release, a portable program sends no temperature unless the model's documentation says it is supported, and relies on the system prompt, not on the sampling setting, to make replies focused.

True or false?

Setting temperature to 0 guarantees the same reply to the same request on every provider.

False. Low temperature makes the choice of token close to always picking the likeliest one, but providers do not promise identical output, and some models do not accept the setting at all.

A higher temperature gives unlikely tokens a bigger share of the chances.

True. Dividing the scores by a larger T flattens the distribution, so tokens with low probability are drawn more often.

Every model on a Chat Completions endpoint accepts a temperature value.

False. Some reasoning models do not: their providers say to omit it, and some reject a request that sets it with an error.