7 Prompt Engineering Patterns Every Developer Should Know
Master the most effective prompt patterns — from chain-of-thought to few-shot learning — and learn when to use each one for maximum results.
Prompt engineering isn't about magic words — it's about understanding how LLMs process instructions and structuring your inputs accordingly. Here are seven patterns that consistently produce better results.
1. Chain-of-Thought (CoT)
The most impactful pattern. Instead of asking for a direct answer, instruct the model to think step-by-step.
Without CoT: "What is 23 * 47?"
With CoT: "What is 23 * 47? Think through this step-by-step."When to Use
CoT is most effective for math, logic, reasoning, and multi-step problems. For simple factual recall, it adds unnecessary latency.
2. Few-Shot Examples
Provide 2-3 examples of the desired input/output format before your actual query:
Convert the following to JSON:
Input: "John is 30 years old and lives in NYC"
Output: {"name": "John", "age": 30, "city": "NYC"}
Input: "Sara, age 25, based in London"
Output: {"name": "Sara", "age": 25, "city": "London"}
Input: "Mike is 35, works from Tokyo"
Output:3. Role Assignment
Set a specific persona to prime the model's knowledge and tone:
You are a senior security engineer reviewing code for vulnerabilities.
Focus on OWASP Top 10 issues and provide severity ratings.
Review this code:
[code here]4. Output Structuring
Explicitly define the output format you expect:
Analyze this error log and respond in exactly this format:
**Root Cause:** [one sentence]
**Severity:** [Critical/High/Medium/Low]
**Fix:** [actionable steps]
**Prevention:** [how to avoid this in the future]5. Constraint Setting
Add explicit constraints to narrow the output:
Explain quantum computing in exactly 3 paragraphs.
- Use no jargon
- Include one real-world analogy
- End with a practical application6. Self-Consistency
Ask the model to generate multiple solutions and pick the best one:
Generate 3 different approaches to solve this problem.
Then evaluate each approach on: correctness, efficiency, readability.
Finally, recommend the best approach with your reasoning.7. Iterative Refinement
Break complex tasks into stages:
Step 1: Outline the main sections for a blog post about [topic]
Step 2: For each section, write 2-3 key points
Step 3: Expand each section into full paragraphs
Step 4: Review for technical accuracy and add code examplesCombining Patterns
The real power comes from combining patterns. Here's an example that uses role assignment + CoT + output structuring:
You are an expert Python developer specializing in performance optimization.
Given this function, identify performance bottlenecks step-by-step, then provide an optimized version.
Format your response as:
1. **Analysis:** [step-by-step reasoning about bottlenecks]
2. **Optimized Code:** [the improved function]
3. **Benchmarks:** [expected improvement with explanation]Key Takeaways
- Start simple — add complexity only when needed
- Test patterns against your specific model and use case
- Document what works for your team as a prompt library
- Remember that newer models often need less prompting — don't over-engineer