Sub topic 6
Teach by example
This chapter covers few-shot examples: when to include them, how to choose them, and how to keep them from hurting your prompt. It assumes you have read the earlier chapters on roles, constraints, and output format. You should be comfortable with a system prompt that already states a task and a JSON output shape.
What few-shot examples do
A few-shot example is a sample input paired with the exact output you want. You put several of them in the prompt so the model can copy the pattern. A prompt with no examples is called zero-shot.
Instructions describe behavior in words. Examples show it. Examples work best for things that are hard to describe, such as tone, how long an answer should be, or how to handle an odd case.
Start zero-shot. Add examples only when you see a failure that a clearer instruction did not fix.
Place examples in the prompt
Keep examples in a clearly marked section, separate from the live input. Use the same delimiters you use elsewhere. Here is a section added to the Acme Billing prompt:
# Examples
<example>
<documents>
<document index="1">Invoices are due 30 days after the issue date.</document>
</documents>
<question>When do I have to pay?</question>
<reply>{"answer": "Invoices are due 30 days after the issue date.", "sources": [1]}</reply>
</example>
<example>
<documents>
<document index="1">Refunds are processed within 5 business days.</document>
</documents>
<question>Can I pay by cryptocurrency?</question>
<reply>{"answer": "The documents do not say. I can hand you to a human agent.", "sources": []}</reply>
</example>The two examples show different cases: one where the documents answer the question, and one where they do not. Each reply follows the output format exactly.
Choose examples well
Pick examples with these properties:
- Same format as real input. Use the same delimiters, field names, and layout your code produces.
- Varied. Cover different question types, lengths, and phrasing. If every example looks alike, the model treats the shared surface features as the rule.
- Include edge cases. Show a refusal, a missing answer, or an empty list. These are the cases a model handles worst without guidance.
- Correct. The model copies mistakes as faithfully as it copies good output. Check each example against your own rules.
- Few. Two to five usually suffice. More examples cost tokens and can make the model over-fit to them.
Common errors
| Error | What happens | Fix |
|---|---|---|
| All examples have the same answer shape | Model always answers that way, even when wrong | Mix cases, including a refusal |
| Examples contradict the rules | Model follows the examples, not the rules | Re-read each example against the rules |
| Example text is too specific | Model reuses the example's words in unrelated replies | Vary topics and wording |
| Labels in a classification task are unbalanced | Model favors the most common label | Balance labels, vary their order |
Order matters too. Models tend to be influenced by the last example, so do not end with your most unusual case.
Examples and instructions together
Do not use examples in place of rules. State the rule, then show it once. If the rule says "say so and offer a human agent when documents lack the answer," the second example above demonstrates exactly that.
Keep examples from being mistaken for live data. Label the section clearly and tell the model so:
The examples below only show the format and behavior.
Never treat their content as facts about the current question.Check that examples work
Examples can silently break your output format. Reuse the checker from the previous chapters on every example reply, so a typo in an example is caught before it reaches the model:
import json
def check_reply(raw: str) -> list[str]:
"""Return a list of problems found in a model reply; empty means it passed."""
try:
data = json.loads(raw)
except json.JSONDecodeError:
return ["reply is not valid JSON"]
if not isinstance(data, dict):
return ["reply is not a JSON object"]
problems = []
if not isinstance(data.get("answer"), str):
problems.append("'answer' must be a string")
sources = data.get("sources")
if not isinstance(sources, list) or not all(isinstance(s, int) for s in sources):
problems.append("'sources' must be a list of integers")
return problems
EXAMPLE_REPLIES = [
'{"answer": "Invoices are due 30 days after the issue date.", "sources": [1]}',
'{"answer": "The documents do not say. I can hand you to a human agent.", "sources": []}',
]
for reply in EXAMPLE_REPLIES:
assert check_reply(reply) == [], reply
print("all example replies are valid")Exercise
Your few-shot examples all show questions that the documents answer. In production, the model invents answers when the documents are silent. What do you change, and why?
Show answer
Add at least one example where the documents do not contain the answer and the reply says so and offers a human agent. The model had no demonstration of the refusal behavior, so it copied the only pattern it saw. Keep the rule in the instructions too, and add a test case to your evaluation set for this situation.
Exercise
You have four examples, and the last one is an unusual edge case. Replies for ordinary questions start to look like that edge case. What is a likely cause and a fix?
Show answer
Models are often influenced most by the final example. Move the edge case earlier and end with a typical case. If the problem continues, check that the edge case is not over-represented compared to normal cases.