Few-Shot Prompting

Few-shot prompting is a technique where you provide AI with a few examples of the desired input-output pattern before asking it to perform a task. By showing examples, you teach the AI your specific requirements, formatting preferences, and expected behavior without fine-tuning the model.

Example

Before asking AI to write commit messages, you show it three examples: 'Input: added login button → Output: feat: add login button to header', teaching it your preferred format.

Few-shot prompting is one of the most effective techniques for getting consistent, correctly-formatted output from AI. Instead of describing what you want, you show it.

How It Works

Zero-shot (no examples): "Write a commit message for adding dark mode"

Few-shot (with examples): "Here's how I write commit messages:

  • Added login form → feat: add user login form with validation
  • Fixed header bug → fix: resolve header overlap on mobile
  • Updated docs → docs: update API documentation for v2

Now write one for: Added dark mode toggle"

When to Use Few-Shot

  • Specific formatting — Commit messages, PR descriptions, documentation
  • Consistent style — Matching existing code patterns
  • Complex transformations — Data conversion, refactoring patterns
  • Unfamiliar tasks — When AI's default behavior isn't what you need

Tips for Effective Examples

  1. Use 2-4 examples — Enough to show pattern, not so many it's overwhelming
  2. Show variety — Different inputs demonstrating the same pattern
  3. Include edge cases — If relevant, show how to handle unusual inputs
  4. Match your real data — Examples should resemble actual tasks

Few-Shot vs Fine-Tuning

Few-shot prompting is quick and flexible — no training required. Fine-tuning is more powerful but requires data and compute. Start with few-shot; fine-tune only if needed.