Sub topic 2
What Few-Shot Prompting Is and Why It Works
What this chapter assumes
You should know that a large language model (LLM) turns a prompt into a completion, and you should be able to read short Python. You should also recognize the system, user, and assistant roles in chat prompts. The previous chapter showed where a zero-shot prompt, which has instructions and no examples, falls short. This chapter defines few-shot prompting and explains the mechanism behind it. Later chapters cover how to write, choose, order, and test examples.
The core idea
Few-shot prompting means placing a small number of worked input and output examples in the prompt, followed by a new input that you want the model to handle. The model reads the examples, infers the pattern, and continues it for the new input. The "shots" are the examples.
Here is a sentiment classification prompt with three examples and an unfinished fourth entry:
Classify the sentiment of each review as positive, negative, or neutral.
Review: The battery lasts all day and charging is fast.
Sentiment: positive
Review: It stopped working after a week.
Sentiment: negative
Review: The package arrived on Tuesday.
Sentiment: neutral
Review: The screen is bright but the speakers are weak.
Sentiment:The prompt has four parts:
- An instruction that states the task.
- Three complete examples, each with an input (
Review:) and an output (Sentiment:). - A final input that has the same shape as the examples.
- An output slot,
Sentiment:, left empty.
The model's most likely continuation of this text is a single label. It will probably be a word from the set seen in the examples, and it will not be a paragraph of commentary. Nothing in the instruction said "answer with one word." The examples carried that requirement.
Zero-shot, one-shot, few-shot
The terms differ only in how many examples the prompt contains.
| Term | Examples in prompt | Typical use |
|---|---|---|
| Zero-shot | 0 | The task is common and the instruction is enough |
| One-shot | 1 | You mainly need to show an output format |
| Few-shot | 2 to about 10 | The task, labels, or style need to be shown |
| Many-shot | dozens or more | Long-context models, hard or unusual tasks |
There is no sharp boundary between these. "Few-shot" is the common name for the whole technique of teaching by example inside a prompt.
Why it works: in-context learning
The model's weights (its trained parameters) do not change when you add examples. Nothing is saved after the call, and the next request starts from the same model. What changes is the context, which is the text the model conditions on while it generates. This behavior is called in-context learning: the model adapts to a task from information in the prompt alone.
The mechanism is easiest to see from how a language model generates text. At each step it predicts the next token (a word or word fragment) given everything before it. During training on very large amounts of text, the model saw many documents with repeated structure: lists, question and answer pages, tables, glossaries, and dialogues. To predict well in such documents, it had to notice a pattern from earlier entries and continue it. A few-shot prompt gives the model a document of exactly that kind. The most probable continuation of "three labeled reviews, then a fourth review" is a fourth label in the same format.
What the examples communicate
Examples carry several kinds of information at once, and this is why a handful of them often beats a long paragraph of instructions.
- The task. The mapping from input to output is shown, not described. The model can see that "It stopped working after a week" maps to a negative label.
- The label space. The model sees the allowed outputs. In the example above, the only labels are
positive,negative, andneutral. It sees nomixedand nobad. - The output format. Capitalization, punctuation, length, and structure are all visible. If every example ends with a single lowercase word, the completion tends to do the same.
- The style and register. Tone, vocabulary, and level of detail carry over. This matters for tasks like rewriting, summarizing, or writing support replies.
- The boundary of the task. Examples show where one input ends and the output begins, which is the role of the delimiters (the
Review:andSentiment:markers).
Some research on in-context learning suggests that format and label space explain a large share of the benefit. Models can still do reasonably well when example labels are partly wrong, as long as the format and the set of labels look right. Do not take this as permission to be careless. It shows that examples work partly by setting expectations, not only by teaching a rule. Correct labels still matter for the cases that need real discrimination, and wrong labels make the prompt harder to debug.
A minimal working builder
You will usually construct few-shot prompts in code, not by hand. This function assembles the text prompt shown earlier from a list of (text, label) pairs. It runs as shown and needs no API key, because it only builds the string.
INSTRUCTION = "Classify the sentiment of each review as positive, negative, or neutral."
EXAMPLES = [
("The battery lasts all day and charging is fast.", "positive"),
("It stopped working after a week.", "negative"),
("The package arrived on Tuesday.", "neutral"),
]
def build_prompt(instruction, examples, query):
parts = [instruction, ""]
for text, label in examples:
parts.append(f"Review: {text}")
parts.append(f"Sentiment: {label}")
parts.append("")
parts.append(f"Review: {query}")
parts.append("Sentiment:")
return "\n".join(parts)
if __name__ == "__main__":
print(build_prompt(INSTRUCTION, EXAMPLES, "The screen is bright but the speakers are weak."))Two details are worth noticing. The same two lines are produced for every example, so the format cannot drift between entries. The prompt also ends with Sentiment: and no label, which leaves the model one obvious thing to write. The next chapters build on this structure.
Few-shot compared with the alternatives you already know
Few-shot prompting is one of several ways to steer a model, and it helps to place it among them.
| Approach | What changes | Cost to change behavior | Persists across calls |
|---|---|---|---|
| Zero-shot instruction | The prompt text | Edit a sentence | Only if you resend it |
| Few-shot examples | The prompt text | Edit or swap examples | Only if you resend it |
| Fine-tuning | The model weights | Collect data and train | Yes |
Few-shot prompting sits between a bare instruction and fine-tuning. It is faster to iterate on than fine-tuning because you edit text and rerun. It is more expressive than an instruction when the behavior is easier to show than to describe. The last chapter of this tutorial covers when to move to a different approach.
Limits to keep in mind from the start
Few-shot prompting is not free or guaranteed, and several limits follow directly from how it works.
- Examples cost tokens on every call. The model only sees what you send, so each request carries all of the examples. Ten long examples in a prompt used thousands of times add up in both cost and latency.
- The context window is finite. Examples compete for space with the instruction, the input, and the output.
- The model imitates what it sees. If every example is short, the answers tend to be short. If all your examples share an accidental trait, the model may treat that trait as part of the task.
- Nothing is learned permanently. A pattern that works in one call must be present in the next.
- It cannot add knowledge the model lacks. Examples show how to do a task. They do not teach facts the model never encountered. A model that does not know your internal product codes will not learn them from three examples.
When few-shot is a good first move
Reach for few-shot prompting when these conditions hold:
- The task is easier to demonstrate than to describe, such as a specific labeling scheme, a house style, or a structured output format.
- Zero-shot output is roughly right but inconsistent in format or labels.
- You have a handful of reliable examples, or can write them quickly.
- You are still iterating, so retraining a model would be premature.
Skip it, or combine it with something else, when a clear one-line instruction already gives the right behavior, when the task needs multi-step reasoning that examples alone do not expose, or when the prompt would need so many examples that cost becomes the main problem.
Where this goes next
The rest of the tutorial develops each piece of this picture. You will look at what makes one example better than another, how to keep format and wording identical, how to express examples as chat messages, and how to pick, count, order, and balance them. You will also learn how to teach edge cases, add reasoning steps, choose examples per query, and measure whether any of it helps.
Exercise
A teammate says, "Adding examples to a prompt fine-tunes the model on our task, so after a few calls the model will remember the pattern." Explain what is wrong with this claim, and name two kinds of information that the examples in the sentiment prompt give the model.
Show answer
Few-shot prompting does not change the model's weights, so nothing is learned or remembered between calls. The examples only become part of the context for that one request, and the model continues the pattern it sees there. If the pattern is absent from the next request, the model will not apply it. The examples convey, for instance, the label space (positive, negative, neutral), the output format (one lowercase word after "Sentiment:"), and the mapping from review text to label. Any two of these is a correct answer.
Exercise
You are classifying support tickets as billing, technical, or other. A zero-shot prompt returns correct labels, but sometimes wraps them in full sentences such as "This ticket seems to be about billing." Why might adding two or three examples fix this more reliably than adding the sentence "Only output the label"? What should you still keep in the prompt?
Show answer
The examples show the exact output format instead of describing it. If every example ends with a bare label such as "billing", the most likely continuation is also a bare label, so the format is demonstrated and no longer depends on the model following a written rule. You should still keep the instruction that states the task and the allowed labels, because the instruction defines the goal and the examples fill in the details.