Basics
The whole idea first, then the terms the rest of this page uses.
1.1Whole idea
A prompt gives the model its task, its format and its examples. Four patterns do most of the work: show examples, ask for reasoning, constrain the output, and explore options when reasoning can branch.
Four patterns cover most prompts. Test each against an eval set rather than trusting folklore.
1.2Key concepts
One idea per card, in plain words.
Shows worked examples. Often the fastest way to fix format and tone.
Asks for reasoning before the answer. It helps multi-step problems, and costs tokens and latency.
Explores several reasoning paths and prunes them. Powerful for puzzles, expensive for everyday tasks.
Forces a schema, so code can read the result.
Question
What exactly you must answer, and what to settle before you start.
2.1Question
A ticket classifier sits at 78% accuracy and sometimes returns prose instead of a label. Improve it using prompt patterns only, and prove the gain.
Detailed answer
How it works, and how to build or explain it.
3.1Step by step
The mechanism, in the order you would explain it.
- Build the eval first
Label 200 tickets, including hard ones. Without this you can't see improvement.
- Fix the format
Require JSON with an enum of labels.
- Add few-shot examples
Pick examples near the decision boundary, covering each label.
- Add reasoning if needed
Ask for brief reasoning only if evals improve enough to justify the tokens.
- Version and test
Treat prompts as code: review them, version them, and gate them on evals.
3.2Code
Working code for the core of it.
Assembling a prompt
A prompt is a program with a structure. Keep it in code, so you can test and version it.
1export interface Example {2 input: string;3 output: string;4}5 6/** Assembles a few-shot prompt: rules first, examples next, the real input last. */7export function buildPrompt(opts: { task: string; examples?: Example[]; schema?: object; reason?: boolean; input: string }): string {8 const parts = [opts.task];9 if (opts.reason) parts.push("Work through the problem step by step, then give the final answer.");10 if (opts.schema) parts.push(`Reply with JSON matching this schema:\n${JSON.stringify(opts.schema)}`);11 for (const e of opts.examples ?? []) parts.push(`Input: ${e.input}\nOutput: ${e.output}`);12 parts.push(`Input: ${opts.input}\nOutput:`);13 return parts.join("\n\n");14}parts = [opts.task]- Rules first.
opts.schema- States the output format, which later gets checked by the guard.
Input: ... Output:- The real input goes last, in the same shape as the examples.
Tested by npm run test:modules.
Q&A
What interviewers ask next. Answer out loud, then open.
4.1Interview questions
How many examples?
When does chain of thought hurt?
Where do prompts live?
Extra depth
Edge cases, counterpoints and what to read next.
5.1Edge cases and counterpoints
Includes the strongest argument against this approach.
Show detailsHide details
- Example order
- Recent examples can weigh more. Shuffle them in your evals.
- Counterpoint
- Long prompts accumulate rules nobody tests. Delete any instruction your evals don't need.