Home Knowledge Base Prompt engineering patterns

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?

Why Patterns Matter

Core Prompt Patterns

Pattern 1: Role-Based Prompting:

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:

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):

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:

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:

prompt = """
Break this complex task into subtasks:

Task: {complex_task}

Subtasks:
1.
2.
3.
...

Now complete each subtask:
"""

Prompt Template Library

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:

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.

prompt patternsprompt engineeringtemplatesfew-shotchain of thoughtrole prompting

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.