← AI Agents Foundations
2 / 25
Lesson 2 — Messages as a list of dicts
How a conversation is stored
Models do not remember anything between calls. Every time, your code sends the whole conversation as a list. Each item is a dict with two keys:
- role — who is speaking:
"system"(rules for the model),"user"(the person) or"assistant"(the model's own earlier replies). - content — the text.
messages = [
{"role": "system", "content": "You are a helpful tutor."},
{"role": "user", "content": "What is a variable?"},
]
When the model answers, you append its reply as an "assistant" message, so next time it can see what it already said.
Why it matters: OpenAI, Anthropic and Gemini all use this same list-of-roles idea. Once you can build and read this list, you can talk to any of them.
Want it another way?
Task
Append the assistant's reply "A named box that stores a value." to messages, so the program prints three messages and total: 3.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.