Sub topic 2
The three core roles at a glance
This chapter gives you a working map of the three roles that appear in almost every chat request: system, user, and assistant. It assumes you can read basic Python, know what lists, dictionaries, and JSON are, and have a general idea of what a large language model is. Later chapters cover each role in depth. Here you learn what each role is for, who writes it, and how the roles fit together in one request.
Roles in one sentence each
Every message in a chat request is a dictionary with a role and content. The role tells the model who is speaking and how to treat the text.
systemcarries the instructions from the developer who built the application. It sets behavior, tone, format, and limits.usercarries what the person (or program) using the application wants. It holds questions, tasks, and input data.assistantcarries what the model said. You send earlier assistant messages back so the model can see its own previous replies.
Here is the same idea as a table.
| Role | Written by | Typical content | Appears |
|---|---|---|---|
| `system` | The developer | Rules, persona, output format | Usually once, at the start |
| `user` | The end user, or your code on their behalf | Questions, tasks, documents to process | Every turn |
| `assistant` | The model (or you, when you write examples or replay history) | Answers, earlier replies | After each user turn |
A request is a list in order
A chat request does not send one prompt string. It sends a list of messages, and the order of the list is the order of the conversation.
messages = [
{"role": "system", "content": "You are a concise assistant that answers in plain English."},
{"role": "user", "content": "What is a REST API?"},
{"role": "assistant", "content": "A REST API lets programs exchange data over HTTP using URLs and standard methods such as GET and POST."},
{"role": "user", "content": "Give me one example URL."},
]The model reads this list from top to bottom and writes the next assistant message. The last message here is a user message, which is the usual shape for a request: the model is being asked to respond to it. The second user message, "Give me one example URL.", only makes sense because the earlier turns are in the list.
The loop at the bottom of the diagram is the key point. The assistant message is not stored by the model. Your code takes it from the response and adds it to the list, so the next request includes it.
The system role
The system message is where the application author speaks to the model. It is usually the first message and usually the same across every conversation your app has.
Typical contents:
- Who the model should act as: "You are a support agent for a bike shop."
- Constraints: "Answer in under three sentences."
- Output format: "Reply with one word: positive, negative, or neutral."
- Boundaries: "If the question is not about bikes, say you can only help with bikes."
A good system message describes standing rules, not one particular question. If an instruction should apply to every turn, it belongs here. If it applies only to this request, put it in the user message.
The system message is not magic. It is text, and the model follows it because it was trained to give it extra weight, not because anything enforces it. A long conversation or a persuasive user message can still pull the model away from it. Treat the system message as strong guidance, not as a security boundary.
The user role
The user message is the input for the current turn. Most of the time it is a person's typed question, but "user" means "the party the assistant is serving", not "a human typing". Your code may also place text in user messages that no person typed: a document to summarize, a log file to explain, or a web page your program fetched.
That last case matters. Anything inside a user message is data supplied at request time, and you should not assume it is trustworthy just because it arrived in the right role. A later chapter covers this in detail. For now, remember that the role says where the text sits in the conversation, not that the text is safe.
The assistant role
An assistant message is text the model produced. You meet it in two ways.
- In a response. The API returns a new assistant message. You read its content and show it to the user.
- In a request. You include earlier assistant messages in the list so the model can see what it already said.
The second case also lets you write assistant messages yourself. When you put a hand-written assistant message in the list, the model treats it as something it said earlier. That is how few-shot examples work: you show the model pairs of user input and assistant output, and it continues the pattern.
messages = [
{"role": "system", "content": "Classify the sentiment of the text as positive, negative, or neutral. Reply with one word."},
{"role": "user", "content": "The delivery was fast and the box was intact."},
{"role": "assistant", "content": "positive"},
{"role": "user", "content": "The app crashes every time I open it."},
{"role": "assistant", "content": "negative"},
{"role": "user", "content": "The package arrived on Tuesday."},
]The two assistant messages here were never produced by the model. You wrote them. The model still treats them as its own earlier behavior and answers the final message in the same style.
Inspecting a message list
When a conversation behaves oddly, the first debugging step is to look at the exact list you are sending. This small helper prints each message with its position and counts the roles.
from collections import Counter
def describe(messages):
for i, m in enumerate(messages):
preview = m["content"][:50].replace("\n", " ")
print(f"{i}: {m['role']:<10} {preview}")
return Counter(m["role"] for m in messages)
messages = [
{"role": "system", "content": "You are a concise assistant that answers in plain English."},
{"role": "user", "content": "What is a REST API?"},
{"role": "assistant", "content": "A REST API lets programs exchange data over HTTP."},
{"role": "user", "content": "Give me one example URL."},
]
counts = describe(messages)
print(counts)Running it prints four numbered lines, then Counter({'user': 2, 'system': 1, 'assistant': 1}). In real projects, this kind of printout often reveals the bug: a missing assistant message, a system message that was appended twice, or an old conversation that was never cleared.
Checking roles before you send
Providers reject or mishandle messages with unknown roles. A small check catches typos such as "User" or "bot" before the network call.
ALLOWED_ROLES = {"system", "user", "assistant"}
def check_roles(messages):
for i, m in enumerate(messages):
if m.get("role") not in ALLOWED_ROLES:
raise ValueError(f"Message {i} has unknown role: {m.get('role')!r}")
if not isinstance(m.get("content"), str):
raise ValueError(f"Message {i} content must be a string in this simple form")
return True
print(check_roles([
{"role": "system", "content": "Be brief."},
{"role": "user", "content": "Hello"},
]))The string-only check on content is a simplification. Many APIs also accept content as a list of parts (for example text plus an image). This tutorial uses plain strings.
Common mistakes
- Putting per-turn data in the system message. If you rebuild the system message with user-specific text on every call, you lose the clean split between standing rules and current input. Put the changing part in a user message.
- Forgetting to append the reply. If you do not add the assistant response to your list, the next request has no memory of it. The model will seem to have forgotten the conversation.
- Leaving the last message as an assistant message by accident. Most chat models expect the final message to be a
usermessage. Some providers treat a trailing assistant message as text to continue, and others return an error. Check your provider's rules. - Using role names from one provider on another. The three core names are shared widely, but where the system prompt goes is not (see below).
- Assuming the system message cannot be overridden. It can be diluted or contradicted by later text. Keep critical rules short and clear, and enforce important limits in your own code too.
Where providers differ
The role names user and assistant are close to universal. The system role is where providers part ways.
| Provider style | Where the system prompt goes | What `messages` may contain |
|---|---|---|
| OpenAI-style Chat Completions | A message with `role: "system"` at the start of the list | `system`, `user`, `assistant` (plus others such as `tool`) |
| Anthropic Messages API | A separate top-level `system` parameter | Only `user` and `assistant` turns |
Here is the Anthropic form, with the system prompt outside the list.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="MODEL_NAME", # replace with a model available to you
max_tokens=300,
system="You are a helpful assistant. Keep answers under three sentences.",
messages=[
{"role": "user", "content": "What is a list in Python?"},
],
)
print(response.content[0].text)The idea is the same in both styles: standing instructions are separate from the conversation turns. Only the placement differs. If you write code that supports both, keep your own internal list with a system message first, and convert it to each provider's shape at the edge.
More roles exist
The three roles cover most chat use. Some APIs add others. A tool role carries the result of a function call your code ran for the model. Newer OpenAI models also use a developer role for instructions that play the part the system role played earlier. Names and rules vary by provider and model version, so check the current documentation. A later chapter covers these.
Exercise
A developer's app sends a request whose message list is: one system message, one user message, one assistant message, one user message. (a) Which message does the model respond to? (b) Who wrote the assistant message, and how did it get into the list? (c) The developer wants every reply to be under 50 words. Which role should carry that rule, and why?
Show answer
(a) The model responds to the final user message, but it reads the whole list, so earlier turns give it context. (b) The model wrote it in an earlier response, and the developer's code appended it to the history before the next request. (A developer can also write assistant messages by hand, as in few-shot examples.) (c) The system role, because it is a standing rule for every turn, not a part of one question.
Exercise
You switch an app from an OpenAI-style API to the Anthropic Messages API and keep your list with the system message first. The request fails. What is the most likely cause, and what is the fix?
Show answer
The Anthropic API does not accept a system role inside the messages list. Move the system message's text into the top-level system parameter and leave only user and assistant turns in messages.