Prompt engineering patterns are reusable templates and techniques for structuring LLM interactions — providing proven approaches like few-shot examples, chain-of-thought reasoning, and role-based prompting that improve response quality, consistency, and task performance across different use cases.
What Are Prompt Patterns?
- Definition: Standardized templates for effective LLM prompting.
- Purpose: Improve quality, consistency, and reliability.
- Approach: Reusable structures that work across tasks.
- Evolution: Patterns discovered through experimentation.
Why Patterns Matter
- Consistency: Same structure produces predictable results.
- Quality: Proven techniques outperform ad-hoc prompts.
- Efficiency: Don't reinvent the wheel for each task.
- Scalability: Libraries of prompts for different needs.
- Debugging: Structured prompts are easier to iterate.
Core Prompt Patterns
Pattern 1: Role-Based Prompting:
``python
SYSTEM_PROMPT = """
You are an expert {role} with {years} years of experience.
Your specialty is {specialty}.
When answering:
- Be precise and technical
- Cite sources when possible
- Acknowledge uncertainty
"""
# Example
SYSTEM_PROMPT = """
You are an expert machine learning engineer with 10 years
of experience. Your specialty is optimizing LLM inference.
When answering:
- Be precise and technical
- Provide code examples when helpful
- Acknowledge uncertainty
"""
`
Pattern 2: Few-Shot Examples:
`python
prompt = """
Classify the sentiment of these reviews:
Review: "This product exceeded my expectations!"
Sentiment: Positive
Review: "Terrible quality, broke after one day."
Sentiment: Negative
Review: "It works, nothing special."
Sentiment: Neutral
Review: "{user_review}"
Sentiment:"""
`
Pattern 3: Chain-of-Thought (CoT):
`python
prompt = """
Solve this step by step:
Question: {question}
Let's think through this step by step:
1. First, I need to understand...
2. Then, I should consider...
3. Finally, I can conclude...
Answer:"""
# Zero-shot CoT (simpler)
prompt = """
{question}
Let's think step by step.
"""
`
Pattern 4: Output Formatting:
`python
prompt = """
Analyze this code and respond in JSON format:
`python`
{code}
Respond with:
{
"issues": [{"line": int, "description": str, "severity": str}],
"suggestions": [str],
"overall_quality": str // "good", "needs_work", "poor"
}
"""
`
Advanced Patterns
Self-Consistency (Multiple samples):
`python
# Generate multiple responses
responses = [llm.generate(prompt) for _ in range(5)]
# Take majority vote or consensus
final_answer = most_common(responses)
`
ReAct (Reasoning + Acting):
`
Question: What is the population of Paris?
Thought: I need to look up the current population of Paris.
Action: search("population of Paris 2024")
Observation: Paris has approximately 2.1 million people.
Thought: I have the answer.
Answer: Paris has approximately 2.1 million people.
`
Decomposition:
`python
prompt = """
Break this complex task into subtasks:
Task: {complex_task}
Subtasks:
1.
2.
3.
...
Now complete each subtask:
"""
`
Prompt Template Library
`python
TEMPLATES = {
"summarize": """
Summarize the following text in {length} sentences:
{text}
Summary:""",
"extract": """
Extract the following information from the text:
{fields}
Text: {text}
Extracted (JSON):""",
"transform": """
Transform this {source_format} to {target_format}:
Input:
{input}
Output:""",
"critique": """
Review this {artifact_type} and provide:
1. Strengths
2. Weaknesses
3. Suggestions for improvement
{artifact}
Review:"""
}
`
Best Practices
Structure:
``
1. Role/Context (who the LLM is)
2. Task (what to do)
3. Format (how to respond)
4. Examples (if few-shot)
5. Input (user's content)
Tips:
- Be specific and explicit.
- Use delimiters for sections (`, ---, ###).
- Put instructions before content.
- Include format examples.
- Test with edge cases.
Anti-Patterns to Avoid:
`
❌ Vague: "Make this better"
✅ Specific: "Improve clarity by using shorter sentences"
❌ No format: "Analyze this"
✅ With format: "Analyze this and list 3 key points"
❌ Contradictory: "Be brief but comprehensive"
✅ Clear: "Summarize in 2-3 sentences"
``
Prompt engineering patterns are the design patterns of AI development — proven templates that solve common problems, enabling faster development and better results than starting from scratch for every LLM interaction.