Prompt Engineering Fundamentals
Why Prompting Matters
The same model can give a terrible answer or a brilliant one depending on how you ask. Prompt engineering is the skill of writing instructions that reliably get the output you want.
Bad prompt: "Fix this code"
Good prompt: "You are a senior Python developer. The function below raises a
KeyError when the input dict is missing the 'email' field.
Fix the bug and add defensive checks for all required fields.
Return only the corrected function."
The difference isn't magic. It's specificity.
The Core Techniques
1. Zero-Shot Prompting
What: Ask directly. No examples. The model relies entirely on its training.
Prompt: "Translate 'hello' to French."
Output: "Bonjour"
Prompt: "Classify this review as positive, negative, or neutral:
'The battery life is amazing but the screen is dim.'"
Output: "Mixed/Neutral"
When it works: Simple, well-defined tasks where the model already knows the format.
When it fails: Ambiguous tasks, unusual output formats, or domain-specific patterns.
2. One-Shot Prompting
What: Provide exactly 1 example, then ask.
Prompt:
"Convert product descriptions to JSON.
Example:
Input: Red Nike running shoes, size 10, $89.99
Output: {"product": "running shoes", "brand": "Nike", "color": "red", "size": "10", "price": 89.99}
Now convert:
Input: Blue Adidas soccer cleats, size 9, $120.00"
Output: {"product": "soccer cleats", "brand": "Adidas", "color": "blue", "size": "9", "price": 120.00}
Why it works: One example anchors the format. The model sees the pattern and replicates it.
3. Few-Shot Prompting
What: Provide 2-5 examples. The model learns the pattern from the examples, not from explicit rules.
Prompt:
"Classify support tickets by priority.
Ticket: 'Cannot log in to my account' → Priority: HIGH
Ticket: 'Logo looks blurry on mobile' → Priority: LOW
Ticket: 'Payment charged twice' → Priority: CRITICAL
Ticket: 'Can you add dark mode?' → Priority: LOW
Ticket: 'All customer data showing as blank' → Priority:"
Output: "CRITICAL"
Best practices:
- Use 3-5 examples for consistent results
- Cover edge cases in your examples
- Keep examples representative (not just easy ones)
- Order matters: put the hardest/most ambiguous examples last
When few-shot beats zero-shot:
| Scenario | Zero-shot | Few-shot |
|---|---|---|
| Standard translation | Works fine | Overkill |
| Custom JSON format | Inconsistent | Reliable |
| Domain classification | Guesses labels | Matches YOUR categories |
| Tone matching | Generic | Matches the style you show |
| Edge cases | Often wrong | Much better with examples |
4. Chain-of-Thought (CoT) Prompting
What: Ask the model to reason step by step before answering. This dramatically improves performance on reasoning, math, and logic tasks.
Before (without CoT)
Prompt: "If a store has 45 apples and sells 3/5 of them, then receives
a shipment of 20 more, how many apples does it have?"
Output: "38" (wrong — model rushed to answer)
After (with CoT)
Prompt: "If a store has 45 apples and sells 3/5 of them, then receives
a shipment of 20 more, how many apples does it have?
Think step by step."
Output: "Step 1: The store starts with 45 apples.
Step 2: It sells 3/5 of 45 = 27 apples.
Step 3: Remaining = 45 - 27 = 18 apples.
Step 4: New shipment adds 20: 18 + 20 = 38 apples.
Answer: 38"
Wait, that's actually 38. Let me use a trickier example:
Prompt: "A farmer has 17 sheep. All but 9 die. How many are left?"
Without CoT: "8" (wrong — subtracted)
With CoT: "All but 9 die means 9 survive. Answer: 9"
The magic phrase: Simply adding "Let's think step by step" or "Think through this carefully" can boost accuracy by 20-40% on reasoning tasks.
Role Prompting
What: Assign the model a persona. This activates relevant knowledge and adjusts tone.
Generic: "Review this SQL query."
With role: "You are a senior database administrator with 15 years of
PostgreSQL experience. Review this SQL query for performance
issues, missing indexes, and N+1 patterns."
Effective roles:
| Role | Effect |
|---|---|
| "Senior security engineer" | Finds vulnerabilities, thinks adversarially |
| "Experienced tech lead at a FAANG company" | Focuses on scalability, clean architecture |
| "Patient teacher explaining to a junior dev" | Simpler explanations, more context |
| "Devil's advocate" | Challenges assumptions, finds weaknesses |
| "Technical writer" | Clear, structured, jargon-free output |
Tip: Combine role with specific expertise. "You are a Python developer" is okay. "You are a senior Python developer specializing in async/await patterns and FastAPI" is better.
Delimiter Usage
Problem: When your prompt mixes instructions, context, and data, the model can confuse them.
Solution: Use delimiters to clearly separate sections.
XML Tags (recommended for Claude)
xml<instructions> Summarize the article below in 3 bullet points. Focus on technical details, not opinions. </instructions> <article> [Your 2000-word article here] </article> <output_format> - Bullet 1: ... - Bullet 2: ... - Bullet 3: ... </output_format>
Triple Backticks
Review the following code for security vulnerabilities:
```python
user_input = request.args.get('query')
result = db.execute(f"SELECT * FROM users WHERE name = '{user_input}'")
```
List each vulnerability and its fix.
Markdown Headers
# Context
You are reviewing a pull request for a payments microservice.
# Code to Review
[code here]
# Review Criteria
- Security
- Error handling
- Performance
Output Format Control
Tell the model exactly what format you want.
"Respond as a JSON object with keys: summary, severity, recommendation"
"Use a markdown table with columns: Issue | Severity | Fix"
"Return ONLY the corrected code, no explanation"
"Answer with exactly one word: YES or NO"
Before:
Prompt: "What are the pros and cons of microservices?"
Output: (3 paragraphs of prose)
After:
Prompt: "What are the pros and cons of microservices?
Respond as a markdown table with columns: Aspect | Pro | Con"
Output:
| Aspect | Pro | Con |
|--------|-----|-----|
| Deployment | Independent deploys | Complex orchestration |
| Scaling | Scale individual services | Network overhead |
| ... | ... | ... |
The Prompt Structure Formula
The most reliable prompt structure:
1. CONTEXT → Background the model needs
2. TASK → What you want it to do
3. CONSTRAINTS → Rules, limits, what NOT to do
4. EXAMPLES → Few-shot examples (if needed)
5. OUTPUT → Exact format for the response
Full Example
xml<context> You are reviewing a Python REST API built with FastAPI. The codebase follows PEP 8 and uses type hints throughout. </context> <task> Review the function below for bugs, security issues, and style violations. </task> <constraints> - Do NOT suggest rewriting the entire function - Focus on actionable issues only - Rate each issue as CRITICAL, HIGH, MEDIUM, or LOW </constraints> <code> def get_user(user_id): query = f"SELECT * FROM users WHERE id = {user_id}" result = db.execute(query) return result </code> <output_format> | Issue | Severity | Line | Suggestion | |-------|----------|------|------------| </output_format>
Common Prompting Mistakes
| Mistake | Example | Fix |
|---|---|---|
| Too vague | "Make this better" | "Improve readability by extracting helper functions and adding docstrings" |
| No format specified | "Analyze this data" | "Analyze this data and return a JSON with keys: trend, outliers, recommendation" |
| Too many constraints at once | 15 rules in one prompt | Start with 3-5 key rules, iterate |
| No examples for unusual tasks | "Classify using our custom categories" | Add 3-5 few-shot examples |
| Contradictory instructions | "Be concise. Explain everything in detail." | Pick one: brief summary OR detailed explanation |
| Burying the task | Context → context → more context → task at the end | Lead with the task, then provide context |
| Not testing edge cases | Works for the happy path | Test with malformed input, empty data, adversarial input |
Quick Reference: Which Technique When?
Simple factual question → Zero-shot
Custom output format → One-shot or Few-shot
Classification task → Few-shot (3-5 examples)
Math or reasoning → Chain-of-thought
Domain expertise needed → Role prompting
Complex multi-part task → Structure formula + delimiters
Consistency across calls → Few-shot + strict output format
Practical Exercise
Take any prompt you use regularly and apply this checklist:
- Does it have a clear task statement?
- Is there enough context?
- Are constraints explicit?
- Is the output format specified?
- Would an example help?
- Is the role/persona appropriate?
If you check 4+ boxes, your prompt is solid. If not, iterate.
Resources
- 🔗 Anthropic — Prompt Engineering Guide
- 🔗 OpenAI — Prompt Engineering Best Practices
- 📄 Chain-of-Thought Prompting (Wei et al., 2022)
- 🔗 Learn Prompting — Free Course
- 📄 Few-Shot Learners (Brown et al., 2020)
Previous: 06 - Fine-Tuning vs Prompting vs RAG | Next: 08 - Advanced Prompting Techniques