Prompt Engineering Patterns
Master advanced prompt engineering techniques to maximize LLM performance, reliability, and controllability.
When to Use This Skill
- Designing complex prompts for production LLM applications
- Optimizing prompt performance and consistency
- Implementing structured reasoning patterns (chain-of-thought, tree-of-thought)
- Building few-shot learning systems with dynamic example selection
- Creating reusable prompt templates with variable interpolation
- Debugging and refining prompts that produce inconsistent outputs
- Implementing system prompts for specialized AI assistants
- Using structured outputs (JSON mode) for reliable parsing
Core Capabilities
1. Few-Shot Learning
- Example selection strategies (semantic similarity, diversity sampling)
- Balancing example count with context window constraints
- Constructing effective demonstrations with input-output pairs
- Dynamic example retrieval from knowledge bases
- Handling edge cases through strategic example selection
2. Chain-of-Thought Prompting
- Step-by-step reasoning elicitation
- Zero-shot CoT with "Let's think step by step"
- Few-shot CoT with reasoning traces
- Self-consistency techniques (sampling multiple reasoning paths)
- Verification and validation steps
3. Structured Outputs
- JSON mode for reliable parsing
- Pydantic schema enforcement
- Type-safe response handling
- Error handling for malformed outputs
4. Prompt Optimization
- Iterative refinement workflows
- A/B testing prompt variations
- Measuring prompt performance metrics (accuracy, consistency, latency)
- Reducing token usage while maintaining quality
- Handling edge cases and failure modes
5. Template Systems
- Variable interpolation and formatting
- Conditional prompt sections
- Multi-turn conversation templates
- Role-based prompt composition
- Modular prompt components
6. System Prompt Design
- Setting model behavior and constraints
- Defining output formats and structure
- Establishing role and expertise
- Safety guidelines and content policies
- Context setting and background information
Quick Start
python
1from langchain_anthropic import ChatAnthropic
2from langchain_core.prompts import ChatPromptTemplate
3from pydantic import BaseModel, Field
4
5# Define structured output schema
6class SQLQuery(BaseModel):
7 query: str = Field(description="The SQL query")
8 explanation: str = Field(description="Brief explanation of what the query does")
9 tables_used: list[str] = Field(description="List of tables referenced")
10
11# Initialize model with structured output
12llm = ChatAnthropic(model="claude-sonnet-4-6")
13structured_llm = llm.with_structured_output(SQLQuery)
14
15# Create prompt template
16prompt = ChatPromptTemplate.from_messages([
17 ("system", """You are an expert SQL developer. Generate efficient, secure SQL queries.
18 Always use parameterized queries to prevent SQL injection.
19 Explain your reasoning briefly."""),
20 ("user", "Convert this to SQL: {query}")
21])
22
23# Create chain
24chain = prompt | structured_llm
25
26# Use
27result = await chain.ainvoke({
28 "query": "Find all users who registered in the last 30 days"
29})
30print(result.query)
31print(result.explanation)
Key Patterns
Pattern 1: Structured Output with Pydantic
python
1from anthropic import Anthropic
2from pydantic import BaseModel, Field
3from typing import Literal
4import json
5
6class SentimentAnalysis(BaseModel):
7 sentiment: Literal["positive", "negative", "neutral"]
8 confidence: float = Field(ge=0, le=1)
9 key_phrases: list[str]
10 reasoning: str
11
12async def analyze_sentiment(text: str) -> SentimentAnalysis:
13 """Analyze sentiment with structured output."""
14 client = Anthropic()
15
16 message = client.messages.create(
17 model="claude-sonnet-4-6",
18 max_tokens=500,
19 messages=[{
20 "role": "user",
21 "content": f"""Analyze the sentiment of this text.
22
23Text: {text}
24
25Respond with JSON matching this schema:
26{{
27 "sentiment": "positive" | "negative" | "neutral",
28 "confidence": 0.0-1.0,
29 "key_phrases": ["phrase1", "phrase2"],
30 "reasoning": "brief explanation"
31}}"""
32 }]
33 )
34
35 return SentimentAnalysis(**json.loads(message.content[0].text))
Pattern 2: Chain-of-Thought with Self-Verification
python
1from langchain_core.prompts import ChatPromptTemplate
2
3cot_prompt = ChatPromptTemplate.from_template("""
4Solve this problem step by step.
5
6Problem: {problem}
7
8Instructions:
91. Break down the problem into clear steps
102. Work through each step showing your reasoning
113. State your final answer
124. Verify your answer by checking it against the original problem
13
14Format your response as:
15## Steps
16[Your step-by-step reasoning]
17
18## Answer
19[Your final answer]
20
21## Verification
22[Check that your answer is correct]
23""")
Pattern 3: Few-Shot with Dynamic Example Selection
python
1from langchain_voyageai import VoyageAIEmbeddings
2from langchain_core.example_selectors import SemanticSimilarityExampleSelector
3from langchain_chroma import Chroma
4
5# Create example selector with semantic similarity
6example_selector = SemanticSimilarityExampleSelector.from_examples(
7 examples=[
8 {"input": "How do I reset my password?", "output": "Go to Settings > Security > Reset Password"},
9 {"input": "Where can I see my order history?", "output": "Navigate to Account > Orders"},
10 {"input": "How do I contact support?", "output": "Click Help > Contact Us or email support@example.com"},
11 ],
12 embeddings=VoyageAIEmbeddings(model="voyage-3-large"),
13 vectorstore_cls=Chroma,
14 k=2 # Select 2 most similar examples
15)
16
17async def get_few_shot_prompt(query: str) -> str:
18 """Build prompt with dynamically selected examples."""
19 examples = await example_selector.aselect_examples({"input": query})
20
21 examples_text = "\n".join(
22 f"User: {ex['input']}\nAssistant: {ex['output']}"
23 for ex in examples
24 )
25
26 return f"""You are a helpful customer support assistant.
27
28Here are some example interactions:
29{examples_text}
30
31Now respond to this query:
32User: {query}
33Assistant:"""
Pattern 4: Progressive Disclosure
Start with simple prompts, add complexity only when needed:
python
1PROMPT_LEVELS = {
2 # Level 1: Direct instruction
3 "simple": "Summarize this article: {text}",
4
5 # Level 2: Add constraints
6 "constrained": """Summarize this article in 3 bullet points, focusing on:
7- Key findings
8- Main conclusions
9- Practical implications
10
11Article: {text}""",
12
13 # Level 3: Add reasoning
14 "reasoning": """Read this article carefully.
151. First, identify the main topic and thesis
162. Then, extract the key supporting points
173. Finally, summarize in 3 bullet points
18
19Article: {text}
20
21Summary:""",
22
23 # Level 4: Add examples
24 "few_shot": """Read articles and provide concise summaries.
25
26Example:
27Article: "New research shows that regular exercise can reduce anxiety by up to 40%..."
28Summary:
29• Regular exercise reduces anxiety by up to 40%
30• 30 minutes of moderate activity 3x/week is sufficient
31• Benefits appear within 2 weeks of starting
32
33Now summarize this article:
34Article: {text}
35
36Summary:"""
37}
Pattern 5: Error Recovery and Fallback
python
1from pydantic import BaseModel, ValidationError
2import json
3
4class ResponseWithConfidence(BaseModel):
5 answer: str
6 confidence: float
7 sources: list[str]
8 alternative_interpretations: list[str] = []
9
10ERROR_RECOVERY_PROMPT = """
11Answer the question based on the context provided.
12
13Context: {context}
14Question: {question}
15
16Instructions:
171. If you can answer confidently (>0.8), provide a direct answer
182. If you're somewhat confident (0.5-0.8), provide your best answer with caveats
193. If you're uncertain (<0.5), explain what information is missing
204. Always provide alternative interpretations if the question is ambiguous
21
22Respond in JSON:
23{{
24 "answer": "your answer or 'I cannot determine this from the context'",
25 "confidence": 0.0-1.0,
26 "sources": ["relevant context excerpts"],
27 "alternative_interpretations": ["if question is ambiguous"]
28}}
29"""
30
31async def answer_with_fallback(
32 context: str,
33 question: str,
34 llm
35) -> ResponseWithConfidence:
36 """Answer with error recovery and fallback."""
37 prompt = ERROR_RECOVERY_PROMPT.format(context=context, question=question)
38
39 try:
40 response = await llm.ainvoke(prompt)
41 return ResponseWithConfidence(**json.loads(response.content))
42 except (json.JSONDecodeError, ValidationError) as e:
43 # Fallback: try to extract answer without structure
44 simple_prompt = f"Based on: {context}\n\nAnswer: {question}"
45 simple_response = await llm.ainvoke(simple_prompt)
46 return ResponseWithConfidence(
47 answer=simple_response.content,
48 confidence=0.5,
49 sources=["fallback extraction"],
50 alternative_interpretations=[]
51 )
Pattern 6: Role-Based System Prompts
python
1SYSTEM_PROMPTS = {
2 "analyst": """You are a senior data analyst with expertise in SQL, Python, and business intelligence.
3
4Your responsibilities:
5- Write efficient, well-documented queries
6- Explain your analysis methodology
7- Highlight key insights and recommendations
8- Flag any data quality concerns
9
10Communication style:
11- Be precise and technical when discussing methodology
12- Translate technical findings into business impact
13- Use clear visualizations when helpful""",
14
15 "assistant": """You are a helpful AI assistant focused on accuracy and clarity.
16
17Core principles:
18- Always cite sources when making factual claims
19- Acknowledge uncertainty rather than guessing
20- Ask clarifying questions when the request is ambiguous
21- Provide step-by-step explanations for complex topics
22
23Constraints:
24- Do not provide medical, legal, or financial advice
25- Redirect harmful requests appropriately
26- Protect user privacy""",
27
28 "code_reviewer": """You are a senior software engineer conducting code reviews.
29
30Review criteria:
31- Correctness: Does the code work as intended?
32- Security: Are there any vulnerabilities?
33- Performance: Are there efficiency concerns?
34- Maintainability: Is the code readable and well-structured?
35- Best practices: Does it follow language idioms?
36
37Output format:
381. Summary assessment (approve/request changes)
392. Critical issues (must fix)
403. Suggestions (nice to have)
414. Positive feedback (what's done well)"""
42}
Integration Patterns
With RAG Systems
python
1RAG_PROMPT = """You are a knowledgeable assistant that answers questions based on provided context.
2
3Context (retrieved from knowledge base):
4{context}
5
6Instructions:
71. Answer ONLY based on the provided context
82. If the context doesn't contain the answer, say "I don't have information about that in my knowledge base"
93. Cite specific passages using [1], [2] notation
104. If the question is ambiguous, ask for clarification
11
12Question: {question}
13
14Answer:"""
With Validation and Verification
python
1VALIDATED_PROMPT = """Complete the following task:
2
3Task: {task}
4
5After generating your response, verify it meets ALL these criteria:
6✓ Directly addresses the original request
7✓ Contains no factual errors
8✓ Is appropriately detailed (not too brief, not too verbose)
9✓ Uses proper formatting
10✓ Is safe and appropriate
11
12If verification fails on any criterion, revise before responding.
13
14Response:"""
Performance Optimization
Token Efficiency
python
1# Before: Verbose prompt (150+ tokens)
2verbose_prompt = """
3I would like you to please take the following text and provide me with a comprehensive
4summary of the main points. The summary should capture the key ideas and important details
5while being concise and easy to understand.
6"""
7
8# After: Concise prompt (30 tokens)
9concise_prompt = """Summarize the key points concisely:
10
11{text}
12
13Summary:"""
Caching Common Prefixes
python
1from anthropic import Anthropic
2
3client = Anthropic()
4
5# Use prompt caching for repeated system prompts
6response = client.messages.create(
7 model="claude-sonnet-4-6",
8 max_tokens=1000,
9 system=[
10 {
11 "type": "text",
12 "text": LONG_SYSTEM_PROMPT,
13 "cache_control": {"type": "ephemeral"}
14 }
15 ],
16 messages=[{"role": "user", "content": user_query}]
17)
Best Practices
- Be Specific: Vague prompts produce inconsistent results
- Show, Don't Tell: Examples are more effective than descriptions
- Use Structured Outputs: Enforce schemas with Pydantic for reliability
- Test Extensively: Evaluate on diverse, representative inputs
- Iterate Rapidly: Small changes can have large impacts
- Monitor Performance: Track metrics in production
- Version Control: Treat prompts as code with proper versioning
- Document Intent: Explain why prompts are structured as they are
Common Pitfalls
- Over-engineering: Starting with complex prompts before trying simple ones
- Example pollution: Using examples that don't match the target task
- Context overflow: Exceeding token limits with excessive examples
- Ambiguous instructions: Leaving room for multiple interpretations
- Ignoring edge cases: Not testing on unusual or boundary inputs
- No error handling: Assuming outputs will always be well-formed
- Hardcoded values: Not parameterizing prompts for reuse
Success Metrics
Track these KPIs for your prompts:
- Accuracy: Correctness of outputs
- Consistency: Reproducibility across similar inputs
- Latency: Response time (P50, P95, P99)
- Token Usage: Average tokens per request
- Success Rate: Percentage of valid, parseable outputs
- User Satisfaction: Ratings and feedback
Resources