AI Agents & LLMsEasy
What is the difference between zero-shot and few-shot prompting?
Learning path, stage 2 of 5: Prompting and model features · question 2 of 10Zero-shot gives the model only instructions. Few-shot adds a handful of worked input→output examples to the prompt so the model copies the pattern. This is "in-context learning": no weights change, and the model picks up the task from the examples.
When to use which
- Zero-shot: common tasks that strong models already do well (summarise, translate, explain). Cheaper and shorter.
- Few-shot: unusual formats, a specific tone, tricky edge cases, or classification labels with house definitions.
Writing good examples
- 3–5 diverse examples usually beat 20 similar ones.
- Cover the edge cases (empty input, ambiguous input, "none of the above").
- Keep them consistent: the model copies quirks, including mistakes.
- Watch for bias: if 4 of 5 examples are "positive", the model leans positive.
- Wrap them in tags (
<example>) so they aren't confused with the real input.
Sources
Code example
tsx
1// Few-shot classification with tagged examples.2const EXAMPLES = [3 { text: "App crashes when I upload a PNG", label: "bug" },4 { text: "Could you add dark mode?", label: "feature_request" },5 { text: "How do I reset my password?", label: "question" },6 { text: "asdfgh", label: "other" }, // edge case: noise7];89export function buildPrompt(ticket: string) {10 const shots = EXAMPLES.map(11 (e) => `<example>\n<ticket>${e.text}</ticket>\n<label>${e.label}</label>\n</example>`,12 ).join("\n");1314 return (15 "Classify the support ticket as one of: bug, feature_request, question, other.\n" +16 `${shots}\n\n<ticket>${ticket}</ticket>\nRespond with only the label.`17 );18}1920console.log(buildPrompt("The export button does nothing on Safari"));
To run it: follow the one-time setup guide, add npm i @anthropic-ai/sdk zod, save the code as a .ts file and run npx tsx file.ts. Anything marked declare is a part you supply.
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the AI Agents & LLMs set.