Understanding the Chat Interface
How LLM chat UIs work: roles, turns, and session context.
The Three Roles
Every conversation with an LLM is built on three roles: system, user, and assistant.
The system role sets the rules and persona before the conversation starts. The user role is you sending messages. The assistant role is the model replying.
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-your-key-here')
message = client.messages.create(
model='claude-opus-4-5',
max_tokens=256,
system='You are a helpful cooking assistant.',
messages=[
{'role': 'user', 'content': 'What is mise en place?'}
]
)
print(message.content[0].text)The System Message
The system message is the model's instruction manual. It runs before the first user turn and stays active for the whole session.
Use it to set persona, tone, domain restrictions, or output format. The model treats it as its guiding context throughout the conversation.
import openai
client = openai.OpenAI(api_key='sk-your-key-here')
response = client.chat.completions.create(
model='gpt-4o',
messages=[
{
'role': 'system',
'content': 'You are a senior Python engineer. '
'Always include type hints and docstrings in your examples.'
},
{
'role': 'user',
'content': 'Show me a function that parses JSON safely.'
}
]
)
print(response.choices[0].message.content)All lessons in this course
- Understanding the Chat Interface
- Types of Requests AI Can Handle
- How AI Generates Responses
- What AI Cannot Do