Concepts / Writing Clear Instructions for Language Models

Writing Clear Instructions for Language Models

The examples only change the text the model is given.

  • 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, rather than changing the model itself, is the focus of this article.

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.

Tracing a Prompt from Parts to Output

A few-shot prompt is usually assembled in a predictable order. It begins with an instruction that describes the task. Next it lists 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 that same text, the model can compare the new input against them as it writes. The diagram below traces this flow from the components you assemble to the output the model returns.

Task instructiondescribes the taskExample 1input and labeled outputExample 2input and labeled outputNew inputoutput left blankUnchanged modelweights not updatedContinuationcompletes the patternthenthenthenread as one textgenerates
How does adding examples to the prompt flow through an unchanged model to produce a different output, with no weight updates?

The key point is that nothing inside the model changes. The examples only change the text the model is given. The model's underlying parameters stay the same, and the different output comes entirely from the pattern that the prompt demonstrates.

Zero-Shot and Few-Shot Prompts

The two approaches differ in how much work the prompt does before the model begins. The difference is easiest to see by comparing what each prompt contains and what each one asks the model to figure out on its own.

Comparing the Trade-offs

The comparison below lays out the trade-offs that follow from the difference described above. A zero-shot prompt is the lighter option. A few-shot prompt gives the model more guidance at the price of a longer prompt.

Instruction onlytask described in wordsInstruction plusexamplestask shown by demonstration0 exampleszero shotsSeveral examplesfor example 3 examples = 3shotsShorter promptcheaper to sendLonger promptnarrower range of answers
What is the difference between a prompt with no examples and one with several, and how many shots does each contain?
FeatureZero-shot promptFew-shot prompt
Number of examplesNone (zero)One or more (for example, 3 examples is 3-shot)
Prompt lengthShorterLonger, because demonstrations add text
Style guidanceInferred from the instruction aloneShown through concrete demonstrations
Cost per requestLowerHigher, since examples are resent with each call
Range of answersBroaderTends to be narrower

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

Why the Context Length Sets a Ceiling

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, instruction, and new input must fit inside that limit together, and the model's output must fit as well. 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 available input space, with the instruction and examples occupying most of the space and only a small remainder left for the new input and the model's answer.

Instructiontask descriptionExample 1input and output pairExample 2input and output pairExample 3input and output pairNew inputtext to processOutputmodel's answer
How do the instructions, examples, new input, and expected output share one fixed-size context window, and what gets crowded out as examples are added?

Read the diagram as one row of fixed space. Each added example takes a bigger slice of that row. The instruction, the new input, and the output all still need room, so the more examples you add, the less room remains for everything else. The source describes the constraint simply: instructions, examples, the new input, and the output must all fit.

Tracing How Examples Raise the Cost

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

Worked example

Measuring the cost of a prompt that is resent

A team sends the same labeling prompt thousands of times a day. The instruction and the new message together take a fixed amount of space. Each example adds more. The illustrative units below are not real token counts; they show how the total grows.

  1. 1
    Measure the fixed part

    Assume the instruction plus the new message together take 2 units of prompt space. This part is sent on every call whether or not examples are used.

  2. 2
    Measure a zero-shot call

    With zero examples, each call sends 2 units in total.

  3. 3
    Measure a three-shot call

    Assume each example takes 2 units. Three examples add 6 units, so each call now sends 8 units in total.

  4. 4
    Multiply by volume

    Because the prompt is resent with every request, the extra 6 units are paid again on every call. Across thousands of requests, a modest per-call increase becomes a large total increase.

Result

The three-shot prompt sends four times as much text per call as the zero-shot prompt in this illustration, and that difference repeats on every request.

This is why the cost trade-off matters most for prompts that are sent at high volume. The best design is not settled by intuition alone. The source notes that the best number of examples is found by experiment rather than assumed.

Why More Examples Are Not Always Better

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 a 2023 Microsoft 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.

Returns from adding examples diminish. The first few demonstrations may clarify the task a great deal, while later ones add cost and length with little additional gain. Once the extra examples cost more in length and money than they improve the output, they are no longer the best choice.

Mistakes When Choosing an Example Count

  • Common mistake

    Assuming more examples always gives better output

    What it looks like

    Adding a tenth labeled message to a prompt that already works well, then paying for the longer prompt on every call.

    Why it fails

    Returns diminish, and each added example lengthens the prompt and raises inference cost.

    Do this instead

    Start with a small number of examples and measure whether extra examples change the results enough to justify their cost.

  • Common mistake

    Forgetting the context length limit

    What it looks like

    Pasting in long examples until the prompt leaves no room for the new message or the answer.

    Why it fails

    Every example, instruction, new input, and output must fit inside the model's maximum context length together.

    Do this instead

    Count the space the instruction and new input need first, then add examples only while room remains for the output.

  • Common mistake

    Judging examples by results from another model or task

    What it looks like

    Dropping examples from a prompt because a published study found little benefit on a different model.

    Why it fails

    The benefit of examples differs across models, and the source notes that domain-specific tasks can benefit more than a general study suggests.

    Do this instead

    Test with and without examples on your own task and your own model.

  • Common mistake

    Putting the new input before the examples, which breaks the pattern the model is supposed to continue

Designing a Few-Shot Prompt

Work through the customer message task. Suppose your zero-shot prompt keeps returning the label 'support' instead of 'technical problem'. You decide to add examples. Before you run anything, plan the prompt in the order the source describes: instruction, examples, then the new input with its output left blank.

What do you think happens?

If you add three correctly labeled messages to the zero-shot prompt, what kind of prompt is it, and how many shots does it contain?

Reveal answer
Answer:

It is a few-shot prompt, and it contains three shots.

A shot is one example placed inside the prompt, and a prompt with examples is few-shot learning. Three examples means three shots.

MEDIUM

Write a few-shot prompt for the customer message task with two labeled examples and one new message whose label is left blank. Then explain which part of the context window would fill up first if you added twenty more examples, and why the output might no longer fit.

Hints
  • Start with the instruction, then add examples one after another, each showing an input and its label.
  • Place the new message last with its label blank.
  • Think about the context length: every example, the instruction, the new input, and the output must all fit together.

Key Takeaways

  • Examples placed inside a prompt change the model's output without any retraining, because the model continues the pattern the prompt demonstrates.
  • A zero-shot prompt contains no examples, while a few-shot prompt contains examples, and the number of examples is the number of shots.
  • Context length is a fixed ceiling: instructions, examples, the new input, and the output must all fit inside it together.
  • Each added example lengthens the prompt and raises inference cost, and returns diminish, so the best number of examples is found by testing on your own task.

More concepts starting with W