Instructions and examples
8 min read
What instructions are for
The instructions usually go in the System promptThe instructions at the start of a prompt that set the model's role, its rules and the format of its answer. It stays the same on every call., the message that sets the rules for the whole conversation. The user's message then asks for something inside those rules. A good set of instructions answers five questions.
- Role. Who is the model acting as, and for whom?
- Task. What should it produce?
- Rules. What must it always do, and what must it never do?
- Sources. Where may it get facts: only the supplied documents, or its own knowledge too?
- Format. How long, in what shape, in what tone?
The Lesson Writing a clear prompt covers the writing itself. Here the question is different: what must an application's instructions carry so that thousands of different requests all come out right?
Write instructions that can be checked
Vague instructions cannot be tested. "Be helpful and accurate" gives you nothing to compare an answer against. Compare two versions of the same rule.
| Vague | Checkable | |
|---|---|---|
| Sources | Use the documents | Answer only from the sources below, and cite each claim with its number |
| Length | Keep it short | Reply in at most three sentences |
| Missing facts | Be honest | If the sources do not answer, reply exactly: I could not find that |
| Tone | Be friendly | Use plain words and no exclamation marks |
Each checkable rule can be tested by a program or a quick read. The Citations and honest refusals Lesson shows code that checks the first and third.
Two habits make instructions work better.
- Say what to do, not only what to avoid. "Do not be long" leaves the model guessing. "Reply in at most three sentences" does not.
- Give the reason. "Keep answers short because they appear in a phone notification" lets the model make good choices in cases you did not list.
Examples teach by showing
A rule says what you want. An example shows it. Giving a model a few input and output pairs is called Few-shot promptingPutting a few worked examples of the task in the prompt, so that the model copies their pattern and format in its answer., and it is the quickest way to fix a format, a tone or a way of handling an awkward case.
Choose examples with care.
- Cover the variety. Three examples that all look alike teach the model to expect that shape and no other.
- Include an edge case. Show what a good reply looks like when the answer is no, or the question is out of scope. Models copy what they are shown.
- Keep them correct. A wrong example is an instruction to be wrong, and it is easy to miss.
- Keep them few. Two to five usually do the job. More of them use space and rarely add much.
Pick the closest examples for each request
A fixed set of examples is simple. A better set depends on the request. If you keep a bank of good examples, you can choose the few most like the current question and put those in the context. This is a small retrieval step, and it works for the same reasons that search works.
The program below holds four examples and picks the two whose questions share the most words with the new request.
examples = [
("How long do I have to return a bike?", "30 days from delivery, if it is unused."),
("Is the battery covered by the warranty?", "Yes. The battery has a 2 year warranty."),
("How long does shipping take?", "3 to 5 business days."),
("Can I return a bike I have ridden?", "Only unused bikes can be returned. Please contact support."),
]
def words(text):
return set(text.lower().replace("?", "").split())
request = "How long do I have to return a sale bike?"
closest = sorted(examples, key=lambda pair: -len(words(request) & words(pair[0])))[:2]
for question, answer in closest:
print(f"Q: {question}\nA: {answer}\n")It prints:
Q: How long do I have to return a bike?
A: 30 days from delivery, if it is unused.
Q: Can I return a bike I have ridden?
A: Only unused bikes can be returned. Please contact support.The two chosen examples are the returns question and the question about a ridden bike. The second one is useful because it shows how to answer when the rule has an exception. The shipping and warranty examples would have taught the format just as well, but nothing about returns.
Word overlap is a crude way to compare questions. Real systems compare meaning, with the vector search from Embeddings and semantic search. The principle does not change: show the model the examples closest to the job in hand.
Where each part goes
Put instructions first and keep them stable between requests. Put examples after them and the question last. A stable start also lets the provider reuse its work between requests, which the next Lessons return to. Keep instructions out of the user's message wherever you can, so a user cannot rewrite them by accident.
Treat the instructions as code. Keep them in a file, review changes, and run your test questions after every edit, as Testing a context change shows.
Which instruction can you test?
You are rewriting a rule in a system prompt.
A checkable rule gives you something to compare an answer against. A wish does not.
What makes a good set of examples?
You have room for three examples in the prompt.
Models copy what they are shown, so cover the variety and show a good answer to an awkward case.
Why pick examples per request instead of using a fixed set?
You keep a bank of 40 good examples.
Choosing the few examples most like the request is a small retrieval step, and it works for the same reasons search does.
Your reading position is saved
0 of 11 complete
Lesson complete
Instructions and examples
Up next