Generative AI & LLM

Module 2 of 16

Module 02: Prompt Engineering Mastery

6 min read1,055 words
What you'll learn
Apply zero-shot, few-shot, and chain-of-thought prompting techniquesCraft effective system prompts that shape LLM behaviorUse structured output prompting to get reliable JSON/data from LLMsDesign prompt templates that are reusable and parameterizableIteratively refine prompts using a systematic methodology

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)

LayerWhat you’re doingWhen it shines
Zero-shotJust askClear tasks the model has seen a lot
Role + systemSet voice, rules, taboosAnything conversational or brand-sensitive
Few-shotShow input/output examplesFormatting, edge cases, taxonomy
Chain-of-thoughtAsk for steps before the answerMath, logic, policy reasoning
Agents / tools (later modules)Let the model call functionsActions, 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 fitsWeak fits
Simple classification, rewrite, translateWeird schemas, rare labels, strict JSON
Brainstorming with loose shapeAnything 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.

TipWhy
3–5 diverse examplesCovers boundaries without overfitting one class
Show exact output shapeModels mimic surface form aggressively
Put hard cases last or in the middleOrder 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.

python
[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.”

VariantRecipe
Zero-shot CoTAppend “Let’s think step by step.”
Few-shot CoTProvide Question → Reasoning → Answer demos
When CoT helpsWhen it’s overkill
Multi-step arithmetic, logic puzzlesSimple 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.

PatternExample 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.

ApproachTradeoff
Prompt-only JSONFlexible; validate with a schema in code
API JSON modeFewer 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.

python
[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:

PracticeBenefit
Named templates (summarize, classify)Easier reviews and diffs
Default params (length, tone)Consistent behavior
Central registryVersioning + 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)

StepYou do
1Write 5–20 golden inputs with expected shapes
2Run prompt v1; log failures by type (format, fact, tone)
3Patch one failure class per iteration
4Re-run suite; watch regressions
Failure typeTypical fix
Wrong formatStronger examples + schema echo
Skips edge casesAdd contradictory few-shots
Too chattySystem 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

PatternPurposeMini-template
PersonaAnchor expertise“You are a {role} who {constraint}…”
Output contractKill ambiguity“Return markdown table with columns …”
Step-by-stepUnlock reasoning“First … Then … Finally …”
ConstraintsBoundaries“Max 120 words; no emojis; cite sources if given.”
Self-critiqueQuality 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

ExerciseGoal
Prompt battleSame task: zero- vs few-shot vs CoT; score on a rubric
System prompt trioLegal advisor, nutritionist, travel planner—same user turn
Robustness set10 sarcastic inputs for sentiment; iterate
Meta-promptModel 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

← Previous: What Are LLMs? | Next: Working with LLM APIs →