Role Prompting

Assign the model a persona to shape tone, depth, and vocabulary.

Role prompting tells the model who to be: "You are a senior security engineer" or "You are a friendly kindergarten teacher." This sets tone, vocabulary, and how much detail to give.

Where to put it

The system message is the natural home for a role, plus any standing rules about format and behaviour.

Good roles are concrete

  • Include the audience: explain to a beginner.
  • Include constraints: use plain language, avoid jargon.
  • Include the goal: help them fix the bug.

Example

Example · json
{
  "messages": [
    {"role": "system", "content": "You are a patient coding mentor. Explain to a beginner, use short sentences, and always show a tiny example."},
    {"role": "user", "content": "What is a variable?"}
  ]
}

When to use it

  • A cybersecurity firm prompts 'You are a senior penetration tester' to get replies with the vocabulary and risk-awareness of that role.
  • A children's education app assigns 'You are a friendly dinosaur teacher who explains science with dino facts' to make lessons more engaging.
  • A technical writing team uses 'You are a concise technical writer who avoids jargon' to get documentation drafts that need minimal editing.

More examples

Assign expert persona via system prompt

Sets an expert persona in the system prompt so every reply reflects that role's knowledge and tone.

Example · python
from openai import OpenAI

client = OpenAI()
r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[
        {'role': 'system', 'content':
            'You are a senior DevOps engineer with 15 years of experience. '
            'Give concise, opinionated advice.'},
        {'role': 'user', 'content': 'Should I use Kubernetes for a team of 3?'}
    ]
)
print(r.choices[0].message.content)

Role shapes vocabulary and depth

Runs the same question with two audience personas to show how the role controls vocabulary and depth.

Example · python
from openai import OpenAI

client = OpenAI()
question = 'What is a neural network?'

for role in ['a 5-year-old child', 'a PhD machine-learning researcher']:
    r = client.chat.completions.create(
        model='gpt-4o-mini',
        messages=[
            {'role': 'system', 'content': f'Explain everything as if talking to {role}.'},
            {'role': 'user',   'content': question}
        ],
        max_tokens=80
    )
    print(f'--- Audience: {role} ---')
    print(r.choices[0].message.content)
    print()

Persona with behavioural constraints

Combines persona assignment with behavioural guardrails to keep the model in-character and on-topic.

Example · python
from openai import OpenAI

client = OpenAI()
r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[
        {'role': 'system', 'content':
            'You are Aria, a friendly support agent for CloudStore. '
            'Only discuss CloudStore products. '
            'If asked about competitors, politely decline and redirect.'},
        {'role': 'user', 'content': 'How does your pricing compare to AWS?'}
    ]
)
print(r.choices[0].message.content)

Discussion

  • Be the first to comment on this lesson.