Sub topic 2
The Three Core Roles at a Glance
This chapter assumes you can read basic Python, know lists and dictionaries, have a general idea of what a large language model is, and have seen an HTTP API or SDK call. It gives you the working model of the three core roles. Later chapters cover each role in depth.
What a role is
A chat message is a dictionary with two fields: role and content. The content is the text. The role tells the model who is speaking and how to treat that text. A conversation is a list of these messages in the order the turns happened.
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."},
]This list holds all three core roles. The last message is a user message, so the model's next output answers it.
The roles side by side
| Role | Who writes it | Main purpose | Typical lifetime |
|---|---|---|---|
| `system` | You, the developer | Set behavior, tone, and constraints | Usually one message at the start, sent on every request |
| `user` | The end user, or your code on their behalf | Ask questions and supply input | One per turn |
| `assistant` | The model | Hold the model's replies | One per turn, added after each response |
System
The system message describes how the assistant should behave. Typical content includes a persona, an output format, a length limit, or topics to avoid. It is the place for instructions that should apply to the whole conversation, not just one question.
User
User messages carry what is being asked. That includes questions, documents to summarize, and data to classify. Your code often builds these messages, for example by inserting a customer's text into a template.
Assistant
Assistant messages hold what the model said. After each API call you take the reply and append it to the list as an assistant message. You can also write assistant messages yourself. This is how few-shot examples work, and a later chapter covers it.
How the roles fit together
The system and user messages travel to the model together in a single request. The assistant message comes back in the response. Your code then decides whether to store it for the next turn.
Seeing the roles in a real call
The following example uses the OpenAI Python SDK. The system message sets a length limit, and the user message asks the question.
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="MODEL_NAME", # replace with a chat model available to you
messages=[
{"role": "system", "content": "You are a helpful assistant. Keep answers under three sentences."},
{"role": "user", "content": "What is a list in Python?"},
],
)
print(response.choices[0].message.role) # assistant
print(response.choices[0].message.content) # the reply textThe response message has the role assistant. Its content is the text you would append to your history.
Common errors
- Putting everything in one user message. Rules mixed into the user's text are harder to maintain and easier for the user to override. Put standing rules in the system message.
- Forgetting to append the reply. If you do not add the assistant message to your list, the next request has no record of what the model said.
- Mistyping the role. Role names are exact strings such as
"assistant". A typo like"bot"or"ai"is usually rejected with an error. - Treating the system message as secret or unbreakable. It is ordinary text in the request. Do not store secrets in it, and do not assume it cannot be overridden by clever input. A later chapter covers instruction priority.
Exercise
A developer's chatbot answers the first question well. On the second question, "How do I add an item to it?", the model asks what "it" means. The code sends only the new user message each time. What is missing, and which role should the missing messages have?
Show answer
The earlier turns are missing. The request must include the previous user message and the model's previous reply, appended in order. The earlier questions keep the role user, and the model's earlier replies must be stored with the role assistant.
Exercise
Which role should hold the instruction "Always answer in two sentences or fewer," and why?
Show answer
The system role. It is a standing rule for the whole conversation, so it belongs with the developer's instructions and not inside one user turn.