Concepts / Choosing Representative Examples for Prompts

Choosing Representative Examples for Prompts

Zero-shot means no examples, one-shot means one example, and few-shot means a small number of examples placed inside the prompt. The examples are part of the input text, not a change to the model.

  • Programming

When One Instruction Is Not Enough

Imagine you want a language model to sort customer messages into three groups: billing, technical problem, or general question. The simplest approach is a single instruction such as 'Label each message as billing, technical, or general.' Sometimes that works well. Other times the model returns labels in a different style than you wanted, uses a word you did not expect, or treats borderline messages inconsistently.

A natural fix is to show the model a few messages that have already been labeled correctly, placed right before the new message you want labeled. The model then sees the pattern in those examples and continues it. Improving a model's behavior by placing demonstrations inside the prompt is the subject of this article. The key idea is that the examples change what the model produces without any change to the model itself.

Shots, Zero-Shot, and Few-Shot Prompts

Definition

A shot is one example placed inside a prompt. A prompt that contains examples is called few-shot learning, and a prompt that contains none is called zero-shot learning.

The number of examples in the prompt is what distinguishes these cases. A zero-shot prompt has zero examples and relies on the instruction alone. A 1-shot prompt contains one example. A 3-shot prompt contains three. Each example is an input paired with the output that should follow it, so the prompt demonstrates the task rather than only describing it.

Key point

Zero-shot means no examples, one-shot means one example, and few-shot means a small number of examples placed inside the prompt. The examples are part of the input text, not a change to the model.

Tracing How a Few-Shot Prompt Is Assembled

A few-shot prompt is usually assembled in a predictable order. It begins with an instruction that describes the task. Next come the examples, one after another, each showing an input and the output that should follow it. The new input you actually want processed comes last, with its output left blank for the model to complete.

The model reads the whole sequence as one block of text and produces a continuation. Because the examples sit inside the same text, the model can compare the new input against them as it writes. Follow the flow in the diagram below, from the components you assemble to the output the model returns.

Instructiondescribes the taskExample 1input and correct outputExample 2input and correct outputNew inputoutput left blankModelreads the whole text as oneblockOutputcontinues the demonstratedpatternfollowed byfollowed byfollowed bysent as one textgenerates
Where do the examples travel in a request, and how do they influence the output while the model's weights stay unchanged?

Notice what does not appear in this flow: any step that updates the model's underlying parameters. The examples travel as text in the request, and the model's behavior on this request is shaped by that text alone.

EASY

Using the customer-message task, write out the order of a few-shot prompt with two labeled messages. Name which piece is the instruction, which pieces are examples, and which piece is left for the model to complete.

Hints
  • The instruction comes first and describes the three labels.
  • The new message you want labeled comes last with its label left blank.

The Fixed Window of Context Length

A model can only read so much text at once. The maximum amount of input it can accept is called its context length. Every example, the instruction, and the new input must fit inside that limit together. The model's output also needs room, so the same space is shared by the answer it will write.

This is why the number of examples you can use is bounded. Adding examples does not stack indefinitely. Once the prompt reaches the model's maximum context length, there is no room for further demonstrations. The diagram below represents a prompt that fills the whole available input. The instruction and examples occupy most of the space, leaving only a small remainder for the new input and the answer.

Instructiontask descriptionExample 1input and output pairExample 2input and output pairExample 3input and output pairNew inputmessage to be labeledAnswer spacesmall remainder for output
How do the instructions, the examples, and the new input share a fixed-size window, and what happens when the examples no longer fit?

Zero-Shot Versus Few-Shot Trade-Offs

The two approaches differ in how much work the prompt does before the model begins. A zero-shot prompt is shorter and cheaper to send, but it asks the model to infer the exact style you want from the instruction alone. A few-shot prompt spends more of its length on demonstrations, which tends to narrow the range of acceptable answers. The comparison below shows how the number of examples changes what the prompt contains.

Instructiontask descriptionInstructiontask descriptionInstructiontask descriptionZero examplesstyle inferred frominstructionOne exampleone input and output pairThree examplesthree input and outputpairsNew inputoutput left blankNew inputoutput left blankNew inputoutput left blank
What does the prompt contain in a zero-shot prompt versus a 1-shot or 3-shot prompt, and how does the count of examples change?
FeatureZero-shot promptFew-shot prompt
Number of examplesZeroOne or more (for example 1-shot or 3-shot)
Prompt lengthShorterLonger, because demonstrations add text
Cost to sendLowerHigher, since inference cost rises with prompt length
How style is communicatedInferred from the instruction aloneShown directly through demonstrated input and output pairs
Effect on acceptable answersWider range possibleTends to narrow the range of acceptable answers

