Skip to main content

Prompt Patterns

Learn proven prompt engineering patterns to get better results from AI models.

Introduction

Effective prompts follow patterns that consistently produce better results. This guide covers the most valuable patterns for AI development.

Foundational Patterns

Role Assignment

Define who the AI should be:

You are an experienced senior software engineer with 
expertise in React, TypeScript, and system design.

Role assignment creates consistent context throughout the conversation.

Context Setting

Provide relevant background:

Context:
- Building a SaaS dashboard application
- Using Next.js 14 with App Router
- Team uses Tailwind CSS and shadcn/ui
- Must support dark mode

Output Formatting

Specify exactly how you want responses:

Respond in the following format:
 
## Analysis
[Your analysis here]
 
## Implementation
````typescript
// Code here

Considerations

  • [Point 1]
  • [Point 2]

---

## Advanced Patterns

### Chain of Thought

Guide the AI through logical steps:

````markdown
Think through this problem step by step:

1. First, identify the core requirements
2. Then, consider potential approaches
3. Evaluate trade-offs of each approach
4. Finally, recommend the best solution with reasoning

Few-Shot Learning

Provide examples to establish patterns:

Convert these function names to documentation:
 
Example 1:
Input: getUserById
Output: Retrieves a user record by their unique identifier
 
Example 2:
Input: validateEmail
Output: Validates an email address format and returns a boolean
 
Now convert:
Input: processPayment
Output:

Structured Output

Request specific data formats:

Generate a component specification as JSON:
 
{
  "name": "component name",
  "props": [
    {
      "name": "prop name",
      "type": "string | number | boolean",
      "required": true | false,
      "description": "what it does"
    }
  ],
  "examples": ["usage example 1", "usage example 2"]
}

Domain-Specific Patterns

UI Component Pattern

Create a [Component Name] component with:
 
**Functionality:**
- [Feature 1]
- [Feature 2]
 
**Visual Requirements:**
- [Style requirement]
- [Animation/interaction]
 
**Technical Requirements:**
- TypeScript with proper types
- Accessible (WCAG 2.1 AA)
- Responsive design
 
**Props Interface:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| variant | string | Yes | Visual variant |
| size | string | No | Component size |

API Endpoint Pattern

Design an API endpoint for [purpose]:
 
**Endpoint:** [METHOD] /api/[path]
 
**Request:**
- Headers: [required headers]
- Body: [expected payload schema]
- Query params: [available params]
 
**Response:**
- Success (200): [response schema]
- Error cases: [error scenarios and codes]
 
**Validation Rules:**
- [Rule 1]
- [Rule 2]
 
**Security Considerations:**
- [Auth requirements]
- [Rate limiting]

Debugging Pattern

Debug this issue:
 
**Error Message:**

[paste error]


**Context:**
- File: [filename]
- Function: [function name]
- What I was doing: [action]

**What I've Tried:**
1. [Attempt 1]
2. [Attempt 2]

**Relevant Code:**
````typescript
// paste code

Please:

  1. Identify the root cause
  2. Explain why it's happening
  3. Provide a fix with explanation
  4. Suggest how to prevent this in the future

---

## Constraint Patterns

### Quality Constraints

````markdown
Requirements:
- Production-ready code (no TODOs or placeholders)
- Full error handling
- Proper TypeScript types (no `any`)
- Include JSDoc comments
- Follow [specific style guide]

Length Constraints

Keep your response:
- Under 50 lines of code
- Concise but complete
- No unnecessary comments

Negative Constraints

Do NOT:
- Use deprecated APIs
- Include inline styles
- Add unnecessary dependencies
- Over-engineer the solution

Combining Patterns

Complete Prompt Template

# Role
You are a [role] specializing in [domain].
 
# Context
[Background information]
 
# Task
[What needs to be done]
 
# Requirements
- [Requirement 1]
- [Requirement 2]
 
# Format
[How to structure the response]
 
# Constraints
- [What to avoid]
- [Limitations]
 
# Examples (if applicable)
[Input/output examples]

Pattern Reference Table

PatternWhen to UseKey Benefit
Role AssignmentStart of conversationConsistent expertise
Chain of ThoughtComplex reasoningBetter logic
Few-ShotEstablish formatsPattern matching
Structured OutputData extractionParseable results
Negative ConstraintsAvoid pitfallsCleaner output

Combine multiple patterns for best results. Start simple and add constraints as needed.

Next Steps