Technique · 19 studies
Few-shot prompting: what the research shows
Few-shot prompting means showing the model a few worked examples so it copies the pattern. Studies find 5-25 examples placed before the task work best, that examples shape form more than logic, and that explicit rules can beat them on compound conditions.
Also called: in-context examples, examples in the prompt, few-shot CoT, demonstrations
What few-shot prompting is
Few-shot prompting puts a handful of worked examples into the prompt so the model can copy the pattern instead of inferring it from a description. An example can show only input and output, or input, reasoning and output. The second form, few-shot chain-of-thought, is what a study on reading hidden intentions used to bring GPT-4o to human level on inferring what a vague request really meant, where zero-shot and plain answer-only examples fell short.
In practice the examples rarely travel alone. The prompts that tested well combine three parts: a definition of the task or output schema, the examples, and the new input. A clinical extraction study pairs a JSON schema with one or two reference answers; a fact-versus-opinion classification study pairs detailed category definitions and edge cases with 2-6 examples, and that plain setup beat multi-agent debates.
Why examples work, and how they mislead
Examples work because models are strong pattern matchers within the context window. They show the format, the level of detail and the boundary between categories in a way a paragraph of instructions often cannot. Two consequences follow from the same mechanism.
- The model copies form, not intent. In Show and Tell, 160 coding sessions with Gemini 2.5 Pro showed that concise examples produced concise first drafts, but once the user asked to "improve the code" the output bloated exactly as if no examples had been given. Explicit rules kept working on revision; rules plus examples cut first-turn tokens by 70%.
- The model cannot tell a good example from a bad one. In a GUI agent that reuses its own past solutions, switching off the quality filter made the agent worse than having no example library at all; only trajectories scored at least 4 out of 5 were safe to reuse.
Examples also shift what the model thinks the task is. In a jailbreak-defense study across 9 models, safe-refusal examples strengthened a prompt framed as a role ("you are a safe assistant", up to +4.5%) but weakened a prompt framed as a list of rules (down to -21.2%), because the extra example tokens drew attention away from the instructions.
How many examples, where, and which ones
The studies give fairly concrete settings.
- How many. On 90,000 code translations with Gemini models, accuracy peaked at 61-62% with 5-25 examples and fell back to 54-58% at 125-625, where the model copied surface patterns; the large prompt also cost 21 times more (the many-shot paradox).
- Where. Examples placed before the question, ideally at the start of the system prompt, beat examples appended after it by up to +6% and flipped fewer answers (positional bias study). For code, putting test cases inside the function's docstring rather than in a block at the end raised correct outputs to 92–100%, because the link between example and task was 2.8–4.4 times stronger (co-located tests).
- Which ones. Cover the distinct cases you care about. One prompt with 6 examples of different meaning shifts (number, polarity, obligation, dropped condition) beat an 8-agent pipeline at spotting translation errors in technical requirements and ran in 17 minutes instead of 181 (Direct Judge). Keep examples consistent in tone; mixed styles confuse the pattern.
- Rare formats. When the input looks unlike training data, two examples can restore performance: on numbers written in unusual scripts and separators, accuracy that had dropped by 87% climbed from 13% to 70-90% (multiscript numeracy).
- Your own history. Past question-answer-result cases worked better than a self-description for personal advice (track record versus bio), and compressing an old case into a short note of what to repeat and what to re-check stops the model from copying stale names and dates (query-conditioned reuse).
A related option is to let the model write its own example first; that is covered on the analogical prompting page.
When rules or reasoning beat examples
The evidence is not one-sided, and the studies disagree on how much examples add once instructions are good.
- Rules beat examples for compound logic. Across five invented tasks, an explicit rule ("URGENT if A OR B, AND C") beat a set of unexplained examples, and adding examples to a clear rule barely helped (rules versus examples). The gap closed on tasks that rely on categories the model already knows.
- Forced reasoning can beat examples. For coding legal texts into categories, a step-by-step decomposition prompt outperformed the few-shot version (deductive coding study).
- But definitions plus examples beat fancier methods. The opinion-detection study above found even randomly chosen examples on top of detailed definitions were enough to beat debates between models.
A reasonable reading of all three: write the rule or definition first, then add a few examples to show format and edge cases. Expect examples to carry the first answer but not follow-up turns, use the role framing rather than a rule list if examples go into a safety prompt, and stop adding examples well before the hundreds.
What the studies measured
| Study | Lab score | Finding | Effect |
|---|---|---|---|
| The Many-Shot Paradox: When More Examples = Worse ResultsOct 19, 2025 | 78 | Code-translation accuracy peaked at 61-62% with 5-25 examples and fell to 54-58% with 125-625 examples. | Mixed |
| The Position Effect: Where Your Examples Sit Matters More Than What They AreJul 30, 2025 | 82 | Examples placed at the start of the prompt improved accuracy by up to +6% and stability versus examples after the question. | Helps |
| Co-Located Tests: Placing Examples Next to the Code in the Prompt Sharply Improves Generation QualityApr 20, 2026 | 80 | Test cases inside the function docstring gave 92–100% correct outputs, with example-to-task attention 2.8–4.4 times stronger than separate tests. | Helps |
| Direct Judge: Comparing Texts with 6 Error-Type Examples Beats Multi-Agent SystemsAug 9, 2026 | 89 | A single prompt with 6 error-type examples beat six architectures, including an 8-agent system, at detecting meaning drift. | Helps |
| A detailed prompt + examples beats "debates" between several LLMsJul 10, 2025 | 78 | Detailed definitions plus 2-6 examples beat multi-LLM debates for subjectivity classification; even random examples helped. | Helps |
| Schema + examples: how to pull structure out of any text with no wasted wordsJul 7, 2025 | 77 | An output schema plus one or two examples clearly raised extraction accuracy over zero-shot on clinical transcripts. | Helps |
| Show and Tell: Controlling Style in Multi-Turn LLM ConversationsNov 17, 2025 | 90 | Examples alone controlled style only on the first turn; rules plus examples cut tokens by 70% and limited bloat on revision. | Mixed |
| Rules vs. Examples: Why ChatGPT Learns New Tasks Better from Instructions Than from DemonstrationsSep 2, 2026 | 79 | Explicit rules beat unexplained examples on tasks with AND/OR logic, and adding examples to a clear rule barely helped. | Hurts |
| Few-Shot with RoP and ToP: Opposite Effects on Jailbreak DefenseFeb 4, 2026 | 82 | Few-shot raised jailbreak defense by up to +4.5% with a role prompt but lowered it by up to -21.2% with a rule-list prompt. | Mixed |
| Multiscript Numeracy: How LLMs Trip Over Number Formats and What to Do About ItJan 21, 2026 | 77 | Two examples lifted arithmetic on unusual number formats from 13% to 70-90% accuracy. | Helps |
| Fs-CoT: show the model how to think, not just what to doJun 26, 2025 | 76 | Examples that show reasoning about intent brought GPT-4o to human level at inferring instructions from context. | Helps |
| Step-by-Step Task Decomposition: How to Make an LLM Reason Instead of GuessJul 18, 2025 | 82 | A step-by-step decomposition prompt classified texts more reliably than a few-shot prompt. | Hurts |
| EchoTrail-GUI: Learning From Your Own Experience Through a Library of Successful ExamplesDec 22, 2025 | 76 | Reusing past solutions only helped when a critic admitted examples scoring at least 4 out of 5; unfiltered examples did worse than none. | Mixed |
| Your Track Record Beats Your Bio: Why Telling an AI About Yourself Can Ruin Its Advice, While Your Chat History Improves ItSep 15, 2026 | 79 | Past question-answer-result cases produced more actionable advice than a self-written profile, which could make advice worse than none. | Helps |
| QCR (Query-Conditioned Reuse): Extracting an Action Template from Past Experience Instead of Copying the Whole HistoryAug 13, 2026 | 79 | Compressing a past case into a four-field note avoided copying stale names and numbers from a pasted history. | Helps |
| LLM as a Judge: Rating Text Quality Better Than Any Automatic MetricJul 15, 2025 | 79 | Combining reference examples of good and bad texts with step-by-step reasoning made an LLM judge outperform automatic metrics. | Helps |
| LEKIA, Constitution + Precedents: how to make an LLM act like an experienced professionalJul 20, 2025 | 72 | Rules plus concrete precedent answers taught an assistant a professional tone that a bare instruction like 'be empathetic' did not. | Helps |
| SA-ICL (Schema-Activated In-Context Learning): learning by analogy by activating mental schemasOct 14, 2025 | 83 | Retrieving an example by its abstract task schema rather than surface topic helped transfer between tasks with the same structure. | Helps |
More studies on this technique:
Prompts to copy
Few-shot block of the right size
You are a {role}. Here are {5-25} examples of {content type} that work well:
[Example 1: {context}]
{example text}
[Example 2: {different context}]
{example text}
...
[Example {N}: {another context}]
{example text}
---
Now create {content type} for a new context:
- {key information 1}
- {key information 2}
- {key information 3}
Keep {what matters to preserve from the examples}, but adapt it to {new context}.
From: The Many-Shot Paradox: When More Examples = Worse Results
Examples next to the code (docstring tests)
def {function_name}({params}):
"""{One line: what the function does}
>>> {function_name}({typical_input_1})
{expected_result_1}
>>> {function_name}({typical_input_2})
{expected_result_2}
>>> {function_name}({edge_case})
{edge_case_result}
"""
pass
{Additional requirements if any}
Write the implementation.
One example per error type
Compare two texts — the original and the {translation/paraphrase/version}.
Determine whether the meaning changed in the aspects that matter to me: {what matters — numbers, obligation level, conditions, objects}.
Here are examples of what counts as a divergence and what doesn't:
1. NO divergence: [example of a paraphrase with no meaning lost]
2. DIVERGENCE ({type 1}): [example]
3. DIVERGENCE ({type 2}): [example]
4. DIVERGENCE ({type 3}): [example]
5. DIVERGENCE ({type 4}): [example]
6. DIVERGENCE ({type 5}): [example]
Now compare:
Text A: {original}
Text B: {version to check}
Answer:
1) Divergence: YES/NO
2) Confidence: low/medium/high
3) What changed (if anything)
From: Direct Judge: Comparing Texts with 6 Error-Type Examples Beats Multi-Agent Systems
Related techniques
Problems it helps with:
Questions people ask
How many examples should a few-shot prompt have?
In the tested settings, a handful. Code translation peaked with 5-25 examples and got worse with hundreds, and an opinion-classification study used 2-6. Past roughly 25 examples the model started copying surface patterns while cost kept rising.
Where should the examples go in the prompt?
Before the question, ideally at the start of the system prompt; examples appended after the question made answers less accurate and less stable. For code, put the examples right next to the function, for instance as docstring tests.
Are examples better than written rules?
Not for compound logic. One study found explicit AND/OR rules beat unexplained examples and that examples added little to a clear rule. Examples are strongest for showing format, tone and edge cases, so write the rule first and add a few examples.
Why does the model ignore my examples after a follow-up request?
Examples act as a template for the first answer, not as a standing rule. In one study, asking to improve the code made output bloat as if no examples existed, while explicit style rules kept applying. Restate the key rules when you ask for revisions.