System Prompts & Roles

Last reviewed Jul 7, 2026Prompt Engineering · intermediate

What you'll learn

  • Explain the difference between system, user, and assistant roles
  • Know what belongs in a system prompt vs a normal message
  • Write a system prompt that holds behaviour across a chat

Three roles in every chat

Chat models organise a conversation into roles:

  • System — the setup: who the model is, how it should behave, what it must never do. Sent once, applies throughout.
  • User — the person’s messages.
  • Assistant — the model’s replies.

In a consumer chatbot the system prompt is set for you (and “custom instructions” let you nudge it). In the API, you write it yourself — and it’s the most important part of building a reliable assistant.

What belongs in the system prompt

Put your non-negotiables here, because they need to survive the whole conversation:

  • Persona and scope — “You are a concise SQL tutor… if a question isn’t about SQL, say so.”
  • Format rules — “Always explain in ≤3 sentences, then a runnable example.”
  • Boundaries — what to refuse, what to escalate, what tone to hold.

Ordinary, one-off requests belong in user messages. The test: should this rule still apply ten messages from now? If yes, it’s a system prompt.

Two failure modes to avoid

  • Contradictions. “Be exhaustive” plus “always answer in one line” makes behaviour erratic — the model ping-pongs between impossible goals. Keep rules coherent.
  • Cache-busting volatility. If you use prompt caching to cut costs (see the token optimization playbook), a changing value like the current timestamp at the top of the system prompt invalidates the cache every call. Keep the system prompt byte-stable and put changing data lower in the prompt.

Finally, remember the boundary a system prompt can’t enforce: it’s instruction, not armour. Users can sometimes inject text that talks the model past its rules, which is why anything sensitive needs real guardrails, covered in the LLM security playbook.

That’s the Prompt Engineering core. Next module: Building with AI, starting with RAG.

Try it yourself — Set persistent rules
Prompt
[System]
You are a concise SQL tutor. Always: explain in <=3 sentences, then show a
runnable example. Never give the answer without the explanation. If a
question is not about SQL, say you only cover SQL.
[User]
How do I get the top 3 highest-paid employees per department?
Sample output
Use a window function to rank rows within each department, then keep ranks
1–3. ROW_NUMBER() restarts per department via PARTITION BY. Filter the
ranked result in an outer query.

SELECT * FROM (
  SELECT *, ROW_NUMBER() OVER (
    PARTITION BY dept_id ORDER BY salary DESC) AS rn
  FROM employees
) t WHERE rn <= 3;

Sample output — AI responses may vary.

Common mistakes

  • Putting critical rules in a normal message instead of the system prompt — they get diluted or forgotten as the conversation grows.
  • Writing a contradictory system prompt ('be extremely detailed' + 'always answer in one line'). The model can't satisfy both and behaviour becomes erratic.
  • Stuffing changing data (today's date, user name) into the system prompt when using prompt caching — it breaks the cache. Keep the system prompt stable.

How to check the AI's answer

  • Test the system prompt against off-topic and adversarial messages: does it hold its boundaries, or can a user talk it out of them?
  • Check the rules actually fire several turns deep, not just on the first reply — that's where weak system prompts break down.

Key terms

System promptInstructions that set behaviour, persona, and rules for an entire conversation, separate from individual user messages.
RoleThe labelled origin of a message in a chat — system, user, or assistant — which the model treats differently.
PersonaThe character or expertise the system prompt tells the model to adopt.

Test yourself

3 questions · answers reveal after you check.

1. The system prompt is best used for…

System prompts set conversation-wide behaviour, so they're the place for non-negotiable rules and persona.

2. In a chat API, the three common roles are…

System sets the rules, user is the person's messages, assistant is the model's replies.

3. Why keep changing values (like today's date) OUT of a cached system prompt?

Prompt caching keys on an identical prefix; a changing byte at the top busts the cache. Keep volatile content later in the prompt.