Prompting and context

What is few-shot prompting?

Few-shot prompting means including a small number of worked examples, pairs of inputs and the outputs you want, inside the prompt before the real input. The model picks up the pattern from the examples and applies it, which is often the fastest way to get a consistent format, tone or labelling scheme without any training.

3 min read·Checked ·Also called few-shot learning, in-context learning, zero-shot prompting

Examples communicate what descriptions cannot. Explaining the tone you want can take a paragraph and still miss; showing two outputs in that tone takes seconds and usually works.

What is the difference between zero-shot and few-shot prompting?

Zero-shot prompting gives the model only instructions. Few-shot prompting gives instructions plus a few examples of the task done correctly. One-shot means a single example.

StylePrompt containsGood for
Zero-shotInstructions onlyCommon tasks the model already does well
One-shotInstructions and one exampleShowing a format quickly
Few-shotInstructions and roughly 2 to 8 examplesConsistent labels, tone, formats and tricky edge cases

The term comes from the 2020 GPT-3 paper, "Language Models are Few-Shot Learners", which showed that a large enough model could perform new tasks such as translation and question answering just from examples in its prompt, with no retraining. This ability is also called in-context learning.

What does a few-shot prompt look like?

Here is a prompt that classifies customer messages, with three examples:

Classify each customer message as billing, bug, feature_request or other.
Return only the label.

<example>
Message: I was charged twice this month.
Label: billing
</example>
<example>
Message: The export button does nothing on Safari.
Label: bug
</example>
<example>
Message: Could you add dark mode?
Label: feature_request
</example>

Message: {customer_message}
Label:

The examples do three jobs at once: they fix the label names, show that only the label should be returned, and show where the boundaries between categories lie.

How do you choose good examples?

Choose examples that are varied, realistic and representative of the hard cases, because the model copies what it sees, including accidents.

  • Do: Cover the range of real inputs: short and long, clean and messy
  • Do: Include at least one tricky or borderline case with the right answer
  • Do: Keep the format identical across examples, since the model copies it exactly
  • Do: Wrap examples in clear tags so they are not confused with the real input
  • Avoid: Use examples that all share an accidental feature, such as the same length or topic, or the model may copy that too
  • Avoid: Put all examples of one label first and another label last; order can bias the answer
  • Avoid: Use examples that contradict your written instructions

Research published in 2022 by Min and colleagues found something surprising: models often gained much of the benefit of examples even when the example labels were randomly wrong, because the examples mainly taught the format, the label set and the kind of input to expect. Correct labels still help, but the lesson for engineers is that the structure of your examples carries a lot of weight.

When should you use few-shot prompting instead of fine-tuning?

Use few-shot prompting first. It needs no training, you can change examples in seconds, and a handful of good examples often gets you most of the way. Move to fine-tuning when you need hundreds of examples to capture the behaviour, when the examples make every prompt long and expensive, or when a smaller fine-tuned model could replace a larger prompted one.

Examples are sent on every request, so they add to token cost. Keeping them at the start of a stable prompt lets prompt caching reduce that cost.

Does few-shot prompting work with reasoning models?

It works, but use it for the output rather than the thinking. With reasoning models, examples that show a rigid step-by-step method can constrain the model's own better approach. Examples of the final answer format and of how to handle edge cases remain useful.

Frequently asked questions

How many examples should a few-shot prompt have?

Often three to five examples are enough. Add more when outputs are still inconsistent or when there are several edge cases to show, and check with an eval whether each extra example actually helps.

What is in-context learning?

In-context learning is a model's ability to pick up a new task from instructions and examples in its prompt, without any change to its weights. Few-shot prompting is the most common way to use it.

Is few-shot prompting the same as fine-tuning?

No. Few-shot prompting puts examples in the prompt for each request and does not change the model. Fine-tuning trains the model on examples so the behaviour becomes part of its weights.

Can bad examples hurt performance?

Yes. The model copies patterns in the examples, including mistakes, odd formatting and accidental biases such as always choosing the same label. Review examples as carefully as instructions.

Last checked for accuracy on . Written by the solidcoder team.

Go deeper in the free guides