Deployed Lab

Module 5 · Prompt engineering and structured output · Chapter 17

Explicit criteria and few-shot prompting: fix inconsistent output

21 min1 clickable picture1 hands-on lab1 predict, 2 quick quizzes, 5-question final quiz5-min video review
In 30 seconds

The picture: criteria are the law, examples are past rulings. Together they make verdicts predictable.

Exam objective: Task statements 4.1 and 4.2

Domain 4, Prompt Engineering and Structured Output (20 percent of the exam). You should be able to:

The cold open

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.

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.

The plan for this chapter
  1. Steps 1 and 2: named, checkable criteria, thresholds, an ignore list, and measuring false positives.
  2. Steps 3 and 4: few-shot examples for format and hard cases, kept consistent.
  3. 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:

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:

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:

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.

From a vague prompt to stable output A vague prompt gives different verdicts on two runs. Adding explicit criteria narrows the verdicts. Adding consistent few-shot examples settles the remaining edge cases, so runs agree. Vague promptflag bad code Two runssame pull request Blockrun one Approverun two Add criteriaclasses, thresholds Add exampleshard cases shown Stable verdictruns agree
  1. The whole instruction is "flag bad code". No classes, no threshold, no ignore list.
  2. Owen sends the same pull request twice. Nothing changed except the run.
  3. One run blocks, one approves. Each run invented its own standard for bad.
  4. Named classes, a threshold and an ignore list replace the guessing. Most runs now agree.
  5. Worked verdicts show how the criteria apply to borderline cases.
  6. Same input, same verdict, run after run. Trust comes back.
The problemThe testThe fixThe result

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.

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:

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.

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.

Field note, November 2026

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

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.

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".