How it works
Instead of just describing the task, you show 2 to 5 examples of how the response should be. The model uses these examples as context to generate the response in the same format. It is one of the most effective techniques for improving results without retraining the model.
The technique works because LLMs are good at recognizing patterns. When they see examples, the model infers the format, tone and logic of the task. This is especially useful for tasks with a specific output format (JSON, classification, extraction).
Few-shot is different from fine-tuning. Fine-tuning changes the model's weights. Few-shot only adds context to the prompt. It is faster and cheaper, but less powerful for very specific tasks.
Practical example
To classify sentiment in reviews, instead of "classify the sentiment", you send: "Review: 'amei o produto' → Positive. Review: 'péssimo atendimento' → Negative. Review: 'entrega rápida, produto ok' → ?". The model responds "Positive/Neutral" following the pattern.
For data extraction, you can show: "Text: 'João Silva, 30 anos, São Paulo' → Name: João Silva, Age: 30, City: São Paulo". Then send a new text and the model extracts the fields in the same format.
What it is for
Improve the quality and consistency of LLM responses in specific tasks. Reduce the need for fine-tuning. Standardize output format.
For developers, this means creating more effective prompts with less trial and error. For prompt engineers, it is an essential day to day tool.
Difference between zero-shot, one-shot and few-shot
Zero-shot: no examples, only the instruction. Works for simple tasks and capable models. One-shot: one example. Helps the model understand the format. Few-shot: several examples (2-5). Better for complex tasks or with specific format.
More examples are not always better. Many examples consume tokens and can confuse the model. The ideal is to use the minimum necessary for the model to understand the pattern.