Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Objectives: LO-5.1, LO-5.2, LO-5.3
Prerequisites: Module 4
Time: about 6 hours: 2 for this reading, 2.5 for Lab 5, and 1.5 for the practice

Module 3 described the output a prompt wants, and Module 4 tagged the data. This module shows the output instead, with examples: inputs paired with the outputs you want for them. You will write examples that cover the range of inputs and labels, in tags that set them apart from the task. You will audit an example set for a skewed balance of labels and for surface features a model could copy. Last, you will choose examples for each input from a pool by similarity, and compare that choice with a fixed set. This module begins Part 2 of the course; Module 7 shows how to measure what its techniques change.

What an example teaches

A demonstration, or example, is an input together with the output you want for it. Brown and colleagues named three settings when they tested GPT-3 in “Language Models are Few-Shot Learners.” Zero-shot gives the model only an instruction describing the task. One-shot adds one demonstration, and few-shot adds several. In every setting the model’s weights stay as they are: the task and its demonstrations reach the model only as text in the prompt. Learning from examples in the prompt this way is often called in-context learning.

Anthropic’s prompting best practices page calls examples one of the most reliable ways to steer an output’s format, tone, and structure. It asks for examples that are relevant, mirroring the real inputs, diverse, covering edge cases so the model doesn’t pick up unintended patterns, and structured, each in an <example> tag inside <examples> so the model can tell them from the instructions. It suggests three to five examples.

What do the examples teach? Min and colleagues, in “Rethinking the Role of Demonstrations,” replaced the labels in demonstrations with random ones. Performance barely dropped, across 12 models including GPT-3, on classification and multiple-choice tasks. What mattered more was what the demonstrations showed: the set of possible labels, the kind of input text, and the format of the whole sequence. The models in these papers are older than the current Claude models, so treat their findings as reasons to check your own examples, not as measurements of today’s models.

Worked example: a few-shot routing prompt

Here is Module 3’s routing task with three examples, one per queue, written in the tags the best-practices page suggests. The real ticket gets its own tag, after the examples:

<instructions>
Route the ticket to repair, billing, or account. Return exactly one line: Label: and the queue.
</instructions>
<examples>
  <example>
    <input>Chain skips under load in two gears</input>
    <output>Label: repair</output>
  </example>
  <example>
    <input>Can I get the receipt for Tuesday's repair?</input>
    <output>Label: billing</output>
  </example>
  <example>
    <input>Please remove the saved bike from my profile</input>
    <output>Label: account</output>
  </example>
</examples>
<ticket>Pedal creaks when I stand up</ticket>

The examples show all three labels, the exact output format, and the short, informal wording of real tickets: the three things Min and colleagues found demonstrations convey. They cost tokens on every request. Suppose the three examples and their tags come to 90 tokens. Across 10,000 requests on Claude Opus 5.5, at $4 per million input tokens (Module 2), they add 900,000 tokens, or $3.60. In Lab 5, Exercise L5.1 builds the <examples> section in code.

Contrast pairs teach the boundary

Examples that differ in an obvious way teach an obvious rule. The examples that teach most are the ones near a boundary between labels. A contrast pair is two inputs that share their surface but get different labels, so the only difference left is the one the rule cares about.

Worked example: two tickets about brakes

A useful pair for routing is My brake pulls left → Label: repair and I need the brake repair receipt → Label: billing. Both contain “brake,” and the second contains “repair” too. The pair shows that the label follows what the customer asks for, not which part the ticket names. An unrelated payment example, such as “Where can I pay the remaining balance?”, teaches the billing label but nothing about where billing ends and repair begins.

Put each example of the pair in its own <example> block, with separate input and output tags, and reserve a distinct input block, such as <ticket>, for the actual ticket. Exercise P5.1 in the practice asks for a contrast pair of your own.

Audit the set for skew and shortcuts

An example set can mislead in two ways. Its labels can be skewed, with one label far more common than the others. Or every example of one label can share a surface feature, a property of the wording, such as a greeting, a word, a price, or the length, that has nothing to do with the rule. A model that picks up the feature has learned a shortcut: it gets the examples right for the wrong reason, and fails on inputs where the feature and the label part ways.

Zhao and colleagues, in “Calibrate Before Use,” measured how much these choices matter for GPT-3. The choice of examples, their format, and even their order moved accuracy between near chance and near the state of the art. On a sentiment task with four examples and a 2.7-billion-parameter GPT-3, changing only the order of the examples could move accuracy from 54.3% to 93.4%. They traced the instability to biases toward answers that are frequent in the prompt (majority label bias), answers near its end (recency bias), and answers common in the model’s training data. More examples didn’t remove it: the variance stayed high even with 16.

Worked example: auditing six examples

A first draft of a routing set has six examples. Two are written for this example; the rest are tickets from data/tickets.json:

#InputLabel
1Chain skips under load in two gearsrepair
2One spoke snapped on the rear wheelrepair
3Please send the receipt for my tune-upbilling
4Brake lever feels soft after rainrepair
5Please refund the extra $12 chargebilling
6Rear tire goes flat after each riderepair
  1. Count the labels: repair 4, billing 2, account 0. The set is skewed toward repair, and it never shows the account label at all.

  2. Look for features one label owns: “please” appears in both billing examples and in no other. A model could learn “please means billing,” which T20, “Please remove the saved bike from my profile,” would break.

  3. Check the length range: every input has six or seven words, while the pool runs from six to ten.

  4. Check the order: the last example is repair, the label the set already favors, so the recency and majority label biases push the same way.

