Lesson 5 of 8

Role & Persona Prompting

Assigning roles and personas to shape AI behavior and expertise

What is Role Prompting?

Role prompting assigns a specific role, persona, or identity to the AI model. By telling the model "you are a [role]", you activate relevant knowledge patterns and communication styles from its training data, leading to more focused and expert responses.

🎭 Why Role Prompting Works

  • Activates Expertise: Primes the model to draw from specific knowledge domains
  • Sets Tone: Establishes appropriate communication style
  • Provides Context: Gives the model a framework for responses
  • Improves Consistency: Maintains character throughout conversation

Basic Role Prompting

// Simple role assignment
"You are a senior TypeScript developer with 10 years of experience.
Review this code and suggest improvements:

[code]"

// More detailed role
"You are a security-focused backend engineer who specializes in:
- Authentication and authorization
- API security best practices
- OWASP Top 10 vulnerabilities

Analyze this API endpoint for security issues:
[code]"

// Expert with specific background
"You are a React performance consultant who has:
- Optimized apps serving millions of users
- Deep knowledge of React internals
- Experience with profiling and debugging

My app is slow when rendering large lists. Help me diagnose 
and fix the performance issues."

Developer Role Examples

// Code Reviewer
"You are a meticulous code reviewer at a top tech company.
Your reviews are known for catching subtle bugs and 
suggesting elegant improvements.

Review this pull request focusing on:
- Correctness and edge cases
- Code clarity and maintainability
- Performance implications
- Test coverage gaps

PR Code:
[code]"

// System Architect
"You are a senior system architect designing for scale.
You prioritize: simplicity, reliability, and maintainability.

Design a system for a real-time chat application that:
- Supports 1 million concurrent users
- Delivers messages in under 100ms
- Handles file uploads up to 10MB
- Maintains message history

Provide architecture diagram (ASCII), component breakdown,
and technology recommendations."

// DevOps Engineer
"You are a DevOps engineer specializing in Kubernetes 
and cloud-native applications.

My pods keep getting OOMKilled. Here's my deployment:
[yaml]

Diagnose the issue and provide solutions."

Combining Role with Constraints

// Role + Audience
"You are a senior developer explaining concepts to a 
junior developer on their first week at the job.

Use simple language, avoid jargon, and provide examples.
If you must use technical terms, explain them.

Explain: What is dependency injection and why do we use it?"

// Role + Format
"You are a technical writer creating API documentation.

Document this function following these rules:
- Use JSDoc format
- Include all parameters with types
- Provide 2 usage examples
- List possible errors
- Keep descriptions concise

function createUser(email, password, options) {
  // implementation
}"

// Role + Constraints
"You are a performance-obsessed JavaScript developer.

Suggest the most efficient way to:
- Find duplicates in an array of 1 million items

Requirements:
- Must be O(n) time complexity
- Memory usage under 100MB
- Works in both Node.js and browsers
- No external libraries"

Multi-Perspective Roles

// Get multiple viewpoints
"I'm designing a new authentication system. 
Provide feedback from these three perspectives:

AS A SECURITY ENGINEER:
Analyze the security implications, potential vulnerabilities,
and recommend security best practices.

AS A UX DESIGNER:
Evaluate the user experience, friction points, and 
suggest improvements for user satisfaction.

AS A BACKEND DEVELOPER:
Consider implementation complexity, scalability, and 
maintenance burden.

Proposed System:
Users authenticate via email magic links. No passwords.
Links expire after 10 minutes. Sessions last 30 days.

Provide feedback from each perspective:"

Persona Templates

// Template: Technical Expert
const expertPrompt = `
You are a [SPECIALTY] expert with deep knowledge of:
- [AREA 1]
- [AREA 2]
- [AREA 3]

Your communication style is [STYLE].
You always [BEHAVIOR].
You never [ANTI-BEHAVIOR].

[TASK]
`;

// Example filled in:
"You are a database performance expert with deep knowledge of:
- Query optimization and indexing strategies
- PostgreSQL internals and execution plans
- Database scaling patterns

Your communication style is direct and practical.
You always provide benchmarks and metrics to support claims.
You never recommend changes without explaining trade-offs.

Optimize this slow query:
[SQL query]"

// Template: Mentor/Teacher
const mentorPrompt = `
You are a patient [ROLE] who teaches by:
1. First explaining the concept simply
2. Then showing a practical example
3. Finally, giving a practice exercise

Adapt your teaching to a [LEVEL] learner who already knows
[PREREQUISITES] but is new to [TOPIC].

Teach: [CONCEPT]
`;

Role Prompting for Different Tasks

Task Effective Role
Code Review Senior developer at FAANG, Tech lead, Security auditor
Documentation Technical writer, API documentation specialist
Architecture System architect, Solutions architect, Staff engineer
Debugging Debugging specialist, Performance engineer
Learning Patient tutor, Coding bootcamp instructor
Testing QA engineer, Test automation specialist

System Messages (Chat APIs)

// In chat APIs, role goes in the system message
const messages = [
  {
    role: "system",
    content: `You are a senior full-stack developer specializing 
    in React and Node.js. You write clean, well-tested code 
    following industry best practices. 
    
    When reviewing code, you:
    - Point out bugs and potential issues
    - Suggest performance improvements
    - Recommend better patterns when applicable
    - Praise good practices to reinforce them
    
    Keep responses concise and actionable.`
  },
  {
    role: "user",
    content: "Review this React component: [code]"
  }
];

// The system message persists across the conversation
// maintaining the role context

⚠️ Role Prompting Tips

  • • Be specific about expertise areas, not just job titles
  • • Include relevant experience and specializations
  • • Define communication style and behaviors
  • • Don't claim the AI is human or use deceptive personas
  • • Match the role's expertise level to the task complexity