Lesson 9 of 15 · Advanced Techniques
System Prompts
What system prompts are, how system, user and assistant messages fit together, what to put in one, and why they are neither secret nor a security wall.
- 8 min read
- Beginner
- Reviewed
- What a system prompt is and how it differs from your messages
- The three message roles: system, user and assistant
- What belongs in a good system prompt
- Why a system prompt is not secret and not a security wall
Instructions behind the scenes
When you chat with an AI app, your messages are not the only instructions the model sees. The app usually adds a system prompt at the start of every conversation, setting out who the assistant is and how it should behave.
Conversations are passed to the model as a list of messages, each with a role:
| Role | Who writes it | Example |
|---|---|---|
| system | The app or developer | "You are a friendly cooking assistant for beginners..." |
| user | The person chatting | "How long do I boil an egg?" |
| assistant | The model (earlier replies) | "About 9 minutes for a firm yolk..." |
system_prompt = (
"You are a friendly cooking assistant for complete beginners. "
"Use metric units. Keep answers under 100 words. "
"If asked about something other than cooking, say so kindly "
"and steer back to cooking."
)
# A conversation, in the format most AI APIs use
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": "How long do I boil an egg?"},
{"role": "assistant", "content": "About 9 minutes for a firm yolk, 6 for a soft one."},
{"role": "user", "content": "And can you help with my maths homework?"},
]
for message in messages:
text = message["content"]
if len(text) > 50:
text = text[:50] + "..."
print(f"{message['role']:>9}: {text}")
print()
print("Messages sent with the next request:", len(messages))With every request, the app sends the system prompt and the whole history. The system prompt is the one message that stays the same in every conversation.
Prompt
SYSTEM: You are a friendly cooking assistant for complete beginners. If asked about something other than cooking, say so kindly and steer back to cooking. USER: Can you help with my maths homework?
Example response
I'm only set up to help with cooking, so maths homework is outside what I can do here. But if you'd like a quick snack to keep you going while you study, I can suggest something easy!
What goes in a system prompt
Write it like a briefing for a capable new employee on their first day:
- Role and purpose: who the assistant is and who it serves.
- Audience: who it will be talking to, and their level.
- Tone and style: formal or casual, length, use of lists.
- Rules: what it must always or never do, with reasons.
- Knowledge: facts it needs, such as product details, prices, opening hours, policies.
- Edge cases: what to do when unsure, when a question is out of scope, or when someone is upset.
- Default format: how answers should usually look.
## Role
You are the support assistant for Leafy, an online plant shop in the UK.
You help customers with orders, deliveries and plant care.
## Style
Friendly and calm. Plain English. Under 120 words unless asked for more.
## Rules
- Only promise refunds for plants that arrived damaged, because that is
our policy; for anything else, offer to pass the case to a human.
- If you are not sure of an answer, say so and offer a human. Never guess
about delivery dates.
## Facts
Delivery: 2 to 4 working days. Returns: within 14 days, damaged plants only.System prompt or user message?
Put stable things (role, tone, rules, facts) in the system prompt. Put the specific task and material for this request in the user message. That way one well-tested system prompt serves thousands of different requests.
Not secret, not a security wall
Common mistakes
Add the learner's next question to the conversation, the way an app does before each request.
messages = [
{"role": "system", "content": "You are a patient maths tutor for 12-year-olds."},
{"role": "user", "content": "What is a fraction?"},
{"role": "assistant", "content": "A fraction is a part of a whole, like 1/2 of a pizza."},
]
new_question = "Is 2/4 the same as 1/2?"
# 1. Add the new question to messages as a "user" message
# 2. Print how many messages there are, like: 4 messages
# 3. Print the roles in order, joined by ", "
Which of these belongs in a system prompt rather than in a single user message?
Summary
- A system prompt gives standing instructions for a whole conversation.
- Conversations are lists of system, user and assistant messages, sent in full with every request.
- Write system prompts like a briefing for a new employee: role, audience, tone, rules with reasons, facts and edge cases.
- Custom instructions in chat apps work like a personal system prompt.
- System prompts are not secret and not a security wall; enforce important limits in code.
Next: breaking big tasks into a chain of smaller prompts.
Finished reading? Tick it off to track your progress.