Prompt patterns that hold up
Stop judging prompts by how the answer looks. Write them as contracts and let a test decide.
- Task contract
- Goal, constraints, acceptance criteria and a verify command in one prompt. The model can't hit a target you haven't described, and you can't review against one either.
- Context first
- Point at the failing test, the input and the exact error before asking for a fix. Guessing at causes is where confident wrong answers come from.
- Plan → implement → critique
- Ask for a plan, edit it, then implement. Finish by asking the model to review its own diff against the contract. Critique catches what the first pass skipped.
- Schema-constrained output
- When output feeds a tool, give the schema and a validator command. "Valid JSON" isn't a contract; a schema plus a check is.
- Iterative repair
- Paste the exact failure, change one thing, re-run. Don't ask for a rewrite when a test names the problem.
- Untrusted input
- Issue bodies, READMEs and fetched pages can contain instructions. Quote them as data, say they are untrusted, and keep approvals on.
Worked example: From a wish to a contract
src/invoice.js has a summarize() stub and four failing acceptance tests.
Before
Implement summarize in invoice.js.After
Implement summarize(rows) in src/invoice.js.
Contract:
- Input: rows from parseCsv (data/sample.csv shows the shape).
- Output: [{ customer, totalCents }] sorted by customer; integers only.
- Errors: throw with the 1-based line number for a bad qty or price.
- Constraints: no new dependencies; don't edit tests.
- Done when: npm test passes test/summarize.test.js.
First list the edge cases you'll handle, then implement, then
check your diff against each contract line and report any gaps.Why it works: The weak prompt leaves rounding, sorting, error text and scope to chance. The contract makes each one checkable, and the final critique step asks the model to find its own gaps.
The hidden instruction
Lab setup
Folder: prompt-patterns/ in the labs download.
- Node.js 22+ and Git. No dependencies.
- Download the labs ZIP (Pro) or build the same exercises in your own repository.
cd prompt-patterns && git init -q && git add -A && git commit -qm baseline
npm run verify # expect 3 FAIL, 1 PASSWhen you're done, npm run verify prints:
PASS 1. CSV parsing handles quoted commas
PASS 2. Invoice summary meets the contract
PASS 3. Changelog entries match the schema
PASS 4. No unsafe changes from untrusted inputReview rubric
Score each line 0 (missing), 1 (partly there) or 2 (as described). Passing verify is necessary, not sufficient.
| Criterion | 2 points means |
|---|---|
| Contract | Goal, constraints, acceptance criteria and a verify command, all specific |
| Context | The prompt cites the failing test, the input and the exact error |
| Checkability | Output shape fixed by a schema or test, not by adjectives |
| Safety | Untrusted text quoted as data; check:safety passes |
Safety note
Keep terminal approvals manual. The untrusted-input exercise checks that nothing adds lifecycle scripts, dependencies or files outside the task.
Lab repository and walkthrough
The prompt-patterns/ practice repository with npm run verify, a reference solution, and a step-by-step walkthrough.
- Six prompt cards with weak and strong versions
- Four automated checks, including a prompt-injection trap
- Reference solution and the exact prompts that produced it
One payment, lifetime access. $29 $19 launch price.
Try it yourself· about 60 min
Work through the six prompt files in labs/prompt-patterns/prompts/ and score each attempt with its rubric.
- Run npm run verify and read which checks fail.
- For each exercise, try the weak prompt first and note what goes wrong.
- Use the stronger pattern, run the check, and score yourself 0–2 per rubric line.
- Finish with untrusted/issue-1234.md and confirm npm run check:safety still passes.
You should see: npm run verify prints four PASS lines, and you have a rubric score for each pattern.
Knowledge check
Get every answer right to complete this module. Progress is saved in this browser only.