Sub topic 3
System Messages: Setting Behavior and Constraints
This chapter assumes you can read Python lists and dictionaries, know that a chat API takes a list of messages with a role and content, and have called an HTTP API or SDK before.
What a system message does
A system message is a message with the role system. It tells the model how to behave for the whole conversation. The user message says what to do now. The system message says who the model is, how it should sound, and what it must or must not do.
Typical uses:
- Set a persona or job ("You are a support agent for a bike shop").
- Set tone and length ("Answer in plain English, under three sentences").
- Set output format ("Reply with one word").
- Set constraints ("Do not give legal advice").
messages = [
{"role": "system", "content": "You are a concise assistant that answers in plain English."},
{"role": "user", "content": "What is a REST API?"},
]The system message goes first. The model reads it before every user turn, so it shapes every reply.
Writing an effective system message
Vague instructions give vague behavior. Compare these two:
| Weak | Stronger |
|---|---|
| "Be helpful." | "You are a helpful assistant. Keep answers under three sentences." |
| "Don't be wrong." | "If you are not sure, say so. Do not invent URLs or function names." |
| "Write nicely." | "Write for a beginner. Define any term you use for the first time." |
Follow these habits:
- State the role in one sentence.
- Give concrete, checkable rules. "Under three sentences" can be tested. "Be short" is harder to test.
- Say what to do, not only what to avoid. "If the question is off-topic, reply that you only answer bike questions" works better than "Never talk about other topics".
- Define the output format when code will read the reply.
- Keep it short. Long lists of rules compete with each other, and the model may follow some and drop others.
A system message in a real call
The system message is the first item in the history, and you send it with every request.
from openai import OpenAI
client = OpenAI()
history = [
{"role": "system", "content": "You are a helpful assistant. Keep answers under three sentences."}
]
def chat(user_text):
history.append({"role": "user", "content": user_text})
response = client.chat.completions.create(
model="MODEL_NAME", # replace with a chat model available to you
messages=history,
)
reply = response.choices[0].message.content
history.append({"role": "assistant", "content": reply})
return reply
print(chat("What is a list in Python?"))
print(chat("How do I add an item to it?"))Because the system message stays at position 0 of history, the length rule applies to both answers. You write it once and it governs the whole conversation.
What a system message cannot do
A system message is a strong hint, not a lock. The model usually follows it, but it can fail in these ways:
- Drift. In a long conversation, early rules can be followed less closely.
- Conflict. If a user asks for something that contradicts the system message, the model has to choose. Most providers train models to favor the system message, but this is not guaranteed.
- Leakage. A user may be able to get the model to repeat the system message. Do not put secrets, API keys, or private data in it.
Where the system message goes
OpenAI-style chat APIs put it in the messages list with the role system, as shown above. The Anthropic Messages API takes it as a separate top-level system parameter instead, and the messages list holds only user and assistant turns.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="MODEL_NAME", # replace with a model available to you
max_tokens=300,
system="You are a helpful assistant. Keep answers under three sentences.",
messages=[
{"role": "user", "content": "What is a list in Python?"},
],
)
print(response.content[0].text)The idea is the same in both. Only the location differs. A later chapter compares providers in more detail.
Common mistakes
- Putting rules in the first user message instead of the system message. Then they may be trimmed away when you shorten the history.
- Forgetting to resend the system message. The API is stateless, so every request must include it.
- Writing contradictory rules, such as "be very detailed" and "answer in one line".
- Placing the system message after user turns in an OpenAI-style list. Keep it first.
Exercise
You build a chatbot that answers only questions about a bike shop. Write a system message of two or three sentences that sets the role, one checkable length rule, and what to do for off-topic questions.
Show answer
For example: "You are a support assistant for a bike shop. Answer in no more than three sentences. If a question is not about bikes or the shop, reply that you can only help with bike questions." This names the role, gives a testable length limit, and says what to do instead of only what to avoid.