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
- Be specific — audience, length, tone, format (JSON, bullets).
- Separate roles — system holds rules; user holds the task.
- Show one good example — few-shot for tricky formats.
- Allow “I don’t know” — reduces invented facts (pairs with RAG).
- 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.”