Few-Shot Prompting: Teaching an LLM by Example
Few-shot prompting means showing the model a handful of input/output examples before the real input. It is the fastest way to teach a specific style or labelling scheme without fine-tuning.
A classification example
Label each commit message as feat, fix, docs or chore.
<examples>
<example><input>Add dark mode toggle to header</input><label>feat</label></example>
<example><input>Fix crash when cart is empty</input><label>fix</label></example>
<example><input>Update README install steps</input><label>docs</label></example>
<example><input>Bump eslint to latest</input><label>chore</label></example>
</examples>
<input>Handle null user in profile page</input>
Passing examples as chat turns
You can also encode examples as earlier user/assistant turns. This works well when the expected output is long or formatted.
const messages = [
{ role: 'user', content: 'Turn into a tweet: We cut API latency by 40% with connection pooling.' },
{ role: 'assistant', content: '40% faster API responses, just by pooling DB connections. Small change, big win. ⚡' },
{ role: 'user', content: `Turn into a tweet: ${announcement}` },
];
How many examples?
- Start with 3 to 5. More examples help less each time and cost tokens on every request.
- Cover the tricky cases, not just easy ones. One borderline example teaches more than five obvious ones.
- Vary length and wording so the model learns the pattern, not the surface.
Mistakes to avoid
- Examples that contradict your written instructions; the model usually follows the examples.
- All examples sharing one label; the model will lean towards that label.
- Examples copied from your test set; you will overestimate quality.
Combine few-shot examples with prompt caching: examples rarely change, so they are ideal cached prefix content.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.