Few-Shot Prompting
On this page
Learning by example
Few-shot prompting involves providing input/output pairs in your prompt to guide the model. Instead of abstractly describing what you expect, you concretely show the desired result through one or more examples.
This approach relies on Grok’s ability to detect patterns and reproduce them. It’s often the most effective technique for getting a precise output format or specific behavior.
Few-shot variants
Zero-shot: no examples
You provide only the instruction, without examples. This is the default behavior.
[
{"role": "system", "content": "Classify customer reviews as 'positive', 'negative', or 'neutral'."},
{"role": "user", "content": "The product arrived broken, very disappointed."}
]
One-shot: a single example
One example is often enough to set the output format.
[
{"role": "system", "content": "Classify customer reviews using the format shown in the example."},
{"role": "user", "content": "Great quality, I recommend it!"},
{"role": "assistant", "content": "{\"sentiment\": \"positive\", \"confidence\": 0.95, \"keywords\": [\"quality\", \"recommend\"]}"},
{"role": "user", "content": "The product arrived broken, very disappointed."}
]
Few-shot: multiple examples
With 2 to 5 examples, you cover more cases and the model grasps nuances better.
[
{"role": "system", "content": "Classify customer reviews in JSON format."},
{"role": "user", "content": "Great quality, I recommend it!"},
{"role": "assistant", "content": "{\"sentiment\": \"positive\", \"confidence\": 0.95}"},
{"role": "user", "content": "Meh, nothing special."},
{"role": "assistant", "content": "{\"sentiment\": \"neutral\", \"confidence\": 0.70}"},
{"role": "user", "content": "Horrible, doesn't work at all."},
{"role": "assistant", "content": "{\"sentiment\": \"negative\", \"confidence\": 0.98}"},
{"role": "user", "content": "Fast delivery but damaged packaging."}
]
When to use few-shot
Few-shot is particularly suitable in these situations:
- Strict output format: JSON, CSV, structured Markdown, custom format
- Classification: sentiment, category, priority, language
- Text transformation: rewriting, translation with style, entity extraction
- Tone consistency: when you have a specific writing style to reproduce
Rules for effective examples
Example diversity
Your examples should cover the main cases. For a three-class classification, include at least one example of each class.
Format consistency
All your examples must follow exactly the same output format. If one example returns JSON with lowercase keys, all others must do the same.
Relevance
Choose examples close to the real cases the model will handle. Examples that are too simple or too exotic can degrade result quality.
Reasonable quantity
More examples consume more input tokens. Find the right balance: 2 to 5 examples are generally enough. Beyond that, adding examples has diminishing returns.
Combining few-shot and system messages
The most effective combination pairs a system message describing the task with few-shot examples showing the format:
[
{
"role": "system",
"content": "You are an assistant that extracts named entities (people, places, organizations) from text and returns them in JSON format. Each entity has a type and a value."
},
{
"role": "user",
"content": "Marie Curie worked at the University of Paris."
},
{
"role": "assistant",
"content": "{\"entities\": [{\"type\": \"person\", \"value\": \"Marie Curie\"}, {\"type\": \"organization\", \"value\": \"University of Paris\"}]}"
},
{
"role": "user",
"content": "The CEO of TotalEnergies met the minister in Lyon."
}
]
The system message explains the task, the example shows the exact format. The model will reproduce this format for the new input.
Practice
Create a few-shot prompt to convert product descriptions into structured sheets. Define your output format with 2-3 examples, then test with a new product.
Start with a one-shot to verify the format, then add examples if the result requires adjustments.
Key takeaways
- Few-shot involves providing examples to guide the model through imitation
- A single example (one-shot) is often enough to set the output format
- 2 to 5 examples cover most cases without wasting tokens
- Your examples must be diverse, consistent in format, and representative
- Combine the system message (description) with examples (demonstration) for optimal results