Prompt engineering from the ground up
Few-shot examples
An example teaches a pattern faster than a paragraph of instructions. The risks are the ones a teacher would recognise: the wrong examples, too few of a class, and giving away the answer.
When an example beats a rule
Some tasks are easy to show and hard to describe: a house style, a particular tone, a labelling convention with edge cases. Writing the rule takes three paragraphs and still misses the nuance; two examples carry it immediately.
The reverse is also true. If the task has one clear rule — “reply with the sentiment label only” — examples add tokens and can introduce a pattern you did not intend.
The mechanism is pattern completion. The examples are tokens in the same context as the new input, so the model conditions on them exactly as it conditions on the instruction. It does not extract a rule and apply it; it continues the sequence. That is why a single inconsistent example can outweigh a sentence of guidance, and why the last example before the input has the most influence.
The model copies everything
An example is not just content; it is a template. If every example answer begins with “Sure!”, the model will begin with “Sure!”. If every example is positive, a neutral input may be labelled positive because that is the pattern in front of it.
That means examples have to be representative and balanced. Two examples of the same class teach the model that the class is common, which is a claim about your data you probably did not mean to make.
A few-shot prompt
EXAMPLES = [
("The delivery never arrived.", "complaint"),
("How do I change my address?", "question"),
("Thanks, that fixed it!", "praise"),
]
prompt = "Classify each message as complaint, question or praise.\n\n"
for text, label in EXAMPLES:
prompt += f"Message: {text}\nLabel: {label}\n\n"
prompt += f"Message: {incoming}\nLabel:"Build the prompt from examples
Run the builder, then add a fourth example whose label is “question”. Confirm the prompt still ends with an open “Label:” for the model to complete.
The order and the ending matter
Examples are read as a sequence, and the last one before the input has the most influence. Put the example closest to the test item in shape and label, and end with the token you want the model to produce. A classification prompt should finish with “Label:” and nothing after it; an extraction prompt should finish with the opening brace of the object.
A common mistake is adding explanations to the examples. “The delivery never arrived, so this is a complaint because the customer is unhappy” teaches the model to write explanations, and the next reply includes one even though the instruction asked for a bare label. Keep the examples in exactly the format you want back, including the silence after the final cue.
Choosing examples at runtime
A fixed set of examples is fine for a stable task. When the inputs vary widely, picking the examples most similar to the current input helps: a question about refunds is better taught by refund examples than by a random sample.
Similarity is usually lexical or embedding-based, and it has the same failure as retrieval — it can pick examples that are similar in wording and different in the thing being labelled. Check the selection on a labelled set before trusting it.
A concrete failure: the query “when will my refund arrive” retrieves the complaint “refund not received after ten days” because the words overlap, and the model copies the complaint label for a question. The fix is not a better similarity metric alone; it is to keep the label balanced across the retrieved set and to check the selection against a labelled sample.
Never include the test case itself as an example. It looks like a spectacular accuracy improvement and measures nothing.
How many is enough
Two or three well-chosen examples cover most formatting tasks. Ten are useful when the label set is large or the edge cases are subtle. Beyond that, the prompt grows faster than the accuracy, and every example is paid for on every call.
The number to tune is not “how many can fit” but “how many until the failures stop being about format and start being about judgement”. Those need different fixes.
One more signal: count the examples by label before you ship. If the counts are uneven, either add examples until they are not, or write down why the imbalance reflects the traffic. An imbalance nobody wrote down is an accident, and the model will treat it as a fact about the world.
Pick the nearest examples
The selector chooses examples by word overlap. Run it for two inputs and see how the chosen examples change; then add an example that is lexically close but labelled differently.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
When is a few-shot example better than an instruction?
