Prompt Engineering Patterns
A working toolkit of reusable techniques for getting reliable, structured output from a large language model — including few-shot examples, chain-of-thought reasoning, and forced output formats — since the same underlying model can perform very differently depending only on how the prompt is written.
Prerequisites: Large Language Models
Two people can send the same large language model completely different prompts and get answers of wildly different quality, even though neither one retrained the model or changed a single parameter. All the model ever sees is the text it's given — so the exact wording, structure, and examples in that text materially change what comes back out. Prompt engineering is the practice of writing that input deliberately, using patterns that reliably improve output quality across many kinds of tasks.
The analogy: briefing a smart intern who has no memory of yesterday
A capable intern with no context does much better work if you show them a worked example of the exact task first, ask them to think out loud before answering, and specify exactly the format you want the answer in — versus just handing them a bare instruction and hoping for the best. A large language model behaves the same way at each new conversation: it has broad knowledge but zero memory of any convention you haven't spelled out in this exact prompt, so the same three briefing techniques — show an example, ask for reasoning, specify the format — carry over directly.
Four patterns worth knowing
- Zero-shot vs. few-shot prompting. A zero-shot prompt just states the task ("classify this headline's sentiment"). A few-shot prompt includes two or three worked examples of input-and-correct-output pairs before the real question, which reliably improves accuracy on tasks with a specific, non-obvious output format the model hasn't seen phrased that way before.
- Chain-of-thought prompting. Instructing the model to "think step by step" before giving a final answer, or showing few-shot examples that include the reasoning steps, measurably improves performance on multi-step tasks like arithmetic or logical deduction, because it lets the model generate and reuse intermediate results rather than jumping straight to a final answer.
- Structured output constraints. Explicitly specifying the required output format — "respond only with valid JSON matching this schema" — reduces the rate of unparseable or inconsistent responses, which matters enormously when the output feeds directly into downstream code.
- Role and context framing. Prefacing a prompt with a role ("You are a financial analyst reviewing a 10-K excerpt") and relevant context narrows the space of plausible continuations toward the domain-appropriate register and vocabulary, rather than a generic conversational tone.
Worked example: zero-shot versus few-shot on a tone-labeling task
Zero-shot prompt: "Classify the tone of this sentence as positive, negative, or neutral: 'Revenue grew modestly but margins compressed.'" A model might reasonably but inconsistently output "mixed," "neutral," or "negative" across repeated runs, since the required label set was stated but never demonstrated. Adding two few-shot examples — one sentence with a genuinely positive label, one with a genuinely negative label, each formatted exactly as {"tone": "positive"} — anchors both the exact three-way label set and the exact output format, sharply reducing the rate of off-format or off-taxonomy answers on the real question that follows.
Worked example: chain-of-thought on a numeric question
Asked directly, "A position of $120,000 lost 8% then gained 15%. What's the final value?", a model prompted for an immediate answer sometimes applies the percentages in the wrong order or skips a step. Prompted instead with "think step by step," it typically works through: , then , landing on the correct $126,960 — the intermediate step keeps a two-stage calculation from being collapsed into a single wrong shortcut like averaging the two percentages.
What this means in practice
Prompt engineering patterns are the cheapest lever for improving LLM output quality — no fine-tuning, no additional training data, just better-structured input — which is why they're usually the first thing tried before reaching for parameter-efficient fine-tuning or a bigger model. The limitation is that prompting only shapes behavior within what the model already knows; it can't teach the model new facts or fix a systematic knowledge gap, and a model that will confidently hallucinate an unknown fact will usually keep doing so no matter how the prompt is engineered around it.
Prompt engineering patterns — few-shot examples, chain-of-thought instructions, explicit output-format constraints, and role framing — reliably change how well a language model performs on a task using the exact same underlying weights, because the prompt is the only channel the model receives information through at inference time.
Related concepts
Practice in interviews
Further reading
- Wei et al., Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- Brown et al., Language Models are Few-Shot Learners (GPT-3)