Trade-offs between zero-shot and few-shot prompting

Why Each Example Raises Inference Cost

Every token the model processes has a cost, and the input prompt is part of that processing. The source describes this as inference cost: the cost of processing a model's input when it generates a response. Each added example makes the prompt longer, so adding examples also raises inference cost.

Prompt lengthinstruction and new inputonlyPrompt lengthinstruction, threeexamples, new inputInput processedshortInput processedlonger
How does the amount of work per request change when the prompt grows from zero examples to several examples?

This trade-off matters most when the same prompt is sent thousands of times. A modest increase per request can become a large increase in total. Before committing to a design, it helps to estimate how much each added example will cost across all the requests your application will make.

Benefits Differ Across Models and Tasks

The benefit of examples is not uniform across models. The source reports that for GPT-3, few-shot learning showed significant improvement over zero-shot learning. In the use cases analyzed in Microsoft's 2023 study, however, few-shot learning gave only limited improvement over zero-shot learning on GPT-4 and a few other models. The source interprets this as a sign that more powerful models follow instructions better, so they can perform well with fewer or no examples.

What do you think happens?

If a prompt already has three examples and the output is good, will a fourth example necessarily make the output better?

  • Yes, more examples always improve output

  • Not necessarily, gains can level off while cost keeps rising

Reveal answer
Answer:

Not necessarily. Gains can level off while inference cost keeps rising, so the best number of examples is found by experiment rather than assumed.

Each added example lengthens the prompt and increases inference cost, and the source notes that the best number of examples is found by experiment. Past a certain point, extra examples may add cost without a matching gain.

The source also cautions that the study might have underestimated the value of examples in domain-specific work. Its illustration is the Ibis dataframe API. If a model has seen few examples of that API in its training data, including Ibis examples in the prompt can still make a large difference, even for a model that otherwise performs well with zero-shot prompts. The lesson is that a result from one set of use cases does not settle the question for your own application.

Mistakes Beginners Make With Example Counts

  • Common mistake

    Assuming more examples always gives better results

    What it looks like

    Adding a fourth and fifth example to a prompt that already works, without checking the output.

    Why it fails

    Each example lengthens the prompt and increases inference cost, and the gains can level off. The source notes the best number of examples is found by experiment rather than assumed.

    Do this instead

    Compare outputs with different example counts on the same test inputs, and keep the smallest number that gives acceptable results.

  • Common mistake

    Forgetting that examples must share the context window with the answer

    What it looks like

    Packing so many demonstrations into the prompt that the new input is squeezed into the last few lines.

    Why it fails

    Instructions, examples, the new input, and the output all have to fit inside the maximum context length together.

    Do this instead

    Budget the window before you begin: reserve room for the new input and the answer, then fit as many examples as remain.

  • Common mistake

    Treating a result from one task as proof for every task

    What it looks like

    Concluding that examples are unnecessary because a zero-shot prompt worked well on a different kind of request.

    Why it fails

    The source reports that the benefit of examples varies across models and use cases, and that a domain-specific case such as the Ibis dataframe API can still benefit a great deal.

    Do this instead

    Test zero-shot and few-shot prompts on your own application's inputs before choosing a design.

  • Common mistake

    Placing the new input before the examples instead of after them

Practice Choosing Your Examples

MEDIUM

You want a model to label customer messages as billing, technical, or general. A zero-shot prompt labels most messages correctly, but borderline messages are inconsistent. Describe how you would decide between a 1-shot, a 3-shot, and a larger few-shot prompt, and name the two constraints you would check before adding more examples.

Hints
  • Think about what a few-shot prompt is trying to fix: the inconsistent borderline cases.
  • The two constraints are the maximum context length and inference cost per request.

Key Takeaways

  • A shot is one example placed inside a prompt. A prompt with examples is few-shot learning, and a prompt with none is zero-shot learning.
  • Examples change what a model produces by appearing in the prompt text, and the model's parameters are not retrained.
  • Maximum context length is a fixed ceiling on the instruction, examples, new input, and output together, which bounds how many examples fit.
  • Each added example lengthens the prompt and raises inference cost, so the best number of examples is found by experiment rather than assumed.
  • The benefit of examples varies across models and tasks, so test zero-shot and few-shot designs on your own inputs.

Related concepts

More concepts starting with C