Prompt Engineering Patterns That Actually Work
After shipping several LLM-powered features, these are the prompt patterns I reach for repeatedly.
1. Role + Constraints + Examples
The most reliable pattern:
You are a senior code reviewer at a fintech company.
Constraints:
- Focus on security issues and data handling
- Ignore style preferences
- Rate severity as: critical, high, medium, low
- Be specific about the line and the fix
Example:
Input: `password = request.form["password"]`
Output: {"line": 5, "severity": "critical", "issue": "Plain text password handling", "fix": "Hash with bcrypt before storing"}
Now review this code:
{code}
2. Chain of Thought
Force the model to reason step by step:
Analyze this error log and identify the root cause.
Think through this step by step:
1. What error occurred?
2. What was the system state?
3. What triggered it?
4. What is the root cause?
5. What is the fix?
Error log:
{log}
Adding “think step by step” consistently improves accuracy on reasoning tasks.
3. Few-Shot with Edge Cases
Do not just show happy path examples. Include tricky cases:
Extract the price from these product descriptions.
"Nike Air Max 90 - $129.99" -> 129.99
"Free shipping on orders over $50" -> null (not a product price)
"Was $200, now $149.99 (25% off)" -> 149.99 (current price)
"Price: contact us" -> null
"Bundle: 3 for $29.97 ($9.99 each)" -> 9.99 (unit price)
Now extract: "{input}"
The edge case examples prevent the most common failures.
4. Output Format Specification
Be explicit about the format you want:
Classify the support ticket.
Return ONLY a JSON object with these fields:
- category: one of ["billing", "technical", "account", "other"]
- priority: one of ["urgent", "normal", "low"]
- summary: one sentence summary (max 20 words)
Do not include any other text before or after the JSON.
Ticket: {ticket}
5. Negative Instructions
Tell the model what NOT to do:
Summarize this article in 3 bullet points.
Do NOT:
- Start with "This article discusses..."
- Include your own opinions
- Use more than 15 words per bullet
- Add a conclusion or recommendation
Negative instructions are surprisingly effective at preventing common model habits.
6. Validation Prompt
Use a second LLM call to check the first:
# First call: generate
response = llm("Write a SQL query for: {request}")
# Second call: validate
check = llm(f"""Is this SQL query correct for the request "{request}"?
Query: {response}
Check for:
1. Correct table and column names
2. Proper JOIN conditions
3. Correct WHERE clause logic
4. SQL injection vulnerabilities
Return: {{"valid": true/false, "issues": [...]}}""")
The cost of a second call is almost always worth it for production reliability.
Anti-Patterns
- “Be creative”: Too vague. Specify what creative means.
- Giant system prompts: Keep under 500 words. Move examples to few-shot.
- “Do your best”: The model always does its best. Tell it what good looks like.
- Assuming context: The model does not remember your last conversation.
The best prompt is the simplest one that consistently produces correct output.