Sub topic 4
Setting behavior with a system message
This chapter assumes you can write Python functions, lists, and dictionaries, and that you have the chat loop and message list from the earlier chapters. Here you will add a system message. It tells the model how to behave, and it has to stay at the start of the history for the whole conversation.
What a system message is
Each entry in the history has a role. You have already used user and assistant. The third role, system, carries instructions from you, the developer, rather than words from the person typing. Typical uses:
- Set the tone ("Answer in two sentences or fewer.").
- Fix the persona or task ("You are a code reviewer for Python.").
- Set rules ("If you are unsure, say so instead of guessing.").
The model is stateless, so it does not remember the system message from a previous request. You send it again on every call, as the first item of the list.
Seeding the history
The chat loop from the earlier chapter already does this. It starts the history with one system entry:
SYSTEM_PROMPT = "You are a concise, helpful assistant."
history = [{"role": "system", "content": SYSTEM_PROMPT}]Every later message is appended after it. The list sent to the model on the second turn looks like this:
[
{"role": "system", "content": "You are a concise, helpful assistant."},
{"role": "user", "content": "What is a list comprehension?"},
{"role": "assistant", "content": "A short way to build a list from an iterable."},
{"role": "user", "content": "Show me one."},
]Keeping it at the start
The system message must survive every change you make to the history. Two operations can remove it by accident: trimming and resetting.
The trim_history function from the sliding-window chapter handles trimming. It separates system messages from the rest and only slices the rest:
def trim_history(history, max_messages):
"""Keep system messages plus the most recent max_messages other messages."""
system = [m for m in history if m["role"] == "system"]
rest = [m for m in history if m["role"] != "system"]
rest = rest[-max_messages:]
while rest and rest[0]["role"] != "user":
rest.pop(0)
return system + restA naive history[-10:] would eventually cut the system message off, and the bot would silently lose its instructions. Resetting has the same risk. A /reset command should rebuild the list from the system prompt instead of clearing it:
def new_history(system_prompt):
return [{"role": "system", "content": system_prompt}]Provider differences
Not every provider takes the system message as the first list item. Some APIs accept it as a separate system parameter next to messages. Keeping it in your own history is still a good choice, because you only need to adapt it in one place. If your provider wants a separate parameter, do the split inside complete():
def split_system(messages):
system = "\n".join(m["content"] for m in messages if m["role"] == "system")
rest = [m for m in messages if m["role"] != "system"]
return system, restThe rest of your program does not need to know the difference.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Slicing the whole history with `history[-n:]` | The system message is dropped after enough turns | Trim only the non-system messages |
| Clearing with `history.clear()` on reset | The bot loses its behavior after `/reset` | Rebuild with `new_history()` |
| Appending the system message after the user message | Some providers reject it, others weigh it differently | Always insert it first |
| Putting secrets in the system message | Users can often coax the model into repeating it | Keep keys and private data out of prompts |
Exercise
Your bot behaves correctly for the first 20 turns, then stops following its persona. The code trims with history = history[-20:]. What is the likely cause, and how do you fix it?
Show answer
Once the history grows past 20 messages, the slice cuts off the system message at index 0, so the model no longer receives its instructions. Fix it by trimming only the non-system messages, as trim_history does, and putting the system messages back in front.