Part IV · Applied concepts · Chapter 21
Prompt engineering
A prompt is the program you hand a language model, written in plain language. Prompt engineering is writing that program so the model does the same right thing every time, including on the inputs you did not think of. It is less about magic phrases and more about the habits of a good specification: say the job, give the reasons, show examples, fix the output shape and keep data apart from instructions.
Every feature built on a language model is driven by prompts. A one-line change to a prompt can change answers across thousands of questions, so prompts deserve the same care as code: review, tests and a version number.
What a prompt is made of
An API call to a chat model sends more than one block of text:
- A system prompt: standing instructions for the whole conversation, such as the role, rules and output format.
- Messages: the conversation so far, alternating user and assistant turns, with tool calls and tool results in between (chapter 28).
- Tool definitions: names, descriptions and input schemas of the tools the model may call. These are prompt text too, and their wording matters.
- An output schema, when structured output is used (chapter 16).
All of it becomes tokens in one context window. The model has no other knowledge of your situation: what is not in the prompt or in its training does not exist for it.
Seven habits that work
1. Say the job, the audience and why it matters
"Summarise this" leaves the model to guess length, reader and purpose. "Summarise this policy for a customer deciding whether to return a book; keep only what affects that decision" does not. Giving the reason behind a rule helps the model apply it to cases the rule did not list.
2. Be specific where it counts
Name the edge cases you care about and what to do in each. Vague rules ("be careful with numbers") change nothing; precise ones ("never state a price or a date from memory; quote it from the document") do. If two rules could conflict, say which wins.
3. Structure the prompt
Use headings or tags to separate role, rules, data and the request. Put long reference material before the question, and the question last. Clear sections also make the prompt easier for people to review and change.
4. Show examples
A few worked input-and-output pairs, called few-shot examples, often teach a format or a judgement call faster than paragraphs of rules. Two cautions: models copy surface features of examples (length, wording, even the order of labels), so vary them; and examples must agree with the rules, or the model will follow the examples.
5. Fix the output shape
If code will read the answer, ask for JSON matching a schema, enforce it where the provider allows, and validate it anyway (chapter 16). If a person will read it, say how long and in what form.
6. Keep data apart from instructions
Anything pasted into a prompt, a document, a web page, an email or a tool result, may contain text that looks like an instruction. Mark it clearly as data and tell the model never to follow instructions inside it. This reduces, but does not remove, the risk of prompt injection; the real defences are in chapter 35.
7. Do not let the model do arithmetic it can get wrong
Models predict digits as tokens (chapter 16). They are good at choosing which calculation to do and poor at guaranteeing its result. Give them a tool that computes, and tell them to use it for every figure.
A worked improvement
Suppose a prompt labels customer messages, and on a test set of 40 messages it gets 31 right (77.5%). Looking at the 9 failures is the work; guessing is not.
| Change | Failures fixed | Correct |
|---|---|---|
| Starting prompt: task only | 31 / 40 | |
| Add the rule "a question about where a replacement parcel is stays orders, even when the message mentions a return" | 4 | 35 / 40 |
| Add three examples, one of them a two-intent message | 3 | 38 / 40 |
| Add JSON-only output (two replies had been prose) | 2 | 40 / 40 |
The numbers are illustrative, but the method is the point: a fixed test set, one change at a time, the score after each change, and every failure read. With only 40 questions, a gain of one or two is within noise (chapter 36 shows how to put an interval around a score), so keep the test set growing and rerun the whole set after every change, because a fix for one case can break another.
Split big jobs into steps
One prompt that must classify, retrieve, calculate and write tends to do each part worse. Splitting the job into steps, each with its own short prompt and checked output, makes each step testable on its own, lets cheaper models handle the easy steps, and shows you which step failed. The cost is more calls and more latency. A support assistant, for example, can be built this way: a first step labels the message and how hard it is, a second step answers it with the right tools, and checks run before anything is shown to the customer (chapter 29 lists the common shapes).
Prompts are code
- Keep them in files, under version control, not in string literals scattered through the code.
- Version them. When a prompt changes in a way that changes behaviour, give it a new version and record the version with every call, so any answer can be traced to the exact text that produced it.
- Test them on a fixed set of cases before and after every change, and look at the failures, not just the score (chapter 44).
- Aim a change at one failure. A prompt change is often one sentence written for one observed failure: when two answers contradict each other, add the rule that says which one wins. Then rerun the whole test set, because the sentence can shift other cases.
- Fill fixed wording in from code. Text that must not vary, such as a legal notice or an approved disclaimer, belongs in code that copies it into a placeholder in the prompt or onto the answer, not in the model's own words.
- Re-test when the model changes. A prompt tuned for one model is not guaranteed to behave the same on another, or on a newer version of the same one.
A prompt says "Keep answers short." and, in another section, "Always explain every figure with its source and date." Answers come back long. What is wrong, and how would you fix it?
Answer
The two rules conflict and the prompt does not say which wins, so the model resolves it inconsistently. Make the priority explicit, for example: "Explain each figure with its source and date in one short clause; keep everything else to two sentences."
In Lab 1 you send a system prompt and a message to a model and read the reply, the token counts and the cost. In Lab 2 you build a test bench: a fixed set of labelled cases, a score for each prompt version and a case-by-case comparison of two runs, which is the worked improvement above done by code.
Self-check
Why give the reason behind a rule, not just the rule?
The model can then apply the rule's intent to cases the rule did not list, and resolve borderline cases the way you would.
What is the risk of few-shot examples?
Models copy their surface features, such as length, wording or label order, and follow the examples over the rules when they disagree. Vary the examples and keep them consistent with the rules.
Why record a prompt version with every call?
So that any answer, cost or failure can be traced to the exact prompt text that produced it, and so results before and after a prompt change can be compared.
Who should do the arithmetic when an answer needs exact figures?
Code. The model chooses which calculation to run and calls a tool; the tool computes it with exact arithmetic and the model quotes the result.
Further reading
- Prompting best practices (Claude API docs) docs beginner
Anthropic's single guide to clear instructions, examples, XML-tag structure, roles and thinking. - anthropics/prompt-eng-interactive-tutorial docs beginner
Free hands-on notebook course on prompt structure, roles, examples and avoiding hallucination. - Prompt engineering | OpenAI API docs beginner
OpenAI's guide, including message roles and how prompting differs for reasoning models. - Language Models are Few-Shot Learners (2020) paper advanced
The GPT-3 paper that showed in-context learning from a handful of examples in the prompt.
Free, checked on 2026-10-21. All resources for this chapter and 4 more.