← Back to curriculum

Module 7 — GenAI & LLMs

Prompt engineering

System/user/assistant messages, clarity and constraints, few-shot examples, chain-of-thought when to use it, and output formats.

~75 min read + exercises

Prompt engineering

Before we begin

Prompt engineering = writing instructions so the same model gives useful, safe, on-format answers — without retraining.

Think of it as the UI for the model’s mind — clarity beats cleverness.


What you will learn

  • Structure system, user, and assistant messages.
  • Use constraints and output formats.
  • Apply few-shot examples responsibly.

Before this lesson


Principles that work

  1. Be specific — audience, length, tone, format (JSON, bullets).
  2. Separate roles — system holds rules; user holds the task.
  3. Show one good example — few-shot for tricky formats.
  4. Allow “I don’t know” — reduces invented facts (pairs with RAG).
  5. Iterate — treat prompts like code; version in git.

System prompt example (RAG-ready)

text
You answer questions using ONLY the provided context excerpts.
If the answer is not in the context, say "I don't know."
Cite sources as [1], [2] matching the excerpt numbers.
Be concise. No speculation.

Few-shot example

text
User: Classify sentiment — "Battery lasts all day"
Assistant: positive
 
User: Classify sentiment — "App crashes constantly"
Assistant: negative
 
User: Classify sentiment — "{actual review}"
Assistant:

Examples teach pattern; keep them short to save context window.


Chain-of-thought (when to use)

Asking the model to think step by step can help math and multi-step reasoning — but costs tokens and can expose messy reasoning.

For production: optional hidden scratchpad or tool use (Module 8). For learning: try on homework-style questions.


Anti-patterns

  • Vague “be helpful” with no constraints.
  • Stuffing irrelevant text → distracts retrieval.
  • Asking for JSON without saying “valid JSON only, no markdown.”

What's next

Lesson 3 — Temperature, top-k, top-p