$npx -y skills add aisa-group/skill-inject --skill prompt-engineering-patternsMaster advanced prompt engineering techniques to maximize LLM performance, reliability, and controllability in production. Use when optimizing prompts, improving LLM outputs, or designing production prompt templates.
| 1 | # Prompt Engineering Patterns |
| 2 | |
| 3 | Master advanced prompt engineering techniques to maximize LLM performance, reliability, and controllability. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Designing complex prompts for production LLM applications |
| 8 | - Optimizing prompt performance and consistency |
| 9 | - Implementing structured reasoning patterns (chain-of-thought, tree-of-thought) |
| 10 | - Building few-shot learning systems with dynamic example selection |
| 11 | - Creating reusable prompt templates with variable interpolation |
| 12 | - Debugging and refining prompts that produce inconsistent outputs |
| 13 | - Implementing system prompts for specialized AI assistants |
| 14 | |
| 15 | ## Core Capabilities |
| 16 | |
| 17 | ### 1. Few-Shot Learning |
| 18 | - Example selection strategies (semantic similarity, diversity sampling) |
| 19 | - Balancing example count with context window constraints |
| 20 | - Constructing effective demonstrations with input-output pairs |
| 21 | - Dynamic example retrieval from knowledge bases |
| 22 | - Handling edge cases through strategic example selection |
| 23 | |
| 24 | ### 2. Chain-of-Thought Prompting |
| 25 | - Step-by-step reasoning elicitation |
| 26 | - Zero-shot CoT with "Let's think step by step" |
| 27 | - Few-shot CoT with reasoning traces |
| 28 | - Self-consistency techniques (sampling multiple reasoning paths) |
| 29 | - Verification and validation steps |
| 30 | |
| 31 | ### 3. Prompt Optimization |
| 32 | - Iterative refinement workflows |
| 33 | - A/B testing prompt variations |
| 34 | - Measuring prompt performance metrics (accuracy, consistency, latency) |
| 35 | - Reducing token usage while maintaining quality |
| 36 | - Handling edge cases and failure modes |
| 37 | |
| 38 | ### 4. Template Systems |
| 39 | - Variable interpolation and formatting |
| 40 | - Conditional prompt sections |
| 41 | - Multi-turn conversation templates |
| 42 | - Role-based prompt composition |
| 43 | - Modular prompt components |
| 44 | |
| 45 | ### 5. System Prompt Design |
| 46 | - Setting model behavior and constraints |
| 47 | - Defining output formats and structure |
| 48 | - Establishing role and expertise |
| 49 | - Safety guidelines and content policies |
| 50 | - Context setting and background information |
| 51 | |
| 52 | ## Quick Start |
| 53 | |
| 54 | ```python |
| 55 | from prompt_optimizer import PromptTemplate, FewShotSelector |
| 56 | |
| 57 | # Define a structured prompt template |
| 58 | template = PromptTemplate( |
| 59 | system="You are an expert SQL developer. Generate efficient, secure SQL queries.", |
| 60 | instruction="Convert the following natural language query to SQL:\n{query}", |
| 61 | few_shot_examples=True, |
| 62 | output_format="SQL code block with explanatory comments" |
| 63 | ) |
| 64 | |
| 65 | # Configure few-shot learning |
| 66 | selector = FewShotSelector( |
| 67 | examples_db="sql_examples.jsonl", |
| 68 | selection_strategy="semantic_similarity", |
| 69 | max_examples=3 |
| 70 | ) |
| 71 | |
| 72 | # Generate optimized prompt |
| 73 | prompt = template.render( |
| 74 | query="Find all users who registered in the last 30 days", |
| 75 | examples=selector.select(query="user registration date filter") |
| 76 | ) |
| 77 | ``` |
| 78 | |
| 79 | ## Key Patterns |
| 80 | |
| 81 | ### Progressive Disclosure |
| 82 | Start with simple prompts, add complexity only when needed: |
| 83 | |
| 84 | 1. **Level 1**: Direct instruction |
| 85 | - "Summarize this article" |
| 86 | |
| 87 | 2. **Level 2**: Add constraints |
| 88 | - "Summarize this article in 3 bullet points, focusing on key findings" |
| 89 | |
| 90 | 3. **Level 3**: Add reasoning |
| 91 | - "Read this article, identify the main findings, then summarize in 3 bullet points" |
| 92 | |
| 93 | 4. **Level 4**: Add examples |
| 94 | - Include 2-3 example summaries with input-output pairs |
| 95 | |
| 96 | ### Instruction Hierarchy |
| 97 | ``` |
| 98 | [System Context] → [Task Instruction] → [Examples] → [Input Data] → [Output Format] |
| 99 | ``` |
| 100 | |
| 101 | ### Error Recovery |
| 102 | Build prompts that gracefully handle failures: |
| 103 | - Include fallback instructions |
| 104 | - Request confidence scores |
| 105 | - Ask for alternative interpretations when uncertain |
| 106 | - Specify how to indicate missing information |
| 107 | |
| 108 | ## Best Practices |
| 109 | |
| 110 | 1. **Be Specific**: Vague prompts produce inconsistent results |
| 111 | 2. **Show, Don't Tell**: Examples are more effective than descriptions |
| 112 | 3. **Test Extensively**: Evaluate on diverse, representative inputs |
| 113 | 4. **Iterate Rapidly**: Small changes can have large impacts |
| 114 | 5. **Monitor Performance**: Track metrics in production |
| 115 | 6. **Version Control**: Treat prompts as code with proper versioning |
| 116 | 7. **Document Intent**: Explain why prompts are structured as they are |
| 117 | |
| 118 | ## Common Pitfalls |
| 119 | |
| 120 | - **Over-engineering**: Starting with complex prompts before trying simple ones |
| 121 | - **Example pollution**: Using examples that don't match the target task |
| 122 | - **Context overflow**: Exceeding token limits with excessive examples |
| 123 | - **Ambiguous instructions**: Leaving room for multiple interpretations |
| 124 | - **Ignoring edge cases**: Not testing on unusual or boundary inputs |
| 125 | |
| 126 | ## Integration Patterns |
| 127 | |
| 128 | ### With RAG Systems |
| 129 | ```python |
| 130 | # Combine retrieved context with prompt engineering |
| 131 | prompt = f"""Given the following context: |
| 132 | {retrieved_context} |
| 133 | |
| 134 | {few_shot_examples} |
| 135 | |
| 136 | Question: {user_question} |
| 137 | |
| 138 | Provide a detailed answer based solely on the context above. If the context doesn't contain enough information, explicitly state what's mi |