Zero-Shot, Few-Shot & Worked Examples
Use input/output examples to lock in a pattern the model should follow, and know when zero-shot is enough.
TL;DR
- Zero-shot asks with no examples; few-shot shows one to a few input/output pairs to lock in a pattern.
- Reach for examples when the shape, naming, or style is easier to show than to describe.
- Two or three well-chosen examples usually beat a long paragraph of rules.
Zero-Shot
What It IsAsk directly with no examples; rely on the model's general knowledge.
"Write a Python function that checks if
a string is a palindrome."When To UseCommon, well-defined tasks where there is one obvious right shape.
Standard algorithms, simple utilities,
idiomatic one-liners.Strengthen ItEven without examples, pin the signature and constraints.
"...signature isPalindrome(s: str) -> bool,
ignore case and spaces."Few-Shot
What It IsProvide a few input/output pairs, then ask for the next output.
format(1500) -> "$1,500.00"
format(0) -> "$0.00"
format(-42.5) -> "-$42.50"
format(99999) -> ?When To UseWhen exact shape, naming, or formatting is specific and easier shown than told.
Custom output shapes, DSLs, label
conventions, structured logs.Cover Edge CasesInclude zero, negative, or empty examples so the model copies that handling.
Show format(0) and format(-42.5),
not just the happy path.Choosing
Describable? Zero-ShotIf a sentence fully captures the task, skip examples and save context.
"Slugify a string: lowercase,
spaces to hyphens." -> no examples neededShow-Me? Few-ShotIf the pattern has quirks, two examples teach it faster than a paragraph.
Quirky mapping -> show 2-3 pairsMix ThemDescribe the task, then add a couple of examples for the fiddly parts.
Rules + 2 examples of the edge cases.Pitfalls
Inconsistent ExamplesOne example that breaks the pattern teaches the wrong rule.
Keep casing, spacing, and shape
identical across all examples.OverfittingThe model may copy incidental details of your samples, not the intent.
Vary the sample values so it learns
the rule, not the specific numbers.Examples vs RulesWhen examples contradict instructions, examples often win; keep them aligned.
Align the examples with the stated rule.Tips
- Make examples cover the tricky cases (empty input, nulls) so the model copies the handling you want.
- Keep examples consistent; one off-pattern example teaches the model the wrong rule.
Warnings
- Too many examples waste context and can make the model overfit to surface details of your samples.
- If examples disagree with your written instructions, the model may follow the examples instead.
In Practice
You want commit-message formatting that your written rules struggle to capture. Three consistent examples, including an edge case, pin the pattern precisely.
- State the task so the examples have context to attach to.
- Give examples that share one consistent shape, scope, then summary.
- Include a breaking-change case so the model learns the exclamation rule.
- Ask for the next output and the model follows the demonstrated pattern.
Task: turn a change description into a
Conventional Commit subject line.
Examples:
"added dark mode toggle"
-> feat(ui): add dark mode toggle
"fixed crash when cart is empty"
-> fix(cart): handle empty cart state
"renamed API field, breaks clients"
-> feat(api)!: rename user field
Now format: "sped up the search query"FAQ
When the task is common and unambiguous, like 'write a function to reverse a string in Python'. Modern models handle these well without examples. Add examples only when the output shape or style is specific to you.
Usually one to three. One fixes the format; two or three teach a pattern and its edge cases. Beyond that you spend context for diminishing returns and risk the model copying irrelevant surface details.
It shows the exact input and the exact desired output, is consistent with your other examples, and includes a tricky case so the model learns how you want edge cases handled, not just the happy path.
No. Examples complement the task, they do not replace it. State the task, then show examples of the result. If the two conflict, the model may trust the examples and ignore your rule.