Prompt Engineering / Reliability & Safety
Measuring prompt quality instead of guessing.
Reviewed by Yuvaraj
Most prompt tuning is done by feel: you tweak the wording, eyeball a couple of outputs, and ship whatever looks good. That works until it does not. A change that fixes one case silently breaks three others, and you have no way to know. Evaluating prompts means replacing gut feeling with a repeatable measurement: a fixed set of representative inputs, an explicit definition of "correct," and a number you can compare across prompt variants. Once you can measure a prompt, you can improve it deliberately, catch regressions automatically, and defend your choices with evidence instead of vibes.
The foundation is a labeled evaluation set: representative inputs paired with expected outputs, or with acceptance criteria when there is no single right answer. "Representative" is the load-bearing word. The set should span the real input distribution, including edge cases and the failures you already know about, not just the three prompts you happened to type while building.
Next, define metrics that match the task:
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
The rule that makes comparison valid: run every variant on the same set. If Prompt A and Prompt B see different inputs, the difference in scores is noise, not signal. And keep a held-out slice you never tune against. If you optimize wording until it aces every example you can see, you have overfit the prompt to those examples and learned nothing about new inputs. Spot-checking a few outputs is fine while exploring; the fixed offline set is what you trust when deciding what to ship.
Task: classify a support message into billing, bug, feature, or other. Prompt A is a zero-shot instruction. Prompt B is the same instruction plus four few-shot examples. Both run on the identical held-out cases.
| # | Input (abridged) | Expected | Prompt A | Prompt B |
|---|---|---|---|---|
| 1 | "charged twice" | billing | billing ✓ | billing ✓ |
| 2 | "crashes on login" | bug | bug ✓ | bug ✓ |
| 3 | "please add dark mode" | feature | bug ✗ | feature ✓ |
| 4 | "renewed after I cancelled" | billing | other ✗ | billing ✓ |
| 5 | "how do I reset password" | other | other ✓ | other ✓ |
| Metric | Prompt A | Prompt B |
|---|---|---|
| Success rate | 60% (3/5) | 100% (5/5) |
| Avg tokens | 180 | 420 |
| Avg latency | 620 ms | 910 ms |
Interpretation: B is clearly more accurate, but it costs roughly 2.3x the tokens and adds about 300 ms per call from the few-shot examples. Pick per budget. For a high-volume, latency-sensitive router, A plus a cheap fallback may win; for correctness-critical routing, B's accuracy justifies the cost. And note the real lesson: five cases is far too few to trust. Treat this table as a template and grow the set to dozens or hundreds before you rely on the percentages.
def evaluate(prompt, cases, model):
passed, tokens, latency = 0, 0, 0.0
for case in cases:
out, usage, ms = model.run(prompt, case["input"])
if check(out, case["expected"]): # your metric
passed += 1
tokens += usage.total_tokens
latency += ms
n = len(cases)
return {"success_rate": passed / n,
"avg_tokens": tokens / n,
"avg_latency_ms": latency / n}
Wire the same function into CI so prompts get the regression testing that application code already enjoys:
results = evaluate(PROMPT_B, held_out_cases, model)
assert results["success_rate"] >= 0.90, "regression: success rate dropped"
LLM-as-judge, used honestly
An LLM grader is genuinely useful when outputs are open-ended and no deterministic check applies. But it is a model, not an oracle. Validate the judge against a set of human-labeled examples and confirm it agrees with people before you trust its verdicts. Watch for known biases: judges favor the first option presented (position bias) and longer, more verbose answers (verbosity bias). Randomize order, run both A-vs-B and B-vs-A, and prefer pairwise comparison over absolute scores.
Common mistakes