5 · Applied & production AI

14. Prompt Engineering as a Discipline

Structure, few-shot examples, chain-of-thought, structured output and prompt injection.

9 min read · 3 MCQs

Structure beats cleverness

A reliable prompt states the role, the task, the constraints, the input and the exact output format — in that order. Ambiguity is the main cause of inconsistent responses; specify length, tone, schema and what to do when information is missing.

Few-shot and reasoning

Two or three worked examples pin down format far more effectively than adjectives. Asking the model to work step by step improves multi-step arithmetic and logic; for user-facing output, keep the reasoning internal and return only the conclusion.

Structured output and injection

Request JSON against a schema and validate it — retry on parse failure rather than trusting the string. Treat any text that came from a user, a web page or a document as untrusted data, never as instructions: prompt injection is the LLM equivalent of SQL injection.

{
  "role": "contract auditor",
  "task": "classify the finding",
  "output_schema": {
    "severity": "critical|high|medium|low",
    "summary": "string, max 200 chars",
    "confidence": "number 0-1"
  },
  "on_missing_info": "return severity 'low' with confidence 0"
}

Chapter quiz

3 questions · pass mark 75%
  1. 1. Few-shot examples are most useful for…

  2. 2. Prompt injection is best defended by…

  3. 3. When you need machine-readable output you should…

Answer every question to submit. Progress for ai-14 is saved in this browser.