Sub topic 3
Anatomy of a Good Example
This chapter assumes you know what a prompt and a completion are, can read short Python, and know the system, user, and assistant roles in chat prompts. The previous chapter showed that examples in a prompt steer a model. Here we look at what a single example is made of and which choices inside it matter.
The three parts of an example
Every few-shot example has three parts:
- Input. The text the model will receive for real, such as a review, a support ticket, or a log line.
- Output. The answer you want back for that input, written exactly as you want the model to write it.
- Delimiters. The markers that show where the input ends, where the output begins, and where one example ends and the next starts.
The model does not see "examples" as a special category. It sees one long text and predicts what comes next. Your examples make a pattern, and the unfinished final entry asks the model to continue it. Each part of an example is a signal about that pattern, and the model will pick up signals you did not intend as well as the ones you did.
The input: realistic, not tidy
The input half of an example should look like the data the model will see in production. If real reviews are messy, with typos, mixed opinions, and fragments, then examples made of clean, textbook sentences teach the model about a task that does not exist.
Check the following about your inputs:
- Length. If real inputs run to several paragraphs, short one-line examples leave the model unsure how to handle long text.
- Register. Casual, formal, and technical text can all appear in real use. Match what you actually get.
- Noise. Include the typos, emoji, or odd casing that real data has, if the task must handle them.
- Source. Real samples from your own data usually beat invented ones. Remove private information first.
One example from the spine is "The screen is bright but the speakers are weak." That is a mixed review. A pool made only of obviously positive or obviously negative text would never show the model what to do with it.
The output: exactly what you want to receive
The output half is the strongest signal in the prompt. The model will imitate its shape, length, casing, and vocabulary. Three rules follow.
Write only the output you want, nothing more. If an example answer is positive, the model will usually answer positive. If an example answer is Sentiment: The review is positive because the user likes the battery., expect sentences like that back, and expect them to be hard to parse.
Use a closed vocabulary where possible. For classification, use the same label strings in every example: positive, not Positive in one place and pos in another. Your parsing code and your accuracy check will compare strings, so the model's output must be comparable.
Make the output correct. A wrong label in an example is not a harmless typo. The model may follow it. Review every example output the way you would review test fixtures.
Delimiters: marking the boundaries
Delimiters are the labels and separators around each part. In the spine's plain-text prompt they are the field labels Review: and Sentiment: plus a blank line between examples.
Review: The package arrived on Tuesday.
Sentiment: neutral
Review: The screen is bright but the speakers are weak.
Sentiment:These markers do three jobs:
- The label before the input (
Review:) tells the model what kind of text follows. - The label before the output (
Sentiment:) tells the model where its answer goes. Ending the prompt on that label, with nothing after it, is what makes the model fill in the answer. - The blank line between examples marks where a pair ends. It also gives you a natural stop point.
You have a lot of freedom in choosing delimiters, but a few properties matter:
- Distinct from content. If your inputs may contain the string
Sentiment:, the boundary becomes ambiguous. Choose markers that rarely appear in the data, or wrap the input in markers such as<review>and</review>. - Used identically everywhere. The final entry must use exactly the same markers as the examples, up to the point where the answer begins.
- Safe for multi-line text. If an input can span several lines or contain blank lines, a blank line alone is a poor example separator. Use an explicit wrapper, such as
###lines or XML-style tags.
Building the prompt in code
Hand-typing prompts invites small inconsistencies, such as a missing colon in the third example. Generate every example from one function so they cannot drift apart. This version also refuses inputs that contain the delimiter text.
INSTRUCTION = "Classify the sentiment of each review as positive, negative, or neutral."
INPUT_TAG = "Review:"
OUTPUT_TAG = "Sentiment:"
def render(text, label=None):
"""Render one entry. With no label, leave the output slot open."""
if INPUT_TAG in text or OUTPUT_TAG in text:
raise ValueError("Input contains a delimiter string: " + text)
block = f"{INPUT_TAG} {text.strip()}\n{OUTPUT_TAG}"
if label is not None:
block += f" {label}"
return block
def build_prompt(examples, query):
parts = [INSTRUCTION]
parts.extend(render(text, label) for text, label in examples)
parts.append(render(query))
return "\n\n".join(parts)
examples = [
("The battery lasts all day and charging is fast.", "positive"),
("It stopped working after a week.", "negative"),
("The package arrived on Tuesday.", "neutral"),
]
print(build_prompt(examples, "The screen is bright but the speakers are weak."))The printed prompt matches the plain-text example from the spine. The last line is Sentiment: with nothing after it.
Because the model is continuing a pattern, it may keep going after its answer and invent a new Review: entry. Guard against this by setting a stop sequence, such as Review: or a blank line, in your API call if the API supports one. You can also cap the maximum output length at a few tokens for a one-word label.
Where the instruction fits
Examples work best alongside a short instruction, not instead of one. The instruction states the rule, and the examples show it. The instruction also fills gaps that a few examples cannot cover, such as what to do when a review is neither positive nor negative. If the instruction and the examples disagree, the model has to guess which to trust, so check that they say the same thing.
What the model learns from an example
An example teaches more than you intend. Besides the task itself, the model may infer:
- The label set. Only the labels it has seen are likely outputs.
- The output length and style. One word, one sentence, or a JSON object.
- Surface patterns. If all three positive examples start with "The" and none of the negative ones do, the model may use that as a clue.
- Frequency. A label that appears in most examples looks more probable. Later chapters cover label balance in detail.
So read your examples as the model would. Ask what, other than the task, they have in common.
Common mistakes in single examples
| Mistake | Effect | Fix |
|---|---|---|
| Output includes explanation when you want only a label | Model writes explanations, parsing breaks | Keep output to the target format only |
| Inconsistent label spelling (`Positive`, `pos`) | Unpredictable outputs, failed string comparisons | Define the label set once and reuse it |
| Delimiter text appears inside the input | Model confuses where the input ends | Pick rarer markers or wrap inputs in tags |
| Final entry uses different labels than the examples | Model drifts from the pattern | Generate every entry with the same function |
| Overly clean, invented inputs | Poor performance on real, messy data | Sample real inputs, with private data removed |
| Wrong label in an example | Model may copy the error | Review outputs like test fixtures |
A worked review
Here is a flawed example set for extracting a priority from support tickets.
Classify each support ticket as high, medium, or low priority.
Ticket: Site is completely down for all users!!
Priority: HIGH - this is urgent
Ticket: Typo on the pricing page
priority = low
Ticket: Export to CSV is slow for large files
Priority: mediumThe problems are easy to list once you know what to look for:
- The first output adds an explanation and uses capitals. The model may do the same.
- The second example uses
priority = lowin place ofPriority: low, so the output label and delimiter both differ. - The final entry has no open slot. A new ticket still needs to be appended with
Ticket:and a barePriority:. - All the inputs are short. If real tickets are long email threads, no example shows that.
A corrected version uses Priority: high, Priority: low, and Priority: medium as outputs, with the same Ticket: and Priority: labels in every entry, and adds one longer, realistic ticket.
Exercise
A few-shot prompt uses Input: and Output: as delimiters. In testing, the model sometimes stops answering correctly when the user's text contains the word "Output:". Name the cause and give two ways to fix it.
Show answer
The input contains the same text as the delimiter, so the model cannot tell where the input ends and its answer begins. Fixes: change the markers to ones that rarely appear in the data, such as <input> and </input> tags; or reject or escape inputs that contain the delimiter text before building the prompt, as the render function in this chapter does.
Exercise
You have three examples whose outputs are Positive, negative, and neutral. (with a period). Your code checks prediction == "negative". What problems can occur, and what single change prevents them?
Show answer
The model may copy the inconsistent casing and the trailing period, so predictions like Positive or neutral. will not match your string checks, and the model has no clear rule for its own output. Define one fixed label set in lowercase without punctuation, and generate all example outputs from it, so every example uses exactly positive, negative, or neutral.