Few-shot prompting

How do you teach someone a new card game? You show them a few rounds. Few-shot prompting works the same way: you give the LLM examples of input-output pairs, and it learns the pattern. This is one of the most powerful techniques in prompt engineering because it works without any fine-tuning.

Zero to Few-shot spectrum

How adding examples improves prompt accuracy

No, they are very different. Few-shot examples live in the prompt and teach the model on-the-fly without changing its weights. Fine-tuning permanently updates the model weights with training data. Few-shot is faster, cheaper, and requires no infrastructure since you just add examples to the prompt.

There are three levels: Zero-shot: No examples. "Classify this email as spam or not spam." One-shot: One example. "Here is an example: 'Win a free iPhone!' -> Spam. Now classify: 'Meeting at 3pm tomorrow'" Few-shot: 2-5 examples that establish a clear pattern. The model picks up on format, style, and decision logic from the examples. More examples means more reliable pattern matching, but also costs more tokens.

05_few_shot_prompting.py
python
# Few-shot email classifier
prompt = """Classify each email into exactly one category.
Respond with only the letter.

A = Pre-sale question
B = Broken/defective product
C = Billing issue
D = Other

Email: "Do you offer bulk discounts for orders over 100 units?"
Category: (A)

Email: "The screen on my laptop cracked after one week"
Category: (B)

Email: "I was charged twice for my subscription"
Category: (C)

Email: "Can I return my order if the package is damaged?"
Category:"""

result = get_completion(prompt)
print(result)  # (B)

Three examples lock in both the classification logic and the exact output format "(X)". The model follows the pattern precisely.

For simple tasks (classification, formatting), 2-3 examples are usually enough. For complex patterns (style transfer, nuanced decisions), 3-5 examples work better. Beyond 5, you get diminishing returns and higher token costs. The key is covering edge cases, so pick diverse examples that show the boundaries of each category.

AI prompt: Try it with AI

Loading practice…

Ordering exercise: Building a Few-shot prompt

Loading practice…

Fill in the blanks: Complete the Few-shot pattern

Loading practice…

Flashcards: Flashcards

Loading practice…

Validation checklist: Few-shot prompting checklist

Loading practice…