Sub topic 1
What a System Prompt Is and How Roles Work
Prerequisites
This chapter assumes you have:
- Called an LLM chat API at least once.
- Worked with JSON objects and lists.
- Read short Python programs.
The working model
A chat API does not take a single block of text. It takes a list of messages, and each message has a role and content. The model reads the whole list and writes the next assistant message.
| Role | Written by | Typical content |
|---|---|---|
| system | The developer | Task, rules, output format, tone |
| user | The end user, or your code on their behalf | The question, plus any data you attach |
| assistant | The model, or earlier replies you replay | Previous answers in the conversation |
Some APIs accept the system prompt as a separate top-level parameter instead of a message. Some also add roles such as developer or tool. The idea is the same in each case: instructions you control are kept apart from input you don't control.
messages = [
{"role": "system", "content": "You are a support assistant for Acme Billing. Answer in two sentences or fewer."},
{"role": "user", "content": "Why was I charged twice in March?"},
]The model does not remember earlier requests. For a multi-turn chat, your code sends the system message and the full history on every call.
Why placement matters
Models are trained to give system content more weight than user content. Where you put an instruction therefore changes how reliably it is followed.
- Put fixed rules in the system message. They apply to every turn and stay the same across users.
- Put per-request material in the user message. This includes the question, retrieved documents, and the current date.
- Keep the two apart. If a rule sits in the user message, a later user message can easily override it.
def build_messages(system_prompt: str, history: list[dict], question: str) -> list[dict]:
return (
[{"role": "system", "content": system_prompt}]
+ history
+ [{"role": "user", "content": question}]
)The system message goes first, then the history, then the new question. Your code never edits the system message while handling a request.
Common errors
- Mixing request data into the system prompt. If you paste each user's text into the system message, you give untrusted text the highest-priority role. It also makes the prompt differ on every call, so you can't test it as one fixed artifact.
- Assuming the model remembers. If you forget to resend history, the model answers each message as if it were the first.
- Treating the system prompt as a secret. Users can often coax a model into repeating its instructions. Never put API keys, internal URLs, or private business rules in it.
- Treating the system prompt as a security boundary. A role label ranks instructions but does not enforce anything. Enforce permissions in your code, not in prose.
- Copying role names between providers. Some providers use a top-level
systemfield, and some acceptdeveloperas well assystem. Check the documentation for the API you call. - Writing a vague prompt. "Be helpful and professional" gives the model nothing to act on. The following chapters replace it with a stated task, concrete rules, and an output format.
Where this leads
Each later chapter fills in one part of a system prompt: the task and audience, structure, constraints, output format, examples, context, and tests. All of them depend on the separation you set up here. Fixed instructions go in the system role. Variable input goes in the user role.
Exercise
A developer builds a chatbot that inserts each customer's pasted email into the system message, so the model "knows the context". Name two problems with this design and say where the email should go.
Show answer
First, the customer's text now has the highest-priority role, so any instructions inside the email carry more weight than they should. Second, the system prompt changes on every request, so you can't test it as one fixed artifact. The email belongs in the user message, ideally inside clear delimiters, while the system message keeps only fixed instructions.