AI Engineering Tutorial · LLM Basics · Lesson 3 of 25
Messages, Roles and System Prompts
How a chat request is structured: the system prompt, user and assistant messages, and how conversation history works.
The message list
A chat request contains:
- A system prompt: instructions and context that apply to the whole conversation (role, rules, tone).
- A list of messages, alternating
userandassistant, ending with the user's latest message.
The model reads all of it and writes the next assistant message. To continue a conversation, append the
reply and the next user message, and send the whole list again.
class StubModel:
# Stands in for an LLM API so the example runs in the browser without a key.
def __init__(self, replies):
self.replies = list(replies)
self.calls = []
def create(self, system, messages, max_tokens=200):
self.calls.append({"system": system, "messages": messages, "max_tokens": max_tokens})
return self.replies.pop(0)
model = StubModel(["Order 10042 shipped yesterday.", "Yes, until it leaves the depot. Use Orders > Change address."])
system = "You are the support assistant for Booknook. Be brief. Never promise refunds."
messages = [{"role": "user", "content": "Where is order 10042?"}]
reply = model.create(system, messages)
messages.append({"role": "assistant", "content": reply})
messages.append({"role": "user", "content": "Can I still change the address?"})
reply = model.create(system, messages)
for m in messages + [{"role": "assistant", "content": reply}]:
print(f"{m['role']:9}: {m['content']}")
print("messages sent on the second call:", len(model.calls[1]["messages"]))
Output
user : Where is order 10042? assistant: Order 10042 shipped yesterday. user : Can I still change the address? assistant: Yes, until it leaves the depot. Use Orders > Change address. messages sent on the second call: 3
Good system prompts
- Say who the assistant is and who it serves.
- State rules that always apply ("answer only about our products", "never promise refunds").
- Describe the output format if it matters.
Keep facts that change per request (the user's order, retrieved documents) in the messages, not baked into the system prompt. See System prompts.
Test yourself
Where should a rule like 'never promise refunds' go?
In the system promptIn the first assistant messageNowhere; models know itIn the max_tokens setting
Show answer
Answer: In the system prompt
The system prompt holds instructions that apply to the whole conversation.