Prompt Engineering Patterns
Master advanced prompt engineering techniques to maximize LLM performance, reliability, and controllability.
Do not use this skill when
The task is unrelated to prompt engineering patternsYou need a different domain or tool outside this scopeInstructions
Clarify goals, constraints, and required inputs.Apply relevant best practices and validate outcomes.Provide actionable steps and verification.If detailed examples are required, open resources/implementation-playbook.md.Use this skill when
Designing complex prompts for production LLM applicationsOptimizing prompt performance and consistencyImplementing structured reasoning patterns (chain-of-thought, tree-of-thought)Building few-shot learning systems with dynamic example selectionCreating reusable prompt templates with variable interpolationDebugging and refining prompts that produce inconsistent outputsImplementing system prompts for specialized AI assistantsCore Capabilities
1. Few-Shot Learning
Example selection strategies (semantic similarity, diversity sampling)Balancing example count with context window constraintsConstructing effective demonstrations with input-output pairsDynamic example retrieval from knowledge basesHandling edge cases through strategic example selection2. Chain-of-Thought Prompting
Step-by-step reasoning elicitationZero-shot CoT with "Let's think step by step"Few-shot CoT with reasoning tracesSelf-consistency techniques (sampling multiple reasoning paths)Verification and validation steps3. Prompt Optimization
Iterative refinement workflowsA/B testing prompt variationsMeasuring prompt performance metrics (accuracy, consistency, latency)Reducing token usage while maintaining qualityHandling edge cases and failure modes4. Template Systems
Variable interpolation and formattingConditional prompt sectionsMulti-turn conversation templatesRole-based prompt compositionModular prompt components5. System Prompt Design
Setting model behavior and constraintsDefining output formats and structureEstablishing role and expertiseSafety guidelines and content policiesContext setting and background informationQuick Start
from prompt_optimizer import PromptTemplate, FewShotSelectorDefine a structured prompt template
template = PromptTemplate(
system="You are an expert SQL developer. Generate efficient, secure SQL queries.",
instruction="Convert the following natural language query to SQL:\n{query}",
few_shot_examples=True,
output_format="SQL code block with explanatory comments"
)Configure few-shot learning
selector = FewShotSelector(
examples_db="sql_examples.jsonl",
selection_strategy="semantic_similarity",
max_examples=3
)Generate optimized prompt
prompt = template.render(
query="Find all users who registered in the last 30 days",
examples=selector.select(query="user registration date filter")
)
Key Patterns
Progressive Disclosure
Start with simple prompts, add complexity only when needed:
Level 1: Direct instruction - "Summarize this article"
Level 2: Add constraints - "Summarize this article in 3 bullet points, focusing on key findings"
Level 3: Add reasoning - "Read this article, identify the main findings, then summarize in 3 bullet points"
Level 4: Add examples - Include 2-3 example summaries with input-output pairs
Instruction Hierarchy
[System Context] → [Task Instruction] → [Examples] → [Input Data] → [Output Format]
Error Recovery
Build prompts that gracefully handle failures:
Include fallback instructionsRequest confidence scoresAsk for alternative interpretations when uncertainSpecify how to indicate missing informationBest Practices
Be Specific: Vague prompts produce inconsistent resultsShow, Don't Tell: Examples are more effective than descriptionsTest Extensively: Evaluate on diverse, representative inputsIterate Rapidly: Small changes can have large impactsMonitor Performance: Track metrics in productionVersion Control: Treat prompts as code with proper versioningDocument Intent: Explain why prompts are structured as they areCommon Pitfalls
Over-engineering: Starting with complex prompts before trying simple onesExample pollution: Using examples that don't match the target taskContext overflow: Exceeding token limits with excessive examplesAmbiguous instructions: Leaving room for multiple interpretationsIgnoring edge cases: Not testing on unusual or boundary inputsIntegration Patterns
With RAG Systems
# Combine retrieved context with prompt engineering
prompt = f"""Given the following context:
{retrieved_context}{few_shot_examples}
Question: {user_question}
Provide a detailed answer based solely on the context above. If the context doesn't contain enough information, explicitly state what's missing."""
With Validation
# Add self-verification step
prompt = f"""{main_task_prompt}After generating your response, verify it meets these criteria:
Answers the question directly
Uses only information from provided context
Cites specific sources
Acknowledges any uncertaintyIf verification fails, revise your response."""
Performance Optimization
Token Efficiency
Remove redundant words and phrasesUse abbreviations consistently after first definitionConsolidate similar instructionsMove stable content to system promptsLatency Reduction
Minimize prompt length without sacrificing qualityUse streaming for long-form outputsCache common prompt prefixesBatch similar requests when possibleResources
references/few-shot-learning.md: Deep dive on example selection and constructionreferences/chain-of-thought.md: Advanced reasoning elicitation techniquesreferences/prompt-optimization.md: Systematic refinement workflowsreferences/prompt-templates.md: Reusable template patternsreferences/system-prompts.md: System-level prompt designassets/prompt-template-library.md: Battle-tested prompt templatesassets/few-shot-examples.json: Curated example datasetsscripts/optimize-prompt.py: Automated prompt optimization toolSuccess Metrics
Track these KPIs for your prompts:
Accuracy: Correctness of outputsConsistency: Reproducibility across similar inputsLatency: Response time (P50, P95, P99)Token Usage: Average tokens per requestSuccess Rate: Percentage of valid outputsUser Satisfaction: Ratings and feedbackNext Steps
Review the prompt template library for common patternsExperiment with few-shot learning for your specific use caseImplement prompt versioning and A/B testingSet up automated evaluation pipelinesDocument your prompt engineering decisions and learnings