A fixed version keeps examples 1, 3, and 4. It replaces 2 and 6 with two account tickets, T20 and T21 (“I want to turn off appointment reminders”). It replaces 5 with T10, “Can I get the receipt for Tuesday’s repair?”, a billing example without “please” that mentions a repair. The order mixes the labels: repair, billing, account, repair, billing, account. Now each label has two examples, and “please” appears under billing and account. In Lab 5, Exercise L5.2 counts the labels and lengths of the whole pool, and practice Exercise P5.2 finds a missing case.

Choose examples for each input

A fixed set puts the same examples in every request. It is reviewed once, and it keeps the start of every prompt identical, which prompt caching needs (Module 10). Anthropic’s prompt caching guide says a cache hit needs the prompt identical up to the cached point, and that the current models cache only prompts of at least 512 tokens. A dynamic set chooses examples for each input from a pool, so a rare input can get an example close to it. Liu and colleagues, in “What Makes Good In-Context Examples for GPT-3?,” retrieved the training examples most similar to each test input, using a sentence encoder, and found that this consistently beat randomly chosen examples on the tasks they studied.

The labs measure similarity more plainly, with word sets. Lowercase the text and keep each run of letters once. The Jaccard similarity of two word sets AA and BB is the number of words they share divided by the number of distinct words in either:

J(A,B)=∣A∩B∣∣A∪B∣J(A, B) = \frac{|A \cap B|}{|A \cup B|}

For “repair chain” and “chain skips,” the shared set has one word and the union has three, so J=1/3J = 1/3. Two identical nonempty sets score 1, and two sets with no word in common score 0. When both sets are empty, the union is empty too, so the labs define J=0J = 0. To choose kk examples, sort the pool by decreasing similarity, break ties by a stable identifier, such as the ticket ID, so a replay picks the same examples, and keep the first kk.

Worked example: the nearest three examples for a new ticket

A new ticket arrives: “The chain skips and the rear brake rubs.” Its word set has seven words, {and, brake, chain, rear, rubs, skips, the}. The three highest-scoring tickets in the pool are:

TicketLabelShared wordsUnionJJ
T01, “Rear brake rubs after a wheel change”repairbrake, rear, rubs113/11 ≈ 0.27
T12, “The quote and the final charge differ”billingand, the112/11 ≈ 0.18
T02, “Chain skips under load in two gears”repairchain, skips122/12 ≈ 0.17

T05, “One spoke snapped on the rear wheel,” also scores 2/12, sharing “rear” and “the”; the tie goes to T02, whose ID comes first. T12 ranks second on two words, “and” and “the,” that say nothing about its request. Dropping such common words fixes that, but then the nearest three are T01, T02, and T05, all repair: a nearest-neighbor policy can return only one queue. A fixed, balanced set such as T02, T10, and T20 shows all three labels whatever the ticket says.

Neither policy is right for every task, so compare them. Inspect how much of the label space each policy’s choices cover, and try a policy that reserves slots for contrast cases, such as one example per label plus the two nearest. Evaluate the choices on test cases held apart from the pool. A test case left in the pool is its own nearest neighbor, with J=1J = 1, so the model would see the answer. In Lab 5, Exercises L5.3 and L5.4 build the word sets and the ranking, and compare the nearest set with a fixed one.

Common misconceptions

Summary

Self-check

Answer each question before you open its answer.

  1. (LO-5.1) What makes a brake receipt a useful example?

    Answer

    It shares repair vocabulary while requesting a billing action. Next to a brake repair ticket, it shows that the label follows the request, not the part the ticket names.

  2. (LO-5.1) Why put the real ticket in a different tag from the examples’ inputs?

    Answer

    So the model, and any code that checks the prompt, can tell the case to answer from the cases already answered. Anthropic’s page gives a related reason for the example tags themselves: they let Claude tell the examples from the instructions.

  3. (LO-5.2) Every account example begins with “Hi.” How do you test a shortcut based on the greeting?

    Answer

    Try account tickets without that greeting and repair and billing tickets with it. If the greeting decides the label, the first group gets other labels and the second gets account.

  4. (LO-5.3) What is the Jaccard score for two identical nonempty word sets?

    Answer

    One. Their intersection equals their union, so the fraction is a number divided by itself.

  5. (LO-5.3) A dynamic policy picks the three nearest examples for every ticket. Name one way its choices can be worse than a fixed, balanced set’s.

    Answer

    It can choose examples of one label only, as for the brake-and-chain ticket once common words are dropped, so the prompt never shows the other labels. It can also rank examples high for shared common words, as T12 was.

Further reading

References
  1. Min, S., & others. (2022). Rethinking the Role of Demonstrations: What Makes In-Context Learning Work? Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing, 11048–11064. 10.18653/v1/2022.emnlp-main.759
  2. Liu, J., Shen, D., Zhang, Y., Dolan, B., Carin, L., & Chen, W. (2022). What Makes Good In-Context Examples for GPT-3? Proceedings of Deep Learning Inside Out (DeeLIO 2022): The 3rd Workshop on Knowledge Extraction and Integration for Deep Learning Architectures, 100–114. 10.18653/v1/2022.deelio-1.10