Intermediate18 min

Learn how to use role prompting to shape AI responses by assigning specific expertise, perspectives, and communication styles.

Role Prompting and Personas

Role prompting assigns the AI a specific identity, expertise, or perspective. This powerful technique shapes how the AI approaches problems and communicates solutions.

What is Role Prompting?

Role prompting tells the AI to "act as" a specific type of expert or adopt a particular perspective. This primes the model to draw on relevant knowledge and communication patterns.

Without Role:

Terminal
Review this code for issues.

With Role:

Terminal
You are a senior security engineer with 10 years of experience in web application security.

Review this authentication code, focusing on:
- OWASP Top 10 vulnerabilities
- Session management best practices
- Input validation gaps

[code]

Why Role Prompting Works

When you assign a role, the AI:

  • Focuses on domain-specific knowledge
  • Adopts appropriate terminology
  • Considers relevant edge cases
  • Communicates at the expected level

Common Developer Roles

Security Expert

Terminal
You are a senior application security engineer specializing in web security.

Analyze this code for security vulnerabilities:
- SQL injection risks
- XSS vulnerabilities
- Authentication/authorization flaws
- Sensitive data exposure
- Input validation issues

Prioritize findings by severity (Critical, High, Medium, Low).

[code]

Performance Engineer

Terminal
You are a performance optimization specialist with deep knowledge of React and browser rendering.

Analyze this component for performance issues:
- Unnecessary re-renders
- Memory leaks
- Bundle size impact
- Runtime complexity
- Missing optimizations (memoization, lazy loading)

Provide specific metrics where possible.

[code]

Code Reviewer

Terminal
You are a senior developer conducting a thorough code review.

Review this pull request focusing on:
- Code correctness and logic
- Error handling completeness
- Code readability and maintainability
- Test coverage gaps
- Documentation needs

Use a constructive tone and explain the "why" behind suggestions.

[code]

DevOps Engineer

Terminal
You are a DevOps engineer experienced with Kubernetes and cloud infrastructure.

Review this deployment configuration for:
- Resource allocation issues
- Security misconfigurations
- High availability concerns
- Scaling considerations
- Cost optimization opportunities

[configuration]

Combining Role with Expertise Level

Adjust the expertise level based on your needs:

Junior Developer Explanation

Terminal
You are a patient senior developer explaining code to a junior teammate.

Explain how this authentication flow works:
- Use simple language
- Avoid jargon or explain it when necessary
- Include analogies where helpful
- Point out common pitfalls beginners encounter

[code]

Expert-Level Discussion

Terminal
You are a systems architect discussing with a fellow expert.

Analyze the trade-offs in this distributed system design:
- CAP theorem implications
- Consistency vs availability decisions
- Partition tolerance strategies
- Performance characteristics at scale

Assume familiarity with distributed systems concepts.

[design]

Role Prompting for Specific Tasks

Database Expert for Query Optimization

Terminal
You are a database performance specialist with expertise in PostgreSQL.

Optimize this query:
- Analyze the execution plan mentally
- Identify missing indexes
- Suggest query restructuring
- Consider table partitioning if relevant
- Note any N+1 query patterns

Current query:
[query]

Table schemas:
[schemas]

API Designer

Terminal
You are an API design expert following REST best practices and the OpenAPI specification.

Design endpoints for a user management system:
- Follow RESTful conventions
- Include appropriate HTTP methods and status codes
- Define request/response schemas
- Consider pagination, filtering, sorting
- Include authentication requirements
- Document rate limiting considerations

Accessibility Expert

Terminal
You are a web accessibility specialist certified in WCAG 2.1 guidelines.

Audit this component for accessibility:
- Semantic HTML usage
- ARIA attributes correctness
- Keyboard navigation support
- Screen reader compatibility
- Color contrast issues
- Focus management

Categorize issues by WCAG level (A, AA, AAA).

[component code]

Multi-Perspective Analysis

Use multiple roles for comprehensive analysis:

Terminal
Analyze this authentication system from three perspectives:

**As a Security Engineer:**
- What vulnerabilities exist?
- What attack vectors are possible?

**As a UX Designer:**
- Is the flow intuitive?
- What friction points exist for users?

**As a DevOps Engineer:**
- How does this scale?
- What monitoring is needed?

[system description and code]

Role + Constraints

Combine roles with specific constraints:

Terminal
You are a senior TypeScript developer who:
- Writes strict, type-safe code
- Prefers functional programming patterns
- Values immutability
- Uses descriptive naming
- Avoids any type assertions

Refactor this JavaScript code to TypeScript following these principles:

[code]

Role Prompting Patterns

The Mentor Pattern

Terminal
You are an experienced mentor helping me learn [topic].

When I make mistakes:
- Explain what went wrong
- Show the correct approach
- Explain why it's better
- Give me a similar exercise to practice

Let's start with: [question or code]

The Pair Programmer Pattern

Terminal
You are my pair programming partner. We're working on [task].

Think out loud as you:
- Consider different approaches
- Weigh trade-offs
- Make decisions
- Write code incrementally

Ask clarifying questions when needed.

The Code Archaeologist Pattern

Terminal
You are a code archaeologist who specializes in understanding legacy systems.

Analyze this legacy code:
- Explain what it does
- Identify the original intent
- Document hidden assumptions
- Note technical debt
- Suggest modernization steps

[legacy code]

When to Use Role Prompting

ScenarioRecommended Role
Security reviewSecurity engineer
Performance optimizationPerformance specialist
API designAPI architect
Code explanationSenior mentor
Legacy code analysisCode archaeologist
Accessibility auditA11y specialist
Database optimizationDBA
Infrastructure reviewDevOps engineer

Common Mistakes

1. Conflicting Roles

Bad:

Terminal
You are a junior developer and a senior architect.
Review this code.

Good: Choose one clear role.

2. Role Without Context

Bad:

Terminal
You are a React expert.
Fix this bug.

Good:

Terminal
You are a React expert specializing in hooks and state management.
This component has a stale closure issue. Debug and fix it.
[code]

3. Overly Generic Roles

Bad:

Terminal
You are a programmer.

Good:

Terminal
You are a senior TypeScript developer with expertise in Node.js backend development and PostgreSQL.

Practice Exercise

Create role prompts for these scenarios:

  1. Reviewing a database schema for a new e-commerce feature
  2. Explaining async/await to someone new to JavaScript
  3. Auditing a React app for performance issues
  4. Designing a caching strategy for a high-traffic API

For each, specify:

  • The role and expertise level
  • Specific focus areas
  • Expected output format

Summary

  • Role prompting shapes AI responses by assigning expertise
  • Be specific about the role, expertise level, and focus areas
  • Combine roles with constraints for more targeted output
  • Use multi-perspective analysis for comprehensive reviews
  • Match the role to your specific task needs

Next Steps

Now that you can shape AI perspectives with roles, let's learn about structured prompts using XML tags—a technique for organizing complex prompts clearly.

Mark this lesson as complete to track your progress