Zero-Shot and Few-Shot Prompting
Zero-shot and few-shot prompting are techniques for instructing a large language model (LLM) to perform a task either with no examples at all (zero-shot) or with only a handful of demonstrations embedded directly in the prompt (few-shot), without updating the model's weights. These techniques matter enormously in modern AI because they allow developers to repurpose a single pretrained model for thousands of different tasks at inference time, slashing the need for expensive labeled datasets and fine-tuning runs. The remarkable fact: a model given just three well-chosen examples in its prompt can match or exceed the accuracy of a model trained on thousands of labeled examples for that same task.
A large language model has already compressed vast knowledge about language, reasoning, and the world into its weights during pretraining — zero-shot and few-shot prompting are simply clever ways of unlocking that knowledge by framing your request in the right context. Think of the model as an incredibly well-read intern: zero-shot is handing them a task description and trusting their background, while few-shot is also slipping them two or three solved examples so they pattern-match to exactly the format you need. The "aha moment" is realizing you are not teaching the model anything new — you are steering it toward a capability it already possesses.
How It Works
At the most basic level, prompting is just text concatenation. You build a string that contains some combination of a system instruction, optional examples, and the actual input you want the model to reason about. The model then predicts the most likely continuation of that string — and if your prompt is well-constructed, that continuation is exactly the output you wanted.
The Anatomy of a Prompt
A prompt has three logical zones: ``` [System / Task Instruction] [Few-shot examples — zero or more] [Live input to process] ``` In zero-shot mode the middle zone is empty. In few-shot mode it holds `k` complete input→output pairs.
Probability Framing
When an LLM generates an answer, it is computing a conditional probability over all possible output tokens given the prompt tokens. We can write this compactly:
Explanation: The model scores the full output sequence y by multiplying together the probability of each output token, conditioned on all previous tokens, the live input x, and the context C (your instruction plus examples).
Symbol guide:
- — the output sequence (e.g., the label "positive")
- — the live input (e.g., the sentence to classify)
- — the context, which is your instruction and any few-shot examples
- — the -th token of the output
- — all output tokens generated before position
Adding few-shot examples changes , which shifts the entire probability distribution toward outputs that match the demonstrated pattern — without ever touching the model weights.
In-Context Learning Score
For classification tasks, a useful way to think about how few-shot examples help is through the in-context learning objective. Given examples , the model selects the label that maximises:
Explanation: Among all possible labels in the label set Y, pick the one the model considers most probable given the live input and all k demonstration pairs.
Symbol guide:
- — the predicted label
- — the set of valid output labels (e.g., {positive, negative})
- — the number of few-shot examples (0 for zero-shot)
A Minimal Python Example
Here is the full zero-shot → few-shot progression using OpenAI's API: ```python from openai import OpenAI client = OpenAI() # uses OPENAI_API_KEY from environment
# ── Zero-shot ────────────────────────────────────────────────────────────── zero_shot_prompt = """ Classify the sentiment of the following review as POSITIVE or NEGATIVE. Reply with a single word only. Review: "The battery died after two hours. Completely useless." Sentiment:""" resp = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": zero_shot_prompt}], max_tokens=5, ) print("Zero-shot:", resp.choices[0].message.content.strip())
# → "NEGATIVE"
# ── Few-shot (3 examples) ────────────────────────────────────────────────── few_shot_prompt = """ Classify the sentiment of a review as POSITIVE or NEGATIVE. Reply with a single word only. Review: "Absolutely love it, works perfectly." Sentiment: POSITIVE Review: "Stopped working after one week. Waste of money." Sentiment: NEGATIVE Review: "Decent quality for the price, very happy." Sentiment: POSITIVE Review: "The battery died after two hours. Completely useless." Sentiment:""" resp = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": few_shot_prompt}], max_tokens=5, ) print("Few-shot:", resp.choices[0].message.content.strip())
# → "NEGATIVE" ``` The only difference between the two calls is the text inside the prompt. The model is identical, the weights are identical — only the context changes.
How Many Examples Is "Few"?
The term "few-shot" is deliberately loose. In practice:
| Name | Examples (k) | When to use |
|---|---|---|
| Zero-shot | 0 | Task is simple or well-known to the model |
| One-shot | 1 | Format is unusual; one demo clarifies it |
| Few-shot | 2–10 | Task has subtlety or non-standard output |
| Many-shot | 10–100+ | Long context models; complex structured output |
Beyond roughly 10 examples the gains per additional example diminish rapidly, and you start to risk hitting context window limits.
Tom Brown led the team at OpenAI that produced GPT-3, the paper that introduced "few-shot learners" as a central concept in the NLP literature. His 2020 paper "Language Models are Few-Shot Learners" demonstrated that a 175-billion-parameter model could perform competitively on dozens of benchmarks with no gradient updates — just carefully constructed prompts. Surprisingly, the GPT-3 paper almost wasn't about prompting at all: the in-context learning results were added late in the project when researchers noticed the large model generalised in an unexpected way during evaluation.
Intuition
Imagine you have just hired a new contractor who has read every book, manual, and forum post ever written — they are extraordinarily knowledgeable but have never worked at your specific company.
Zero-shot is like sending them an email that says: "Please write a SQL query that returns all users who signed up last month." A knowledgeable person can probably do this from the description alone, because SQL and "last month" are concepts they already understand deeply.
Few-shot is like adding to that email: "Here are two examples of queries our team has written before, so you can see our naming conventions and how we format dates." You are not teaching them SQL — they already know it — you are calibrating them to your particular context and style. The key intuition is that the model is not learning from your examples the way a student learns: it is using them as a pattern anchor. The examples shift the model's probability distribution toward outputs that look like your demonstrations, without changing a single weight in the neural network. This is why the quality, diversity, and format of your examples matters enormously — garbage examples anchor to garbage patterns.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
Chain-of-Thought Prompting · Retrieval-Augmented Generation · Instruction Tuning · Prompt Engineering · In-Context Learning