Prompt Patterns: Chain of Thought, Few-Shot, Structured Output

Four patterns cover most prompts. Test each against an eval set rather than trusting folklore.

Part 1 of 5

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.

Why it matters

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.

01Few-shot

Shows worked examples. Often the fastest way to fix format and tone.

02Chain of thought

Asks for reasoning before the answer. It helps multi-step problems, and costs tokens and latency.

03Tree of thoughts

Explores several reasoning paths and prunes them. Powerful for puzzles, expensive for everyday tasks.

04Structured output

Forces a schema, so code can read the result.

Part 2 of 5

Question

What exactly you must answer, and what to settle before you start.

2.1Question

Interviewer

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.

Part 3 of 5

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.

  1. Build the eval first

    Label 200 tickets, including hard ones. Without this you can't see improvement.

  2. Fix the format

    Require JSON with an enum of labels.

  3. Add few-shot examples

    Pick examples near the decision boundary, covering each label.

  4. Add reasoning if needed

    Ask for brief reasoning only if evals improve enough to justify the tokens.

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

prompt-builder.tsTypeScriptCopy
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}
14 linesUTF-8TypeScript
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.

Part 4 of 5

Q&A

What interviewers ask next. Answer out loud, then open.

4.1Interview questions

How many examples?
Three to five usually help most. More cost tokens and can narrow the model's behaviour.
When does chain of thought hurt?
On simple lookups it adds cost and latency with no gain. Newer reasoning models often need less of it.
Where do prompts live?
In version control, with an eval suite, rather than in application strings.
Part 5 of 5

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.