Few-shot prompting

Few-shot prompting is giving a language model a few worked examples of a task inside the prompt, so it can follow the pattern for a new case without any retraining.

Instead of only describing what you want, you show it. Google's machine learning glossary uses a simple example: a prompt asks for the official currency of a country, gives two answered cases, France: EUR and United Kingdom: GBP, then ends with India:. The model sees the pattern and completes it. Google notes that few-shot prompts generally give better results than zero-shot or one-shot prompts, at the cost of a longer prompt.

The setting was defined and named in OpenAI's GPT-3 paper in May 2020. The authors gave the model a number of examples, each a context and its desired completion, followed by one final context for the model to complete, and allowed no updates to the model's weights. They typically used between 10 and 100 examples, as many as fitted into GPT-3's 2,048-token context window. The paper's title, Language Models are Few-Shot Learners, made the point that a large enough model could pick up many tasks this way.

What the model takes from the examples is mostly form. In a 2022 study, Sewon Min and colleagues replaced the correct answers in demonstrations with random ones and found performance barely dropped across 12 models on classification and multiple-choice tasks. The examples mattered mainly because they showed the range of possible answers, the kind of input to expect and the format of the response. That makes few-shot prompting especially good for steering output format, tone and structure.

Current guidance reflects this. Anthropic's prompting documentation suggests three to five examples that closely match the real use case, vary enough to cover edge cases, and are clearly marked off from the instructions. Varied examples matter because a model may copy incidental features, such as length or phrasing, that all the examples happen to share. OpenAI's guidance for its reasoning models is to try a prompt with no examples first and add them only when the output requirements are complex.

Every example adds tokens, and they are sent with every request, so a long few-shot prompt costs more and takes slightly longer to process. For a task repeated thousands of times with a fixed format, a few good examples are often cheaper than fine-tuning; for very large or changing sets of examples, retrieval or fine-tuning can be the better tool.

Common questions

What is an example of few-shot prompting?

A prompt that says "Classify the sentiment of each review", then shows three reviews each labelled positive or negative, and ends with a new unlabelled review. The model follows the pattern and labels the new one. The examples show both what the task is and what the answer should look like.

How many examples should a few-shot prompt include?

Anthropic recommends three to five for its models. The GPT-3 paper in 2020 used 10 to 100, as many as fitted in that model's context window. Quality and variety matter more than quantity, and for its reasoning models OpenAI suggests trying no examples first.

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

Zero-shot gives only the instruction, one-shot adds a single example and few-shot adds several. More examples usually make the expected output clearer, but they lengthen the prompt and raise the risk that the model copies features of the examples you did not intend.

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

No. Few-shot prompting puts examples in the prompt and leaves the model unchanged, so the effect lasts only for that request. Fine-tuning trains the model on examples and permanently changes its weights.

Finding more

Search for "few-shot prompting", "multishot prompting" or "in-context examples" to find guidance and research. The prompting guides published by model makers are the most current source for how many examples to use with a given model, and research papers such as the GPT-3 paper and studies of in-context learning explain why the technique works. Advice that worked for older models may not suit newer ones, so test with your own data.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.