Overview
Designs, tests, and iterates high-quality LLM prompts. Covers prompt anatomy (role, context, instructions, examples, output format), few-shot example selection and ordering, chain-of-thought triggering, structured output (JSON mode, Pydantic, grammar), prompt injection defenses, systematic evaluation, and 10 reusable prompt patterns that have proven effective across many tasks.
When to Use This Skill
- Building or improving system prompts for agents, chatbots, or tools.
- The user wants reliable, structured output from an LLM.
- Debugging inconsistent or low-quality LLM responses.
- Creating few-shot examples or chain-of-thought prompts.
Prerequisites
- Access to one or more LLMs (via API or local).
- A clear task definition and success criteria.
- Evaluation data (even 10-20 examples is useful to start).
Steps
Define the task precisely:
- What is the input?
- What is the desired output (format, content, constraints)?
- What are the failure modes to avoid?
Prompt anatomy (use this structure):
- Role / persona.
- Context / background.
- Task instructions (clear, numbered, imperative).
- Constraints and rules.
- Few-shot examples (2-6 high-quality ones).
- Output format (JSON schema, bullet list, specific template).
- Reasoning encouragement ("Think step by step").
Few-shot best practices:
- Choose diverse, representative, high-quality examples.
- Order matters — put the most similar or most important first.
- Include both positive and "edge" examples when helpful.
Chain-of-Thought (CoT):
- "Let's think step by step."
- Or more specific: "First identify X, then Y, then produce Z."
- For complex reasoning, use "ReAct" style (Thought → Action → Observation).
Structured output:
- Use model
response_format: {type: "json_object"} (OpenAI) or equivalent.
- Provide a Pydantic model or JSON Schema in the prompt.
- Validate the output in code and retry with error feedback if invalid.
Defense against prompt injection:
- Clearly separate instructions from user content (e.g., XML tags, "User input starts here:").
- Instruct the model to ignore instructions inside user content.
- Validate and sanitize output.
Evaluation & iteration:
- Create a small golden set (10-30 examples) with expected outputs.
- Score outputs (exact match, fuzzy, LLM-as-judge).
- A/B test prompt variants.
- Track regression when changing models or prompts.
Output:
- The full system prompt + user template.
- 3-6 few-shot examples.
- Evaluation script or rubric.
- Notes on which model(s) it works best with.
- Version history of the prompt.
Examples
10 reusable prompt patterns (Classification, Extraction, Summarization, Reasoning, Tool Use, Critique, etc.) with full prompts and example inputs/outputs are included, plus a complete worked example for "Extract structured customer feedback from free-text reviews" with JSON schema, CoT, and evaluation.
Edge Cases & Error Handling
- Model refuses or hallucinates: Add explicit "If you cannot answer from the provided context, say 'I don't have enough information'."
- Output not parseable: Always validate + retry loop with the error message fed back to the model.
- Context length: Summarize or chunk long context; use RAG when appropriate.
Verification
- Run the prompt on the golden set — success rate meets the target (e.g., >90% on key fields).
- Test with adversarial or edge-case inputs — model behaves as specified.
- Structured output is always valid JSON that passes Pydantic validation.
- Changing the model (within the same family) still produces good results (or you have noted model-specific tuning).
- Success: The prompt is reliable enough to use in production or as part of an agent, with known limitations documented.
References
1---2name: prompt-engineer3description: Designs, tests, and iterates on LLM prompts for accuracy, reliability, and performance. Use when crafting system prompts, few-shot examples, chain-of-thought prompts, or output format instructions.4license: Apache-2.05compatibility: Claude Code, Codex, Gemini CLI6metadata:7 author: ai-skills8 version: "1.0"9 category: ai-ml10 tags: llm, prompt-engineering, few-shot, chain-of-thought, structured-output, evaluation11---1213## Overview1415Designs, tests, and iterates high-quality LLM prompts. Covers prompt anatomy (role, context, instructions, examples, output format), few-shot example selection and ordering, chain-of-thought triggering, structured output (JSON mode, Pydantic, grammar), prompt injection defenses, systematic evaluation, and 10 reusable prompt patterns that have proven effective across many tasks.1617## When to Use This Skill1819- Building or improving system prompts for agents, chatbots, or tools.20- The user wants reliable, structured output from an LLM.21- Debugging inconsistent or low-quality LLM responses.22- Creating few-shot examples or chain-of-thought prompts.2324## Prerequisites2526- Access to one or more LLMs (via API or local).27- A clear task definition and success criteria.28- Evaluation data (even 10-20 examples is useful to start).2930## Steps31321. **Define the task precisely**:33 - What is the input?34 - What is the desired output (format, content, constraints)?35 - What are the failure modes to avoid?36372. **Prompt anatomy** (use this structure):38 - Role / persona.39 - Context / background.40 - Task instructions (clear, numbered, imperative).41 - Constraints and rules.42 - Few-shot examples (2-6 high-quality ones).43 - Output format (JSON schema, bullet list, specific template).44 - Reasoning encouragement ("Think step by step").45463. **Few-shot best practices**:47 - Choose diverse, representative, high-quality examples.48 - Order matters — put the most similar or most important first.49 - Include both positive and "edge" examples when helpful.50514. **Chain-of-Thought (CoT)**:52 - "Let's think step by step."53 - Or more specific: "First identify X, then Y, then produce Z."54 - For complex reasoning, use "ReAct" style (Thought → Action → Observation).55565. **Structured output**:57 - Use model `response_format: {type: "json_object"}` (OpenAI) or equivalent.58 - Provide a Pydantic model or JSON Schema in the prompt.59 - Validate the output in code and retry with error feedback if invalid.60616. **Defense against prompt injection**:62 - Clearly separate instructions from user content (e.g., XML tags, "User input starts here:").63 - Instruct the model to ignore instructions inside user content.64 - Validate and sanitize output.65667. **Evaluation & iteration**:67 - Create a small golden set (10-30 examples) with expected outputs.68 - Score outputs (exact match, fuzzy, LLM-as-judge).69 - A/B test prompt variants.70 - Track regression when changing models or prompts.71728. **Output**:73 - The full system prompt + user template.74 - 3-6 few-shot examples.75 - Evaluation script or rubric.76 - Notes on which model(s) it works best with.77 - Version history of the prompt.7879## Examples808110 reusable prompt patterns (Classification, Extraction, Summarization, Reasoning, Tool Use, Critique, etc.) with full prompts and example inputs/outputs are included, plus a complete worked example for "Extract structured customer feedback from free-text reviews" with JSON schema, CoT, and evaluation.8283## Edge Cases & Error Handling8485- **Model refuses or hallucinates**: Add explicit "If you cannot answer from the provided context, say 'I don't have enough information'."86- **Output not parseable**: Always validate + retry loop with the error message fed back to the model.87- **Context length**: Summarize or chunk long context; use RAG when appropriate.8889## Verification90911. Run the prompt on the golden set — success rate meets the target (e.g., >90% on key fields).922. Test with adversarial or edge-case inputs — model behaves as specified.933. Structured output is always valid JSON that passes Pydantic validation.944. Changing the model (within the same family) still produces good results (or you have noted model-specific tuning).955. Success: The prompt is reliable enough to use in production or as part of an agent, with known limitations documented.9697## References9899- [OpenAI Prompt Engineering Guide](https://platform.openai.com/docs/guides/prompt-engineering)100- [Anthropic Prompt Engineering](https://docs.anthropic.com/claude/docs/prompt-engineering)101- [Prompt Engineering Guide (DAIR.AI)](https://www.promptingguide.ai/)102- [ReAct Paper](https://arxiv.org/abs/2210.03629)103- [JSON Mode & Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs)