Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 38 ยท Techniques: Few-shot, Chain-of-Thought and Roles

Roles, Personas and System Prompts

Learn how roles and personas shape tone and focus, how system, user and assistant messages differ, and how to write and test a system prompt in Python.

Most chat-style model APIs do not take a single string. They take a list of messages, each with a role that says who is speaking. Understanding these message roles, and the separate idea of giving the model a persona, is central to building applications.

The three message roles

Role Who writes it Typical content
system The application developer Standing rules: purpose, tone, boundaries, output format
user The end user (or your code, per request) The request and its data
assistant The model (or you, to show examples) Earlier replies, or example replies

Providers differ in details (some call the first role "developer", some pass the system prompt as a separate field), but the idea is the same: the system message holds stable instructions; user messages hold the changing request; assistant messages hold history.

flowchart TB
  S["system: stable rules<br/>(you write once)"] --> M["Model"]
  H["history: user and assistant turns"] --> M
  U["user: the new request"] --> M
  M --> A["assistant reply"]

The model receives the whole list every time; it has no memory between calls.

Models are trained to give system instructions extra weight, so put durable rules there. But the weight is not absolute: a user message can still try to override it (see the safety chapter), so never rely on a system prompt as your only security.

What a good system prompt contains

  • Purpose: what the assistant is for and for whom.
  • Tone and style: concrete ("plain English, friendly, no emoji").
  • Scope: what to help with, and what to decline or redirect.
  • Format rules: length, structure, language.
  • Fallbacks: what to do when unsure or out of scope.
  • Facts it may rely on: product names, policies (or how it will receive them).
TEXT
You are the support assistant for Northwind Books, an online bookshop.
Help customers with orders, returns and shipping.
Tone: warm, brief, plain English. At most 4 sentences unless asked for more.
Use only the policy text provided in the conversation. If it does not cover
the question, say so and offer to connect the customer to a human.
Never ask for passwords or full card numbers.

Role prompting (personas)

Role prompting tells the model to act as a certain kind of expert: "You are a senior database engineer." It can shift vocabulary, level of detail, and what the model pays attention to.

What it does well: sets tone and viewpoint; focuses answers on the right domain; makes the audience-appropriate voice easier to hold.

What it does not do: it does not add knowledge the model lacks, and it does not reliably improve factual accuracy. "You are a world-class doctor" does not make medical answers safe. Treat roles as style and focus, not as credentials.

Better role prompts describe behavior, not just a title:

TEXT
Weak:   You are an expert editor.
Better: You are a copy editor for a technical blog. Fix grammar and
        clarity. Keep the author's voice. Do not change technical claims.
        List each change you made at the end.

Build a message list in code

APIs usually take a list of dictionaries. This block builds one and keeps the system prompt separate from per-request data. It makes no API call; it shows the structure.

Python A chat message list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the whole history is sent on every request. The "conversation" is something your application reconstructs each time.

Test persona and rule compliance

A system prompt is a spec: you can write checks for it. Here is a stand-in test harness with canned replies, checking the rules from the system prompt above (length, no forbidden requests).

Python Check replies against system-prompt rules (canned replies)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Few-shot through message history

You can show examples as fake earlier turns: a user message followed by the ideal assistant message. Some teams prefer this to examples inside one big prompt because it matches how the model was trained to converse. Either works; test both.

Keep system prompts maintainable

  • Version them like code (versioning).
  • Keep them as short as the job allows; every line is read on every call.
  • Remove contradictions when you add rules.
  • Do not put secrets in them. A model can often be coaxed into repeating its instructions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should stable, durable instructions normally go?

    The system message holds standing rules; user messages carry the changing request.
  2. 2. What does "You are a world-class doctor" reliably add?

    Roles shape tone and viewpoint; they do not add facts or guarantee correctness.
  3. 3. Why is the whole history sent with each request?

    The application rebuilds the conversation every time because the model is stateless.
  4. 4. Which should NOT be placed in a system prompt?

    Models can often be coaxed into revealing their instructions, so never store secrets there.

Summary

  • Chat APIs take a list of messages with roles: system (stable rules), user (the request) and assistant (replies or examples).
  • A good system prompt states purpose, tone, scope, format, fallbacks and the facts it may use.
  • Role prompting shifts style and focus; it does not add knowledge or guarantee accuracy. Describe behavior, not just titles.
  • Build and test message lists in code, send the full history each call and keep secrets out of prompts.

Next: Getting Structured Output (JSON)