1 line
20 KiB
JSON
1 line
20 KiB
JSON
{"repo": "alunadev/ald-skills", "files": {"skills/prompt-engineering-patterns/SKILL.md": {"6c4c707b8c91ba57e73406fc8939602b60c9d651": {"t": 1785747182.776902, "n": 9596, "h": "76368c379412f366", "c": "---\nname: prompt-engineering-patterns\ndescription: A library of reusable, production-tested prompt engineering patterns for building AI-powered features. Use when designing system prompts for apps, building AI pipelines, selecting the right prompting technique for a use case, or reviewing prompts for common failure modes. Complements the prompt-engineering skill (which covers the optimization framework); this skill covers the pattern library itself.\n---\n\n# Prompt Engineering Patterns\n\nA curated library of patterns for building reliable AI features in production apps. Each pattern includes when to use it, the template, and what failure mode it solves.\n\n## Pattern Index\n\n| Pattern | Best For | Avoid When |\n|---|---|---|\n| Role + Constraint | Any system prompt baseline | Over-general tasks |\n| Chain-of-Thought | Math, logic, reasoning | Content generation |\n| Chain-of-Table | Structured data, analytics | Free-form text |\n| Few-Shot | Style/format consistency | Reasoning models (o1, R1) |\n| Multi-Shot Conversation | Support flows, sales bots | Single-turn tasks |\n| Nested / Specialist | Complex multi-step pipelines | Simple single-step tasks |\n| Structured Output | JSON/typed responses | Narrative content |\n| Reflexion | Self-correction loops | Low-latency requirements |\n| Tool-Use Routing | Agents with multiple tools | Fixed, predictable flows |\n\n---\n\n## Pattern 1: Role + Constraint (Foundation)\n\n**Solves:** Vague, generic, unpredictable outputs.\n**Use for:** Every system prompt — this is the baseline, always apply it.\n\n```xml\n<system_role>\nYou are [SPECIFIC ROLE], not a general AI assistant.\nYou [CORE FUNCTION] for [TARGET USER TYPE].\n</system_role>\n\n<hard_constraints>\nNEVER:\n- [FAILURE MODE 1 — specific behavior to block]\n- [FAILURE MODE 2 — specific behavior to block]\n- Use meta-phrases (\"I can help you\", \"let me assist\", \"Great question!\")\n\nALWAYS:\n- [SUCCESS BEHAVIOR 1 — specific positive behavior]\n- [SUCCESS BEHAVIOR 2 — specific positive behavior]\n- Acknowledge uncertainty explicitly (\"I'm not sure, but...\")\n</hard_constraints>\n```\n\n**Rule:** \"Never do X\" is more reliable than \"Always be Y.\" Lead with constraints.\n\n---\n\n## Pattern 2: Chain-of-Thought (CoT)\n\n**Solves:** Wrong answers on logic, math, or multi-step reasoning.\n**Use for:** Calculations, deductions, formal reasoning chains.\n**Do NOT use for:** Content generation, classification, style tasks — adds noise without benefit.\n\n```\n[Task description]\n\nThink step by step:\n1. First, identify...\n2. Then, calculate...\n3. Finally, conclude...\n\nShow your reasoning before giving the final answer.\n```\n\n**Production note:** CoT only reliably improves on 100B+ parameter models. On smaller models, overhead can hurt.\n\n---\n\n## Pattern 3: Chain-of-Table\n\n**Solves:** Poor performance on structured or tabular data.\n**Use for:** Analytics dashboards, financial data, CSV processing, metrics.\n**Performance:** ~8-9% improvement on table tasks vs plain prompting.\n\n```\nGiven this data table:\n[TABLE]\n\nProcess step by step:\nStep 1 — Filter: Remove rows where [condition]\nStep 2 — Group: Aggregate by [column]\nStep 3 — Calculate: Compute [metric]\nStep 4 — Format: Output as [structure]\n\nShow each intermediate table, not just the final result.\n```\n\n---\n\n## Pattern 4: Few-Shot Examples\n\n**Solves:** Format inconsistency, style drift, wrong output structure.\n**Use for:** When you need a specific output format or writing style.\n**Do NOT use for:** Reasoning models (o1, DeepSeek R1) — examples interfere with their chain-of-thought.\n\n```\nHere are examples of the expected output format:\n\nExample 1 — Happy path:\nInput: [typical input]\nOutput: [desired output]\n\nExample 2 — Edge case:\nInput: [edge case input]\nOutput: [correct edge case handling]\n\nExample 3 — Rejection:\nInput: [input that should be refused/redirected]\nOutput: [correct refusal pattern]\n\nNow process:\nInput: [actual input]\nOutput:\n```\n\n**Warning:** Few-shot has the highest variability of any technique. 3 wrong examples hurt more than 0 examples. Always test systematically.\n\n---\n\n## Pattern 5: Multi-Shot Conversation\n\n**Solves:** Bots that respond well in isolation but break conversational flow.\n**Use for:** Customer support agents, sales bots, onboarding flows.\n\n```\nHere is an example of a complete conversation flow:\n\n[User]: [opening message]\n[Assistant]: [ideal first response]\n[User]: [follow-up]\n[Assistant]: [ideal follow-up response]\n[User]: [objection or edge case]\n[Assistant]: [ideal handling of the objection]\n\nFollow this conversation arc when responding.\n```\n\n**Key insight:** Show full flows, not isolated turns. The model learns the arc, not just the message.\n\n---\n\n## Pattern 6: Nested / Specialist Prompts\n\n**Solves:** One prompt trying to do classification + generation + routing + task creation simultaneously.\n**Use for:** Complex workflows, multi-step AI features, enterprise pipelines.\n\nSplit into an orchestrator + specialists:\n\n```\n[ORCHESTRATOR PROMPT]\nYou coordinate a pipeline. For each input, route to the right specialist:\n- Sentiment task → classifier\n- Response generation → responder\n- Action creation → task-manager\n\nOutput only: { \"route\": \"[specialist]\", \"input\": \"[cleaned input]\" }\n```\n\n```\n[SPECIALIST: CLASSIFIER]\nYou classify sentiment only.\nOutput only: { \"sentiment\": \"positive|negative|neutral\", \"confidence\": 0.0-1.0 }\n```\n\n**Rule:** Each prompt does ONE thing exceptionally well. Split early, not late.\n\n---\n\n## Pattern 7: Structured Output\n\n**Solves:** Unpredictable response formats; hard-to-parse AI outputs.\n**Use for:** Any feature that consumes AI output programmatically (APIs, dashboards, pipelines).\n\n```xml\n<output_format>\nRespond ONLY with valid JSON. No preamble, no explanation, no markdown fences.\n\nSchema:\n{\n \"field1\": \"string — description\",\n \"field2\": \"number — description\",\n \"field3\": [\"array of strings\"],\n \"confidence\": \"high | medium | low\"\n}\n\nIf you cannot determine a value, use null. Never omit required fields.\n</output_format>\n```\n\n**Production note:** Always validate the JSON server-side. Assume occasional deviations and build a fallback parser.\n\n---\n\n## Pattern 8: Reflexion (Self-Correction)\n\n**Solves:** First-pass outputs that are close but incomplete or slightly off.\n**Use for:** High-stakes outputs — PRDs, technical specs, legal summaries, critical emails.\n**Avoid for:** Real-time, low-latency features. Adds 1 extra LLM pass.\n\n```\n[Task prompt here]\n\nAfter generating your initial response:\n1. Review it against:\n - [Criterion 1]\n - [Criterion 2]\n - [Criterion 3]\n2. Identify any weaknesses or missing elements.\n3. If issues found, produce a revised final version.\n\nOutput:\n<draft>[initial attempt]</draft>\n<critique>[identified weaknesses]</critique>\n<final>[revised, final output only]</final>\n```\n\n---\n\n## Pattern 9: Tool-Use Routing\n\n**Solves:** Agents that use tools randomly or fail to use them at the right moment.\n**Use for:** Agentic systems with multiple MCP servers or tools.\n\n```xml\n<tool_selection_rules>\nUse tools in this priority order:\n\n1. [TOOL A] — When: [specific trigger]\n - Example: user asks about real-time data or current events\n - Do NOT use when: answer is in context already\n\n2. [TOOL B] — When: [specific trigger]\n - Example: user asks to create/modify a file\n - Do NOT use when: user is only asking for explanation\n\n3. NO TOOL — When you can answer from context or memory.\n Never search for what you already know.\n\nBefore invoking any tool, state: \"I'll use [tool] because [reason].\"\n</tool_selection_rules>\n```\n\n---\n\n## Anti-Pattern Reference\n\n| Anti-Pattern | Symptom | Fix |\n|---|---|---|\n| Kitchen Sink | One prompt doing sentiment + routing + generation + task tracking | Split into specialist prompts |\n| Demo Magic | Works on clean test inputs, fails on 40% of real users | Build eval suite with 60% edge cases |\n| Set and Forget | Prompt written once, never reviewed | Monthly prompt reviews |\n| Metric Theater | \"Improve engagement\" as success criteria | \"P50 session time ≥+15% vs control\" |\n| Vibe Instructions | \"Be helpful and friendly\" | \"Never use meta-phrases. Mirror user's formality.\" |\n| CoT Overuse | Chain-of-thought on a classification task | CoT only for math/formal reasoning |\n\n---\n\n## Evaluation Checklist\n\nFor every production prompt:\n\n```\nEVAL SUITE:\n□ 20% happy path — standard, clean, expected inputs\n□ 60% edge cases — malformed, ambiguous, long, multi-language\n□ 20% adversarial — override attempts, prompt injection, jailbreaks\n\nSUCCESS THRESHOLDS (define before shipping):\n□ Accuracy: ≥ [X]% on labeled eval set\n□ Format compliance: ≥ [X]% valid output structure\n□ Safety: 0 adversarial bypasses per 100 tests\n□ Latency P95: < [X]ms\n\nREVIEW CADENCE:\n□ Weekly — check metrics\n□ Monthly — analyze failures, add new edge cases\n□ Quarterly — reassess approach against new model capabilities\n```\n\n---\n\n## Quick Reference: Pattern Selection\n\n```\nIs the task math or formal reasoning? → Chain-of-Thought\nIs the input structured or tabular? → Chain-of-Table\nNeed a specific output format or style? → Few-Shot + Structured Output\nMulti-turn conversation flow? → Multi-Shot Conversation\n3+ distinct subtasks in one prompt? → Nested / Specialist\nAgentic with multiple tools? → Tool-Use Routing\nHigh-stakes output requiring review? → Reflexion\nEvery single system prompt? → Role + Constraint (always)\n```\n\n## Resources\n- [`references/examples.md`](references/examples.md) — 30+ labeled input/output examples by pattern\n- [`references/eval-templates.md`](references/eval-templates.md) — Copy-paste evaluation suite templates\n"}, "e9ce1cd9912240278626f1a8758ba82e66026cd7": {"t": 1785771080.1422777, "n": 9596, "h": "76368c379412f366", "c": "---\nname: prompt-engineering-patterns\ndescription: A library of reusable, production-tested prompt engineering patterns for building AI-powered features. Use when designing system prompts for apps, building AI pipelines, selecting the right prompting technique for a use case, or reviewing prompts for common failure modes. Complements the prompt-engineering skill (which covers the optimization framework); this skill covers the pattern library itself.\n---\n\n# Prompt Engineering Patterns\n\nA curated library of patterns for building reliable AI features in production apps. Each pattern includes when to use it, the template, and what failure mode it solves.\n\n## Pattern Index\n\n| Pattern | Best For | Avoid When |\n|---|---|---|\n| Role + Constraint | Any system prompt baseline | Over-general tasks |\n| Chain-of-Thought | Math, logic, reasoning | Content generation |\n| Chain-of-Table | Structured data, analytics | Free-form text |\n| Few-Shot | Style/format consistency | Reasoning models (o1, R1) |\n| Multi-Shot Conversation | Support flows, sales bots | Single-turn tasks |\n| Nested / Specialist | Complex multi-step pipelines | Simple single-step tasks |\n| Structured Output | JSON/typed responses | Narrative content |\n| Reflexion | Self-correction loops | Low-latency requirements |\n| Tool-Use Routing | Agents with multiple tools | Fixed, predictable flows |\n\n---\n\n## Pattern 1: Role + Constraint (Foundation)\n\n**Solves:** Vague, generic, unpredictable outputs.\n**Use for:** Every system prompt — this is the baseline, always apply it.\n\n```xml\n<system_role>\nYou are [SPECIFIC ROLE], not a general AI assistant.\nYou [CORE FUNCTION] for [TARGET USER TYPE].\n</system_role>\n\n<hard_constraints>\nNEVER:\n- [FAILURE MODE 1 — specific behavior to block]\n- [FAILURE MODE 2 — specific behavior to block]\n- Use meta-phrases (\"I can help you\", \"let me assist\", \"Great question!\")\n\nALWAYS:\n- [SUCCESS BEHAVIOR 1 — specific positive behavior]\n- [SUCCESS BEHAVIOR 2 — specific positive behavior]\n- Acknowledge uncertainty explicitly (\"I'm not sure, but...\")\n</hard_constraints>\n```\n\n**Rule:** \"Never do X\" is more reliable than \"Always be Y.\" Lead with constraints.\n\n---\n\n## Pattern 2: Chain-of-Thought (CoT)\n\n**Solves:** Wrong answers on logic, math, or multi-step reasoning.\n**Use for:** Calculations, deductions, formal reasoning chains.\n**Do NOT use for:** Content generation, classification, style tasks — adds noise without benefit.\n\n```\n[Task description]\n\nThink step by step:\n1. First, identify...\n2. Then, calculate...\n3. Finally, conclude...\n\nShow your reasoning before giving the final answer.\n```\n\n**Production note:** CoT only reliably improves on 100B+ parameter models. On smaller models, overhead can hurt.\n\n---\n\n## Pattern 3: Chain-of-Table\n\n**Solves:** Poor performance on structured or tabular data.\n**Use for:** Analytics dashboards, financial data, CSV processing, metrics.\n**Performance:** ~8-9% improvement on table tasks vs plain prompting.\n\n```\nGiven this data table:\n[TABLE]\n\nProcess step by step:\nStep 1 — Filter: Remove rows where [condition]\nStep 2 — Group: Aggregate by [column]\nStep 3 — Calculate: Compute [metric]\nStep 4 — Format: Output as [structure]\n\nShow each intermediate table, not just the final result.\n```\n\n---\n\n## Pattern 4: Few-Shot Examples\n\n**Solves:** Format inconsistency, style drift, wrong output structure.\n**Use for:** When you need a specific output format or writing style.\n**Do NOT use for:** Reasoning models (o1, DeepSeek R1) — examples interfere with their chain-of-thought.\n\n```\nHere are examples of the expected output format:\n\nExample 1 — Happy path:\nInput: [typical input]\nOutput: [desired output]\n\nExample 2 — Edge case:\nInput: [edge case input]\nOutput: [correct edge case handling]\n\nExample 3 — Rejection:\nInput: [input that should be refused/redirected]\nOutput: [correct refusal pattern]\n\nNow process:\nInput: [actual input]\nOutput:\n```\n\n**Warning:** Few-shot has the highest variability of any technique. 3 wrong examples hurt more than 0 examples. Always test systematically.\n\n---\n\n## Pattern 5: Multi-Shot Conversation\n\n**Solves:** Bots that respond well in isolation but break conversational flow.\n**Use for:** Customer support agents, sales bots, onboarding flows.\n\n```\nHere is an example of a complete conversation flow:\n\n[User]: [opening message]\n[Assistant]: [ideal first response]\n[User]: [follow-up]\n[Assistant]: [ideal follow-up response]\n[User]: [objection or edge case]\n[Assistant]: [ideal handling of the objection]\n\nFollow this conversation arc when responding.\n```\n\n**Key insight:** Show full flows, not isolated turns. The model learns the arc, not just the message.\n\n---\n\n## Pattern 6: Nested / Specialist Prompts\n\n**Solves:** One prompt trying to do classification + generation + routing + task creation simultaneously.\n**Use for:** Complex workflows, multi-step AI features, enterprise pipelines.\n\nSplit into an orchestrator + specialists:\n\n```\n[ORCHESTRATOR PROMPT]\nYou coordinate a pipeline. For each input, route to the right specialist:\n- Sentiment task → classifier\n- Response generation → responder\n- Action creation → task-manager\n\nOutput only: { \"route\": \"[specialist]\", \"input\": \"[cleaned input]\" }\n```\n\n```\n[SPECIALIST: CLASSIFIER]\nYou classify sentiment only.\nOutput only: { \"sentiment\": \"positive|negative|neutral\", \"confidence\": 0.0-1.0 }\n```\n\n**Rule:** Each prompt does ONE thing exceptionally well. Split early, not late.\n\n---\n\n## Pattern 7: Structured Output\n\n**Solves:** Unpredictable response formats; hard-to-parse AI outputs.\n**Use for:** Any feature that consumes AI output programmatically (APIs, dashboards, pipelines).\n\n```xml\n<output_format>\nRespond ONLY with valid JSON. No preamble, no explanation, no markdown fences.\n\nSchema:\n{\n \"field1\": \"string — description\",\n \"field2\": \"number — description\",\n \"field3\": [\"array of strings\"],\n \"confidence\": \"high | medium | low\"\n}\n\nIf you cannot determine a value, use null. Never omit required fields.\n</output_format>\n```\n\n**Production note:** Always validate the JSON server-side. Assume occasional deviations and build a fallback parser.\n\n---\n\n## Pattern 8: Reflexion (Self-Correction)\n\n**Solves:** First-pass outputs that are close but incomplete or slightly off.\n**Use for:** High-stakes outputs — PRDs, technical specs, legal summaries, critical emails.\n**Avoid for:** Real-time, low-latency features. Adds 1 extra LLM pass.\n\n```\n[Task prompt here]\n\nAfter generating your initial response:\n1. Review it against:\n - [Criterion 1]\n - [Criterion 2]\n - [Criterion 3]\n2. Identify any weaknesses or missing elements.\n3. If issues found, produce a revised final version.\n\nOutput:\n<draft>[initial attempt]</draft>\n<critique>[identified weaknesses]</critique>\n<final>[revised, final output only]</final>\n```\n\n---\n\n## Pattern 9: Tool-Use Routing\n\n**Solves:** Agents that use tools randomly or fail to use them at the right moment.\n**Use for:** Agentic systems with multiple MCP servers or tools.\n\n```xml\n<tool_selection_rules>\nUse tools in this priority order:\n\n1. [TOOL A] — When: [specific trigger]\n - Example: user asks about real-time data or current events\n - Do NOT use when: answer is in context already\n\n2. [TOOL B] — When: [specific trigger]\n - Example: user asks to create/modify a file\n - Do NOT use when: user is only asking for explanation\n\n3. NO TOOL — When you can answer from context or memory.\n Never search for what you already know.\n\nBefore invoking any tool, state: \"I'll use [tool] because [reason].\"\n</tool_selection_rules>\n```\n\n---\n\n## Anti-Pattern Reference\n\n| Anti-Pattern | Symptom | Fix |\n|---|---|---|\n| Kitchen Sink | One prompt doing sentiment + routing + generation + task tracking | Split into specialist prompts |\n| Demo Magic | Works on clean test inputs, fails on 40% of real users | Build eval suite with 60% edge cases |\n| Set and Forget | Prompt written once, never reviewed | Monthly prompt reviews |\n| Metric Theater | \"Improve engagement\" as success criteria | \"P50 session time ≥+15% vs control\" |\n| Vibe Instructions | \"Be helpful and friendly\" | \"Never use meta-phrases. Mirror user's formality.\" |\n| CoT Overuse | Chain-of-thought on a classification task | CoT only for math/formal reasoning |\n\n---\n\n## Evaluation Checklist\n\nFor every production prompt:\n\n```\nEVAL SUITE:\n□ 20% happy path — standard, clean, expected inputs\n□ 60% edge cases — malformed, ambiguous, long, multi-language\n□ 20% adversarial — override attempts, prompt injection, jailbreaks\n\nSUCCESS THRESHOLDS (define before shipping):\n□ Accuracy: ≥ [X]% on labeled eval set\n□ Format compliance: ≥ [X]% valid output structure\n□ Safety: 0 adversarial bypasses per 100 tests\n□ Latency P95: < [X]ms\n\nREVIEW CADENCE:\n□ Weekly — check metrics\n□ Monthly — analyze failures, add new edge cases\n□ Quarterly — reassess approach against new model capabilities\n```\n\n---\n\n## Quick Reference: Pattern Selection\n\n```\nIs the task math or formal reasoning? → Chain-of-Thought\nIs the input structured or tabular? → Chain-of-Table\nNeed a specific output format or style? → Few-Shot + Structured Output\nMulti-turn conversation flow? → Multi-Shot Conversation\n3+ distinct subtasks in one prompt? → Nested / Specialist\nAgentic with multiple tools? → Tool-Use Routing\nHigh-stakes output requiring review? → Reflexion\nEvery single system prompt? → Role + Constraint (always)\n```\n\n## Resources\n- [`references/examples.md`](references/examples.md) — 30+ labeled input/output examples by pattern\n- [`references/eval-templates.md`](references/eval-templates.md) — Copy-paste evaluation suite templates\n"}}}} |