Duration: 4 hours | Difficulty: Beginner–Intermediate | Prerequisites: Module 01
Learning Objectives
By the end of this module, you will be able to:
- Apply zero-shot, few-shot, and chain-of-thought prompting techniques
- Craft effective system prompts that shape LLM behavior
- Use structured output prompting to get reliable JSON/data from LLMs
- Design prompt templates that are reusable and parameterizable
- Iteratively refine prompts using a systematic methodology
1. The Art and Science of Prompting
Why Prompt Engineering Matters
A sharp prompt on a small model often beats a lazy prompt on a giant one. Think of the model as a very well-read friend who follows instructions literally: you get what you ask for, not what you meant.
The Prompting Hierarchy (Plain English)
| Layer | What you’re doing | When it shines |
|---|---|---|
| Zero-shot | Just ask | Clear tasks the model has seen a lot |
| Role + system | Set voice, rules, taboos | Anything conversational or brand-sensitive |
| Few-shot | Show input/output examples | Formatting, edge cases, taxonomy |
| Chain-of-thought | Ask for steps before the answer | Math, logic, policy reasoning |
| Agents / tools (later modules) | Let the model call functions | Actions, retrieval, calculators |
Fun Fact: Teams often A/B test prompts like product copy—same model, wildly different win rates.
Try This! Write the same request twice: once vague, once with audience, format, and failure mode (“If unsure, say I don’t know”). Compare outputs cold.
2. Zero-Shot Prompting
Zero-shot means no examples—only instructions. The model leans on pretraining plus your wording.
| Good fits | Weak fits |
|---|---|
| Simple classification, rewrite, translate | Weird schemas, rare labels, strict JSON |
| Brainstorming with loose shape | Anything needing identical formatting every time |
In code terms (conceptual): one messages=[{"role":"user","content":...}] call with a crisp instruction—no pasted exemplars.
Key Takeaway
If zero-shot works, stop there. Complexity is a maintenance tax.
3. Few-Shot Prompting
Few-shot means you paste tiny demonstrations—“when input looks like X, output looks like Y”—so the model locks onto a pattern.
| Tip | Why |
|---|---|
| 3–5 diverse examples | Covers boundaries without overfitting one class |
| Show exact output shape | Models mimic surface form aggressively |
| Put hard cases last or in the middle | Order nudges style (not magic, but real) |
Key Example: Intent routing with labeled examples—note the frozen labels and the final blank for the new message.
[object Object], openai ,[object Object], OpenAI
client = OpenAI(api_key=,[object Object],)
,[object Object], ,[object Object],(,[object Object],) -> ,[object Object],:
prompt = ,[object Object],
r = client.chat.completions.create(
model=,[object Object],,
messages=[{,[object Object],: ,[object Object],, ,[object Object],: prompt.,[object Object],(text=text)}],
temperature=,[object Object],,
max_tokens=,[object Object],,
)
,[object Object], r.choices[,[object Object],].message.content.strip()
,[object Object],(few_shot_classify(,[object Object],))Try This! Deliberately give two conflicting examples and watch the model wobble—that’s your signal to fix the teaching set.
4. Chain-of-Thought (CoT) Prompting
CoT asks the model to show reasoning before the final line. It’s the difference between “guess” and “show your work.”
| Variant | Recipe |
|---|---|
| Zero-shot CoT | Append “Let’s think step by step.” |
| Few-shot CoT | Provide Question → Reasoning → Answer demos |
| When CoT helps | When it’s overkill |
|---|---|
| Multi-step arithmetic, logic puzzles | Simple lookups where steps add verbosity only |
Fun Fact: CoT can increase length and cost—you’re buying accuracy, not free lunch.
Key Takeaway
CoT is a knob: turn it on when mistakes are expensive; turn it off when speed matters more.
5. System Prompts and Role Prompting
The system message is your standing brief: persona, safety posture, citation rules, output shape. User messages are the rolling conversation.
| Pattern | Example intent |
|---|---|
| Persona | “You are a senior reviewer…” |
| Constraints | “Never invent URLs; refuse if unknown.” |
| Format | “Always respond with bullets, max 5.” |
Try This! Maintain two system prompts—strict vs creative—and swap only the system line between otherwise identical user prompts.
6. Structured Output Prompting
Why it’s non-negotiable in prod
Free text is great for humans; machines want JSON, rows, enums. Make the schema explicit in the prompt and (when available) use API features like response_format={"type":"json_object"} or provider-native structured output.
| Approach | Tradeoff |
|---|---|
| Prompt-only JSON | Flexible; validate with a schema in code |
| API JSON mode | Fewer stray preambles |
| Pydantic / typed parse (OpenAI etc.) | Strongest guarantees when supported |
Key Example: Strict JSON extraction with a system-defined shape—good template for logs, CRM updates, or tool args.
[object Object], json
,[object Object], openai ,[object Object], OpenAI
client = OpenAI(api_key=,[object Object],)
,[object Object], ,[object Object],(,[object Object],) -> ,[object Object],:
schema_hint = ,[object Object],
r = client.chat.completions.create(
model=,[object Object],,
messages=[
{,[object Object],: ,[object Object],, ,[object Object],: ,[object Object],},
{,[object Object],: ,[object Object],, ,[object Object],: text},
],
temperature=,[object Object],,
response_format={,[object Object],: ,[object Object],},
)
,[object Object], json.loads(r.choices[,[object Object],].message.content)
sample = ,[object Object],
,[object Object],(json.dumps(extract_entities(sample), indent=,[object Object],))7. Prompt Templates and Parameterization
Production prompts are functions, not one-off strings:
| Practice | Benefit |
|---|---|
Named templates (summarize, classify) | Easier reviews and diffs |
| Default params (length, tone) | Consistent behavior |
| Central registry | Versioning + A/B tests |
Think “mail merge for AI”: {{customer_name}}, {{ticket_id}}, etc., filled in by your app right before the call.
Concept: Treat prompts like config—review them, version them, and never scatter magic strings across 40 files.
8. Iterative Refinement Techniques
The loop (no code required)
| Step | You do |
|---|---|
| 1 | Write 5–20 golden inputs with expected shapes |
| 2 | Run prompt v1; log failures by type (format, fact, tone) |
| 3 | Patch one failure class per iteration |
| 4 | Re-run suite; watch regressions |
| Failure type | Typical fix |
|---|---|
| Wrong format | Stronger examples + schema echo |
| Skips edge cases | Add contradictory few-shots |
| Too chatty | System line: “Answer only with …” |
Try This! Build a spreadsheet: input | expected tag | model output | failure tag. Color cells—patterns pop fast.
9. Common Prompt Patterns Catalog
| Pattern | Purpose | Mini-template |
|---|---|---|
| Persona | Anchor expertise | “You are a {role} who {constraint}…” |
| Output contract | Kill ambiguity | “Return markdown table with columns …” |
| Step-by-step | Unlock reasoning | “First … Then … Finally …” |
| Constraints | Boundaries | “Max 120 words; no emojis; cite sources if given.” |
| Self-critique | Quality pass | “List weaknesses, then revise.” |
Fun Fact: “You are an expert” alone does almost nothing without task structure—experts still need a spec.
Practice Exercises
| Exercise | Goal |
|---|---|
| Prompt battle | Same task: zero- vs few-shot vs CoT; score on a rubric |
| System prompt trio | Legal advisor, nutritionist, travel planner—same user turn |
| Robustness set | 10 sarcastic inputs for sentiment; iterate |
| Meta-prompt | Model writes a prompt pack for a new task—you edit |
Mini-Project: Prompt Engineering Toolkit
Sketch a PromptLab that stores named templates, runs A/B variants on a CSV of tests, and logs tokens + win rate. Start with functions, not frameworks.
Key Takeaways
Key Takeaway
- Start simple, add examples or steps only when metrics demand it.
- System prompts are standing policy; user prompts are the daily tickets.
- Structure beats vibes—show the shape you want.
- Test prompts like code: small suite, tight iteration loops.
- JSON / schemas turn LLMs into pipeline components instead of chat toys.
Resources for Further Learning
- OpenAI Prompt Engineering Guide
- Anthropic Prompt Engineering
- Prompt Engineering Guide
- Chain-of-Thought Paper
- DSPy