Module 5 · Prompt engineering and structured output · Chapter 17
Explicit criteria and few-shot prompting: fix inconsistent output
- Vague instructions like "flag bad code" are not criteria. Two runs read them two ways and disagree.
- Explicit criteria name defect classes, a severity threshold, and what to ignore, so the model can check line by line.
- Consistent examples then pin down format and hard edge cases rules describe badly.
The picture: criteria are the law, examples are past rulings. Together they make verdicts predictable.
Domain 4, Prompt Engineering and Structured Output (20 percent of the exam). You should be able to:
- Replace vague instructions with explicit, checkable criteria for review or extraction.
- Measure quality, such as false positives, before and after a prompt change.
- Design few-shot examples that set format and settle edge cases.
- Keep examples consistent with each other and with the criteria.
- Pick examples covering hard cases, not easy ones.
Harbor & Pine Outfitters is a made-up company. Its CI reviewer from chapter 16 runs on every pull request to harbor-web. Owen Brandt re-runs it on the same pull request twice, as a test.
- Run one: "Block. Unsafe code." Run two, same code, same prompt: "Approve."
- The prompt only said: "Flag bad code."
- Lucia Fernandes sees the same split in invoice extraction: one run takes the due date as the invoice date, the next run takes the issue date.
Nadia Okafor states the cost: a tool that disagrees with itself gets ignored. Developers mute the reviewer, and finance rechecks every invoice by hand. This chapter fixes the prompts, not the model.
- Steps 1 and 2: named, checkable criteria, thresholds, an ignore list, and measuring false positives.
- Steps 3 and 4: few-shot examples for format and hard cases, kept consistent.
- Worked example with numbers, then a lab: rewrite the CI reviewer prompt.
Next: why "bad code" fails as an instruction.
Step 1: Replace vague words with named criteria
Why this step: most inconsistent output is a vague prompt, not a weak model.
"Flag bad code" forces the model to invent its own standard on every run. One run counts style as bad. The next does not.
Explicit criteria remove the guessing. Name three things:
- Defect classes. The exact problems to flag: SQL built by string concatenation, secrets in code, missing error handling on a payment call.
- Severity threshold. Which classes block a merge, which only earn a comment.
- An ignore list. What never to flag: naming style, fake data in test files.
Think of a referee's rulebook. "Play fair" gives a different match every week. "A tackle from behind is a foul" gives the same call most weeks.
Where the picture breaks down:
- A referee can stop play and ask. The model just answers, so unclear criteria fail silently.
- No rulebook lists every case. That gap is what Step 3's examples are for.
Quick check: which line is a criterion?
Next: making each criterion checkable, and counting the damage before you change anything.
Step 2: Make criteria checkable, then measure
Why this step: a criterion works only if the model can verify it, and a fix counts only if measured.
A checkable criterion has a yes or no test per line or per field:
- Weak: "Watch out for security problems." No test, no boundary.
- Strong: "Block if a password, token or API key appears as a literal string. Ignore values from environment variables."
- Strong: "Comment, do not block, if a function lacks error handling but makes no payment, auth or database call."
The invoice extractor needs the same treatment. "Extract the date" becomes: "Return the issue date labelled Invoice Date. Never return the due date. If none appears, return null instead of guessing."
Measure before and after. Take a fixed set of past pull requests where humans agreed the right verdict. Run the old prompt, count false positives. Change the prompt, run the same set, count again. Without the before number, "better" is a feeling.
Do this: predict the effect before you read the answer.
They drop. Many false positives come from a small set of repeat causes. Naming those causes and excluding them removes those flags on every run.
Next: the picture, from vague prompt to stable verdict.
The picture: criteria first, examples second
Press Next to walk the fix.
- The whole instruction is "flag bad code". No classes, no threshold, no ignore list.
- Owen sends the same pull request twice. Nothing changed except the run.
- One run blocks, one approves. Each run invented its own standard for bad.
- Named classes, a threshold and an ignore list replace the guessing. Most runs now agree.
- Worked verdicts show how the criteria apply to borderline cases.
- Same input, same verdict, run after run. Trust comes back.
Next: what examples do that paragraphs of rules cannot.
Step 3: Few-shot examples pin down format and edge cases
Why this step: some decisions are easier to show than describe.
A few-shot prompt includes a few input and output pairs before the real input. The model copies the pattern: output shape, labels, level of detail, and how borderline cases were decided.
- Format. One example showing "Verdict: BLOCK, Class: secret in code, Line: 42" teaches the exact shape faster than a paragraph on formatting.
- Edge cases. A fake password in a test file, shown as "Verdict: IGNORE", settles a case rules argue about.
- Field choices. An invoice example pairing two dates with the issue date teaches which date wins.
It works like showing a new hire three past invoices and how each was filed. The pattern lands faster than the manual.
Where the picture breaks down:
- A hire can ask questions. The model cannot, so a confusing example just confuses.
- Three examples cannot cover every case. They guide judgment, not guarantee it. Chapter 18 covers guarantees.
Quick check: which example earns its place?
Next: the consistency rule, where one bad example undoes good criteria.
Step 4: Keep examples consistent, and pick hard cases
Why this step: the model learns from examples even when they are wrong.
- Examples must agree with the criteria. If criteria say test files are ignored, no example may block on one.
- Examples must agree with each other. Two similar inputs with opposite verdicts teach that either answer is fine.
- A contradictory example teaches the contradiction. The model cannot tell which signal was the typo.
- Pick hard cases. Spend examples on borderline calls: the fake secret, the two-date invoice, the payment call with partial error handling.
- Keep the count small. A few sharp examples beat twenty mixed ones, and cost fewer tokens per run.
Before shipping, read the examples against the criteria one by one. Any clash means fix one of them, never ship both.
Next: the numbers, before criteria, after criteria, after examples.
Worked example: false positives, measured three ways
Why this step: prove a prompt fix with counts, not impressions.
Owen builds a test set: 200 past pull requests, labelled by human reviewers. In 26, a real blocking defect exists. All counts below are made-up.
Round 1, vague prompt ("flag bad code"):
1. The reviewer flags 68 pull requests. Of those, 24 are real defects and 44 are false positives.
2. Precision: 24 ÷ 68 = about 35 percent. Two of every three flags are noise.
Round 2, explicit criteria added:
3. Same 200 pull requests. The reviewer flags 34: 24 real defects, 10 false positives.
4. Precision: 24 ÷ 34 = about 71 percent. False positives fell from 44 to 10, a drop of 34.
Round 3, criteria plus three few-shot examples:
5. The reviewer flags 29: 24 real defects, 5 false positives.
6. Precision: 24 ÷ 29 = about 83 percent. The examples removed 5 more false positives, mostly fake secrets in test files.
7. Missed real defects stayed at 2 in every round (26 real, 24 caught). The fix cut noise without hiding real problems.
Notice the order. Criteria did the heavy lifting. Examples cleaned up the borderline cases.
Next: your turn. Rewrite the reviewer prompt in the lab.
Hands-on lab: rewrite the vague prompt
Why this exercise: writing criteria and examples is the exact skill 4.1 and 4.2 test.
Lab: The cold open's CI reviewer prompt says only: Review this pull request. This lab runs in your browser and needs JavaScript enabled.
Next: a field note, then mix-ups.
In production, teams commonly keep a small labelled test set for each prompt, a few dozen past cases with agreed answers, and rerun it after every prompt edit. A change that helps one case and breaks two others shows up in the counts first. Treat exact set sizes as team practice, not a standard.
Mix-ups to avoid
- More rules instead of clear rules. A page of vague rules is still vague. Fix: fewer criteria, each with a yes or no test.
- Examples that contradict the criteria. The model learns the clash. Fix: audit every example against every criterion first.
- Easy examples. They teach what the model already knows. Fix: spend examples on borderline cases.
- Declaring victory without counts. "It feels better" is not evidence. Fix: same test set, before and after numbers.
- Expecting examples to guarantee format. Few-shot guides probabilistically. Fix: for guaranteed structure, use chapter 18's schema tools.
Chapter quiz
Five questions. Score 70% or more to pass.
Review this chapter in 5 minutes
Watch the whole chapter recapped: checkable criteria over vague words, measuring false positives, and consistent few-shot examples for the hard cases.
- "Flag bad code" is not a criterion. Two runs invent two standards and disagree.
- Explicit criteria name defect classes, a severity threshold, and an ignore list, each with a yes or no test.
- Measure on a fixed labelled set: made-up counts here went from 44 false positives to 10 with criteria, then 5 with examples.
- Few-shot examples pin down output format and settle borderline cases that rules describe badly.
- Examples must agree with the criteria and each other. A contradictory example teaches the contradiction.
- Spend examples on hard cases, not easy ones.
- Examples guide probabilistically. Guaranteed structure needs schemas, next chapter.
Decision rule: if output is inconsistent, replace vague words with checkable criteria, add consistent examples for hard cases, and prove it with before and after counts.
Independent exam preparation. Not affiliated with or endorsed by Anthropic.
Review
Glossary
- Explicit criterion
- A rule with a yes or no test the model can apply. Example: block if a password appears as a literal string.
- Defect class
- One named kind of problem to flag. Example: SQL built from user input.
- Severity threshold
- The line between outcomes such as block and comment only. Example: payment calls block, style never flags.
- Ignore list
- Cases never to flag. Example: fake credentials in test files.
- False positive
- A flag where no real problem exists. Example: blocking over a fake test password.
- Precision
- Real problems divided by all flags raised. Example: 24 of 29 flags is about 83 percent.
- Few-shot prompting
- A few input and output examples before the real input. Example: two worked verdicts before a new pull request.
- Edge case
- A borderline input where the right decision is not obvious. Example: an invoice with two dates.
- Contradictory example
- An example that disagrees with the criteria or another example. Example: blocking a test file the criteria say to ignore.
Next: "Guaranteed structure: tool use, JSON schemas and tool_choice".