Use examples to show the pattern
Show 3-5 diverse, consistent examples, balance the labels, and keep them clearly marked.
- Decide when examples help more than instructions
- Choose diverse, balanced, consistent examples
- Format examples so they’re clearly separated from the real input
Instructions describe; examples show. For tone, format, labeling conventions and tricky edge cases, a few demonstrations often beat paragraphs of rules - the GPT-3 paper (Brown et al., 2020) made “few-shot” prompting famous.
Current guidance (Anthropic) suggests 3-5 examples, and making them:
- Relevant - like the real inputs, including the hard cases.
- Diverse - varied enough that the model doesn’t copy surface details (same length, same opening words).
- Balanced - if two of three examples say “billing”, the model leans toward “billing”.
- Clearly marked - inside
<example>tags, so they aren’t confused with the actual input.
Try it
Up to 15 XPWill these examples help or hurt?
You’re building a ticket classifier with labels billing, shipping and technical. Judge each set of examples.
“Five billing examples and one shipping example”
“One example per label, including a ticket that mentions both a refund and a late package (labeled by its main issue)”
“Every example starts with “Hello, I have a problem with…””
“Examples in the same JSON format you want back, wrapped in <example> tags”
“An example whose label contradicts the written rules”
from collections import Counter
labels = ["billing", "billing", "billing", "shipping", "technical"]
print(Counter(labels).most_common())[('billing', 3), ('shipping', 1), ('technical', 1)]Key takeaways
Use examples when the pattern is easier to show than describe.
3-5 relevant, diverse, balanced examples, consistent with your instructions.
Wrap examples in tags and test on cases the prompt doesn’t show.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Pick balanced examples
Line 1 is k. Line 2 is a JSON list of [text, label] candidates. Take the first k candidates of each label (in list order), then print them round-robin - one per label per round, labels in alphabetical order - as label: text.
Finally print warning: only N example(s) for LABEL for each label with fewer than k.
- Unbalanced pool
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Format examples in tags
Each line is input|output. Print an <examples> block with one <example><input>…</input><output>…</output></example> line per example, then </examples>. Escape &, < and > in the text with html.escape(text, quote=False) so example content can’t break the tags.
- Two examples
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…