Few-Shot Prompting
On this page
Learn by example
Few-shot prompting involves providing the model with one or more examples of the task to perform before submitting the actual request. Instead of abstractly explaining what you expect, you show it concretely—just like a trainer who, instead of describing a good meeting report for twenty minutes, hands out two well-written samples and lets participants absorb them. This technique is one of the most effective for obtaining precise and consistent results with Mistral models.
The principle is simple: the model relies on the provided examples to understand the expected format, tone, level of detail, and logic, then it reproduces this pattern for your request. A well-chosen example often replaces an entire paragraph of explanation—and does it better.
Zero-shot, one-shot, few-shot: three levels of guidance
The field’s vocabulary distinguishes three variants based on the number of examples. In zero-shot, you give an instruction without any examples and the model relies solely on its knowledge:
Classify the sentiment of this text as positive, neutral, or negative:
"Customer service was responsive but delivery was late."
Zero-shot works for simple, common tasks the model encountered thousands of times during training. For tasks specific to your business, it is often insufficient: the model will classify, but not necessarily according to your criteria.
In one-shot, a single example is sometimes enough to guide the model. The example shows both the output format and how to justify the answer:
Classify the sentiment and justify in one sentence.
Example:
Text: "Excellent product, highly recommended!"
Sentiment: positive
Justification: Expression of enthusiasm and explicit recommendation.
To classify:
Text: "Customer service was responsive but delivery was late."
Sentiment:
Justification:
With few-shot, multiple examples reduce ambiguity and reinforce consistency. This is particularly useful when edge cases abound—like this mixed review that blends a positive point and a negative point:
Classify the sentiment and justify in one sentence.
Example 1:
Text: "Excellent product, highly recommended!"
Sentiment: positive
Justification: Expression of enthusiasm and explicit recommendation.
Example 2:
Text: "The price is fair for what it is."
Sentiment: neutral
Justification: Factual evaluation without strong emotion.
Example 3:
Text: "Three weeks of waiting for a defective product."
Sentiment: negative
Justification: Delay issue combined with a quality defect.
To classify:
Text: "Customer service was responsive but delivery was late."
Sentiment:
Justification:
With three examples covering the three categories, the model has seen what each case looks like and will calibrate its response accordingly.
Implement few-shot with the Mistral API
In a text prompt, examples stack up as above. But with the API, there is a cleaner method: use the user and assistant roles to create artificial exchanges that serve as examples. You create a conversation history from scratch where the model has “already” responded perfectly:
messages = [
{
"role": "system",
"content": "You classify customer reviews. Respond only with the indicated format."
},
# Example 1
{
"role": "user",
"content": "Classify: \"Excellent product, highly recommended!\""
},
{
"role": "assistant",
"content": "Sentiment: positive\nJustification: Expression of enthusiasm and explicit recommendation."
},
# Example 2
{
"role": "user",
"content": "Classify: \"Three weeks of waiting for a defective product.\""
},
{
"role": "assistant",
"content": "Sentiment: negative\nJustification: Delay issue combined with a quality defect."
},
# Actual request
{
"role": "user",
"content": "Classify: \"Customer service was responsive but delivery was late.\""
}
]
By passing examples as user/assistant messages, the model treats them as a successful conversation history it must continue in the same vein. This is the pattern to prioritize as soon as you scale to production: examples remain separate from data, easy to maintain and enrich.
When to use few-shot
Few-shot excels in four types of situations. Custom classification, first: when your categories are not standard—internal ticket types specific to your company, for example—no description beats a few already classified tickets. Data extraction, next, when the output format is specific to your organization. Writing with a specific tone, also: if you want the model to adopt your editorial style, show it two or three of your own texts rather than trying to describe your style with adjectives. Finally, ambiguous tasks in general: as soon as the text description is not enough to resolve doubts, an example settles it.
How many examples to provide
The optimal number depends on the complexity of the task. For a simple task with a clear format—binary classification, extraction of a single field—one or two examples are enough. For a task with multiple categories or a complex format, count on three to five examples, ideally one per category. Beyond five, you are in the territory of very specific tasks or those rich in edge cases to cover explicitly.
Keep cost in mind, however: each example consumes input tokens, and with Mistral models, input tokens are billed. A classification prompt called ten thousand times a day with eight redundant examples costs significantly more than the same with three well-chosen examples, for often zero quality gain. Find the right balance between precision and efficiency.
Build good examples
A few best practices to wrap up. Vary your examples to cover different possible cases, including edge cases: three all-positive examples teach nothing about handling negative reviews. Keep a rigorously identical format from one example to the next—the slightest variation in structure will be interpreted as significant and reproduced. Use realistic examples, close to your real data: a model trained on your real customer reviews will classify your real customer reviews better than if it has only seen textbook phrases. And always place the actual request last: the model pays more attention to the beginning and end of the prompt.
Key takeaways
- Few-shot prompting shows the model what you expect by example
- Use user/assistant roles to create examples in the Mistral API
- 1 to 5 examples are enough in most cases
- Vary examples to cover different scenarios, identical format everywhere
- Few-shot is ideal for classification, extraction, and writing with a specific format