Using a Language Model in Code › Adding Context with System Messages

Michael BrenndoerferJune 27, 20252 min read
AI Agent HandbookAll chapters
Part 2 of 15Language Models - The Brain of the Agent
Chapter 5Using a Language Model in Code › Adding Context with System Messages
Reading section 4 of 15 · Adding Context with System Messages · 2 minSection 4 of 15 · 2 min

Book contents

Section 4 of 15 · 2 min read

Using a Language Model in Code › Adding Context with System MessagesOne section · Sections in this chapter4 / 15 ↓

The example above works, but it's missing something important: we haven't told the model who it is or how it should behave. That's where system messages come in.

Example (GPT-5)

In[2]:
Code
## Using GPT-5 for basic text generation
response = client.chat.completions.create(
    model="gpt-5",
    messages=[
        {
            "role": "system",
            "content": "You are a helpful personal assistant. Be concise and over-friendly.",
        },
        {"role": "user", "content": "What is 2 + 2?"},
    ],
)

print(response.choices[0].message.content)

The system message sets the tone and behavior. It's like giving the model a job description before asking it to work. Without it, the model will still respond, but you have less control over how it responds.

Think of it this way: if you walked up to someone and said "What is 2 + 2?", they might give you a straightforward answer, or they might be confused about why you're asking such a simple question, or they might launch into a lecture about arithmetic. The system message is like introducing yourself first: "Hi, I'm your personal assistant, and I'm here to help you with quick, friendly answers."

For intermediate readers: System messages are powerful but not foolproof. The model can still deviate from them, especially if user messages strongly contradict the system instructions. In practice, you'll often need to reinforce important behaviors through examples (few-shot prompting) or by structuring your prompts carefully. System messages work best for setting general tone and role, less well for enforcing strict rules.