Prompt engineering as an engineering discipline

Roles, structure, examples, reasoning, and above all prompts you can test and version.

Intermediate about 5 h

The picture

  1. 01

    Task + role

    what, for whom

  2. 02

    Context

    data in tags

  3. 03

    Examples

    few-shot

  4. 04

    Output spec

    format

  5. 05

    Test set

    regression

What actually moves quality

  1. Say the task, the audience and the success criterion. "Summarise" is vague; "summarise for a risk officer in three bullets, quoting figures exactly" is testable.
  2. Separate instructions from data using delimiters or XML-style tags, so the model never confuses a document's text with your instructions (this also helps against prompt injection).
  3. Show examples. Two or three diverse input→output pairs beat a paragraph of rules. Include a hard case.
  4. Specify the output format precisely, and for machine-readable output use structured outputs (next topic).
  5. Let the model think when the task needs it, and keep the reasoning separate from the final answer.
SYSTEM = """You classify customer support tickets for a bank.
Return one label from: billing, fraud, account_access, other.
If a ticket mentions an unrecognised transaction, the label is fraud even if the tone is calm."""

def build(ticket: str, examples: list[tuple[str, str]]) -> list[dict]:
    shots = "\n".join(f"<example>\n<ticket>{t}</ticket>\n<label>{l}</label>\n</example>" for t, l in examples)
    user = f"{shots}\n\n<ticket>\n{ticket}\n</ticket>\nThink briefly inside <reasoning>, then give <label>."
    return [{"role": "user", "content": user}]

Zero-shot, few-shot, chain-of-thought

  • Zero-shot: fastest and cheapest; try first on strong models.
  • Few-shot: best for format and borderline decisions; choose examples that cover edge cases, not only easy ones.
  • Chain-of-thought: asking for step-by-step reasoning helps multi-step problems. Reasoning-tuned models do this internally, so a long manual "think step by step" can be redundant there. Measure, don't assume.

Treat prompts as code

A prompt without a test set is a guess. Keep an evaluation file next to the prompt and run it on every edit.

import json, collections

def evaluate(predict, path="golden.jsonl"):
    rows = [json.loads(l) for l in open(path)]
    confusion = collections.Counter()
    for r in rows:
        confusion[(r["label"], predict(r["ticket"]))] += 1
    acc = sum(v for (g, p), v in confusion.items() if g == p) / len(rows)
    return acc, confusion

Version the prompt and the model together in git, and record both in logs: when quality drifts you need to know which changed.

Common mistakes

  • Optimising on three hand-picked examples.
  • Changing the prompt and the model in the same commit.
  • Burying the important instruction in the middle of a long prompt. Put it first or last.
  • Treating a prompt as a security boundary. It is not: see the guardrails topic.

Exercises

  1. Take a prompt you use and write 20 test cases, including five adversarial ones. Report accuracy before and after one improvement.
  2. Compare zero-shot, three-shot and chain-of-thought on the same set and record cost and accuracy for each.

You are done when you can…

  • Rewrite a vague prompt into a structured one and show the measured improvement
  • Choose between zero-shot, few-shot and chain-of-thought for a given task
  • Keep prompts in version control with a test set that runs on every change