Few-Shot Prompting: How to Teach AI with Examples
When working with Large Language Models (LLMs), explaining a complex rule in plain English can sometimes be difficult. For instance, if you want the model to output sentiment analysis in a specific format with highly nuanced boundaries, writing pages of instructions might still lead to inconsistent results.
This is where Few-Shot Prompting comes in.
Instead of just telling the AI what to do (zero-shot prompting), few-shot prompting involves showing the AI examples of inputs and their expected outputs directly inside your prompt.
Zero-Shot vs. Few-Shot: A Comparison
Zero-Shot (No Examples)
In a zero-shot prompt, you ask the model to perform a task without giving it any examples.
- Prompt: “Classify this customer support ticket as Refund, Technical, or Feedback. Ticket: ‘My screen is black and it won’t turn on.’”
- Response: “This ticket falls under the Technical category because the user is experiencing hardware issues where their screen is not working.”
While the classification is correct, the output is wordy. If you needed to parse this response in a database, you would have to write custom logic to strip the extra text.
Few-Shot (With Examples)
In a few-shot prompt, you show the model the exact format and structure you want.
- Prompt:
Classify the customer support ticket. Respond with ONLY the category name. Ticket: "I bought a shirt but it's too small, I need my money back." Category: Refund Ticket: "I forgot my password and can't log in to my dashboard." Category: Technical Ticket: "I love the new dark mode design, it looks amazing!" Category: Feedback Ticket: "My screen is black and it won't turn on." Category: - Response: “Technical”
By providing three simple examples, the model immediately understood the target output format, the classification criteria, and the constraint to respond with only the category name—without you having to write explicit rule constraints.
When to Use Few-Shot Prompting
Few-shot prompting is particularly powerful for three specific use cases:
- Complex Output Formatting: When you need the output structured in a highly specific or unusual syntax (e.g., custom DSLs, specific JSON shapes, or structured markdown tables).
- Nuanced Classification: When the rules for categorization are subjective. Showing examples of borderline cases helps the model learn where you draw the line.
- Consistent Tone & Voice: When you want the AI to write in a specific voice (e.g., your brand’s voice). Providing 2-3 examples of existing marketing copy guides the model far better than adjectives like “friendly and engaging.”
How to Structure Your Examples
To get the best results from few-shot prompting, follow these rules:
1. Maintain Consistency
Ensure the syntax, spacing, and delimiters of your examples match your final query exactly. If you use User: and AI: in your examples, use them for the actual task as well.
2. Keep Examples Balanced
If you are doing classification, try to show at least one example for each possible class. If you only show “Refund” examples, the model might develop a bias and classify everything as a refund.
3. Use 3 to 5 Examples
Usually, three examples are enough for the model to pick up the pattern. Adding more than five examples increases your token costs and can lead to the model forgetting instructions at the beginning of the prompt due to context congestion.
Automating Examples with PromptCraft
Few-shot prompting is highly effective, but manually writing out pairs of inputs and outputs makes your prompts long and tedious to write.
PromptCraft automatically optimizes your prompts by structuring execution templates and placeholders for variables. This makes it easy to set up stable systems where you can feed examples dynamically.
Head to the homepage to refine your prompts and start getting reliable, structured outputs from your AI integrations today.
Refine Your AI Prompts Automatically
Put the prompt engineering concepts in this guide to work. Use PromptCraft to instantly rewrite, structure, and optimize your prompts.