4. Prompt Engineering
The art and science of crafting inputs (prompts) that guide LLMs to understand the task, context, and constraints — producing better, more reliable outputs.

Table of Contents
- What is Prompt Engineering?
- Anatomy of a Great Prompt
- Core Prompting Techniques
- Advanced Techniques
- System Prompts
- Common Mistakes to Avoid
- Prompt Templates
- Key Takeaway
What is Prompt Engineering?
Prompt engineering is the practice of designing and refining the instructions you give to a Large Language Model to elicit the best possible output. It is part craft, part science — a well-engineered prompt can be the difference between a generic, inaccurate response and a precise, expert-level answer.
Poor prompt: "Summarise this."
→ Vague, unpredictable length and style
Good prompt: "You are a senior technical writer. Summarise the following
document in exactly 3 bullet points. Each bullet should be
under 20 words and highlight a key benefit for developers.
Document: {document}"
→ Consistent, targeted, useful output
Anatomy of a Great Prompt
Every well-structured prompt contains some or all of these components:
┌──────────────────────────────────────────────────────────┐
│ ROLE / PERSONA │
│ "You are an expert Python developer..." │
├──────────────────────────────────────────────────────────┤
│ TASK / INSTRUCTION │
│ "Review the following code and identify bugs..." │
├──────────────────────────────────────────────────────────┤
│ CONTEXT │
│ "This is a FastAPI service handling payment webhooks." │
├──────────────────────────────────────────────────────────┤
│ INPUT / DATA │
│ "Code: {code_snippet}" │
├──────────────────────────────────────────────────────────┤
│ EXAMPLES (few-shot) │
│ "Example bug: Missing null check → Example fix: ..." │
├──────────────────────────────────────────────────────────┤
│ OUTPUT FORMAT │
│ "Return a JSON array: [{line, severity, description}]" │
└──────────────────────────────────────────────────────────┘
Core Prompting Techniques
1. Zero-Shot Prompting
Ask the model to perform a task with no examples. Works for simple, well-known tasks.
Classify the sentiment of this review: "The product broke after 2 days."
Answer: Negative
2. Few-Shot Prompting
Provide examples to show the model the expected input→output pattern. Dramatically improves accuracy for novel or nuanced tasks.
Classify sentiment:
Review: "Absolutely love it!" → Positive
Review: "It's okay, nothing special." → Neutral
Review: "Completely useless, waste of money." → Negative
Review: "Arrived on time, works as expected." →
3. Chain-of-Thought (CoT) Prompting
Instruct the model to reason step-by-step before giving the final answer. Significantly improves performance on reasoning and maths tasks.
Q: A store has 120 items. 30% are electronics.
If 25% of electronics are on sale, how many electronics are on sale?
Think step by step:
1. Electronics = 30% × 120 = 36 items
2. On sale = 25% × 36 = 9 items
Answer: 9 electronics are on sale.
4. Role / Persona Prompting
Assign a role to the model to activate domain expertise and appropriate tone.
You are a senior DevSecOps engineer with 15 years of experience.
Review this Dockerfile for security vulnerabilities and explain each risk in plain English.
5. Self-Consistency
Generate multiple reasoning chains with temperature > 0, then take a majority vote on the final answer. Improves reliability on complex tasks.
Advanced Techniques
ReAct (Reasoning + Acting)
Interleave reasoning steps with tool calls. The model thinks, then acts, then observes.
Thought: I need to find the current weather in London.
Action: search("current weather London")
Observation: 18°C, partly cloudy
Thought: I now have the weather. I can answer.
Answer: The weather in London is currently 18°C and partly cloudy.
Tree of Thoughts (ToT)
Explore multiple reasoning paths simultaneously and evaluate which branch is most promising — like a search tree over thought space.
Prompt Chaining
Break complex tasks into a sequence of simpler prompts, where the output of one becomes the input of the next.
Prompt 1: Extract key facts from this document → {facts}
Prompt 2: Based on these facts: {facts}, write an executive summary
Prompt 3: Translate the summary into formal French
Meta-Prompting
Ask the LLM to generate or improve the prompt itself.
I need to write a prompt that will get an LLM to generate unit tests for Python code.
The prompt should cover: test naming conventions, edge cases, mocking dependencies.
Write an optimal prompt for this task.
System Prompts
The system prompt sets persistent context, persona, and rules for the entire conversation. It is separate from the user message and given higher authority.
messages = [
{
"role": "system",
"content": """You are a helpful customer support agent for Acme Corp.
Rules:
- Only answer questions about Acme Corp products.
- If asked about competitors, politely decline.
- Always be friendly and concise.
- If you don't know something, say so — never guess.
- Format lists as bullet points."""
},
{
"role": "user",
"content": "What's your return policy?"
}
]
Best practices for system prompts:
- Put the most important constraints first
- Use numbered lists for ordered rules
- Be explicit — don't assume the model will infer intent
- Test edge cases to ensure rules hold
Common Mistakes to Avoid
| ❌ Mistake | ✅ Fix |
|---|---|
| Vague instructions: "Write something good" | Specific: "Write a 200-word product description for a wireless keyboard, highlighting battery life and quiet keys" |
| No output format specified | Add: "Return as JSON: {title, description, price}" |
| Overloading one prompt | Break into a chain of focused prompts |
| Ignoring model's training cutoff | State the date and provide fresh context |
| No persona/tone direction | Specify: "Write in a professional but friendly tone" |
| Prompt injection risk | Sanitise user input before inserting into prompt templates |
Prompt Templates
Classification Template
You are an expert classifier. Classify the following {input_type} into one of these categories: {categories}.
Rules:
- Return only the category name, nothing else.
- If no category fits, return "Other".
{input_type}: {input}
Category:
Summarisation Template
You are a professional summariser. Summarise the following text in {format}.
Requirements:
- Length: {length}
- Audience: {audience}
- Tone: {tone}
Text:
{text}
Summary:
Code Review Template
You are a senior {language} developer. Review the following code for:
1. Bugs and potential errors
2. Security vulnerabilities
3. Performance issues
4. Code style and readability
For each issue found, provide:
- Line number (if applicable)
- Severity: Critical / Major / Minor
- Description of the issue
- Suggested fix
Code:
```{language}
{code}
---
## Key Takeaway
> **Prompt Engineering** is the skill of communicating effectively with LLMs. Clear roles, structured instructions, relevant context, and well-specified output formats consistently produce better, more reliable results — without any model retraining.
---
**← [RAG](../03-rag/README.md)** | **[Back to Overview](../README.md)** | **[Semantic Cache →](../05-semantic-cache/README.md)**