Roles: user and assistant
Each message says who is speaking.
Every message has a role. 'user' is the person (or your program) talking to the model. 'assistant' is the model's earlier answers. A conversation is a list of messages that takes turns: user, assistant, user, and so on, always starting with a user message. The model reads the whole list each time and writes the next assistant message.
# To run on your own computer with an API key (it cannot run in this browser).
# First: pip install anthropic, and set the ANTHROPIC_API_KEY environment variable.
import anthropic
client = anthropic.Anthropic()
messages = [
{"role": "user", "content": "I'm planning a picnic."},
{"role": "assistant", "content": "Lovely! Where are you going?"},
{"role": "user", "content": "The park. What food should I bring?"},
]
reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=300, messages=messages)
print(reply.content[0].text)The model sees the earlier turns, so it knows 'the park' and 'picnic' belong together even though the last message doesn't repeat them.
You can even write assistant messages yourself. That is useful for examples (you will see this in the prompt unit) and for testing.
There is a third role idea, the system prompt, which gives the model standing instructions. In the Anthropic API it is a separate system parameter, not a message in the list. That is the next lesson.