Skip to Content

System Prompts and Personas

When to use

Set the LLM’s identity and behavioural boundaries up front — the most powerful place to control tone, constraints and refusal patterns.

Analogy

Casting an actor for a movie. Tell them: you are a grumpy retired detective; do not give away the plot; speak in short sentences.

Data-flow diagram

   messages = [
     {'role':'system','content': PERSONA + RULES + FORMAT},
     {'role':'user',  'content': USER_TEXT},
     ...
   ]

Deep explanation

System prompts set the persona, rules and output format. They are the FIRST part of context and the easiest place to embed must-follow constraints. Keep them short and specific. Persona verbs outperform abstract rules. Always test with edge inputs: empty, off-topic, jailbreak attempt.

Examples

Example 1

system = "You are a senior Python code reviewer. Focus on security. Output a 3-bullet list, each bullet one sentence."
resp = openai.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role':'system','content':system},
              {'role':'user','content':'def add(a,b): return a+b'}])
print(resp.choices[0].message.content)

Persona verbs (you are a reviewer) outperform abstract adjectives (you are careful).

Example 2

system = """You translate English to French.
Rules:
  - Reply with ONLY the French translation, no explanations.
  - Never add punctuation unless it exists in the source.
  - If unsure, output (untranslated)."""
resp = openai.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role':'system','content':system},
              {'role':'user','content':'Translate: "good morning"'}])

Concrete format rules (3 bullets, one sentence each) reduce output variance.

Example 3

system = "You extract invoice line items into JSON. Reply with a JSON array of {description, amount}."
resp = openai.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role':'system','content':system},
              {'role':'user','content':'2 apples at $1.20 each, 1 milk at $3.50'}])
print(resp.choices[0].message.content)

Pre-declaring JSON schema saves parse time and reduces downstream codepaths.

Common mistake

Putting too many rules into the system prompt. Long prompts dilute the signal — by the end the model has forgotten the first rule. Keep under 300 words.

Key takeaway

System prompt equals persona plus rules plus format. Keep it under 300 words. Persona verbs beat abstract rules. Combine with response_format and validation.

Production Failure Playbook

Failure scenario 1: forgot-system-prompt

Failure scenario 2: system-too-long