1.03 – Prompt Engineering | AI Course

✍️ 1.03 – Prompt Engineering

📑 On this page

🎯 What is Prompt Engineering?

📌 Definition

Prompt engineering is the practice of designing and optimizing inputs to AI models to get desired outputs. It's like learning to "speak AI" – crafting instructions that produce predictable, high-quality results.

💡 Why it matters

A well-crafted prompt can be the difference between getting a vague, useless answer and a specific, actionable response. This is possibly the most important skill for working with LLMs today.

🧩 Anatomy of a Prompt

Template:
[ROLE]     You are an expert [domain]...
[CONTEXT]  I am working on [project]...
[TASK]     Please [specific request]...
[CONSTRAINTS] Use [format]. Do not [restrictions].
[EXAMPLES]  Here are 2 examples: ...
📌 Component Breakdown

📐 Core Prompt Patterns

Pattern 1: Zero-shot

Simple instruction with no examples. Best for simple, well-defined tasks.

"Summarize this article in 3 bullet points."
Pattern 2: Few-shot

Provide 2-3 examples of the desired output format. Best for formatting, style transfer, or uncommon tasks.

EXAMPLE 1:
Input: "The weather is beautiful today."
Output: "POSITIVE"

EXAMPLE 2:
Input: "I'm so frustrated with this bug."
Output: "NEGATIVE"

Now classify: "This solution works perfectly!"
Pattern 3: Chain-of-Thought (CoT)

Encourage the AI to "think step by step" before answering. Best for reasoning, math, and logic problems.

"Think step by step. A bakery has 120 croissants. They sell 45 in the morning and 32 in the afternoon. How many are left?"
Pattern 4: Role Prompting

Assign a specific persona or expertise to the AI.

"You are an experienced DevOps engineer. Explain Kubernetes to a junior developer who knows Docker but has never used orchestration."
Pattern 5: Template Filling

Provide a template with placeholders for the AI to fill.

"Write a professional email with the following structure: SUBJECT: [topic] OPENING: [greeting] BODY: [key points 1-3] CLOSING: [call to action]"
Pattern 6: Constraints & Formatting

Specify exact output format, length, or style restrictions.

"List 5 pros and 5 cons of remote work. Format as JSON. Each pro/con should be a string. Keep responses under 50 words each."

🚀 Advanced Techniques

TechniqueDescriptionExample
Self-Consistency Run the same prompt multiple times, take majority answer Best for arithmetic, factual QA
Tree of Thoughts (ToT) Explore multiple reasoning paths, evaluate each Best for complex planning, creative writing
ReAct (Reason + Act) Interleave reasoning with tool use (search, calculator) Best for tasks requiring external information
Automatic Prompt Engineering (APE) Let the AI generate and evaluate its own prompts Best for optimizing prompts for specific tasks

📊 Before vs. After: Prompt Examples

Vague/Bad PromptSpecific/Good Prompt
"Tell me about Python" "You are a Python expert. I know basic syntax (loops, functions) but have never used classes or decorators. Explain object-oriented programming in Python with a simple BankAccount example."
"Write code for a website" "Write HTML/CSS/JS for a responsive to-do list app. Requirements: add tasks, delete tasks, mark complete. Use local storage. Provide the complete code in one file."
"Help me with my resume" "You are a tech recruiter. Review my resume for a software engineer position. Highlight missing keywords, formatting issues, and suggest 3 specific improvements."

✍️ Exercises

Exercise 1.03.1 – Identify the pattern

Which prompt pattern is being used in each example?

  1. "You are a nutritionist. Create a 7-day meal plan for a vegetarian athlete..."
  2. "Q: What is 37 * 41? Let's calculate step by step."
  3. "Here's an example: Input: happy → POSITIVE. Now classify: terrible →"
Answers:
1. Role Prompting
2. Chain-of-Thought (CoT)
3. Few-shot
Exercise 1.03.2 – Rewrite this prompt

Rewrite this vague prompt to be specific and effective:

"Explain how databases work."

Sample Answer:
"You are a database instructor. I'm a web developer who understands CRUD operations but has only used ORMs (Django, Rails). Explain relational database indexes: what they are, how they work (B-trees), when to use them, and common pitfalls. Include a simple SQL example comparing a query with and without an index. Keep explanations concrete and avoid vague language."
Exercise 1.03.3 – Write a prompt

Write a prompt that asks an LLM to generate a Python function that:

Sample Prompt:
"You are a Python expert. Write a function called `calculate_median` that:
1. Accepts a list of numbers (integers or floats)
2. Returns the median value (float)
3. Handles empty lists by raising a ValueError with a helpful message
4. Includes a complete docstring with examples
5. Uses type hints (List[float], float)
6. Does NOT use external libraries like statistics or numpy
Also include example usage with 3 test cases: odd-length list, even-length list, and empty list.
Provide only the code, no extra explanation."
📌 Best Practices Summary
📘 Next Module: 1.04 – AI Integration Patterns →