Prompting Strategies and Tips › Teaching by Example: Few-Shot Prompting
AI Agent HandbookAll chapters Prompting Strategies and Tips › Teaching by Example: Few-Shot PromptingOne section · Sections in this chapter3 / 10 ↓
Sometimes the best way to explain what you want is to show examples. This technique, called few-shot prompting, involves giving the AI a few examples of the pattern you want it to follow. The model learns from these examples and applies the same pattern to new inputs.
Think about how you might train someone to format data. Instead of writing detailed rules, you'd probably just show them a few examples: "Here's how the first one should look, here's the second one, now you try the third." Few-shot prompting works the same way.
When to Use Few-Shot Prompting
This strategy shines in several situations:
Pattern matching: When you want the AI to follow a specific format or structure. For example, if you're extracting information from text and want it in a particular layout.
Classification tasks: When you need the AI to categorize things consistently. Show it a few examples of each category, and it will understand the distinctions.
Style matching: When you want the AI to write in a specific style or tone. A few examples help it capture the voice you're looking for.
Complex transformations: When the task involves multiple steps or subtle rules that are easier to demonstrate than explain.
Example (GPT-5)
Here's a practical example where we want the AI to categorize customer feedback as positive, negative, or neutral:
import os
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
## Few-shot prompt with examples
## GPT-5 handles pattern matching tasks like classification well
prompt = """Categorize the following customer feedback as positive, negative, or neutral.
Examples:
Feedback: "The product arrived quickly and works great!"
Category: positive
Feedback: "The item was damaged and customer service was unhelpful."
Category: negative
Feedback: "The product is okay, nothing special."
Category: neutral
Now categorize this:
Feedback: "I love the design but wish it had more features."
Category:"""
response = client.chat.completions.create(
model="gpt-5", messages=[{"role": "user", "content": prompt}]
)
print(response.choices[0].message.content)Category: neutral
The model sees the pattern in the examples and applies it to the new feedback. It learns that positive feedback includes praise, negative includes complaints, and neutral is somewhere in between. Without these examples, the model might struggle with mixed feedback like "I love the design but wish it had more features."
How Many Examples Do You Need?
The name "few-shot" is literal. You typically need between 2 and 5 examples. More than that rarely helps and makes your prompt longer (which can slow things down and cost more). Start with 2-3 examples and add more only if the results aren't consistent.
Also, make sure your examples are diverse. If you're teaching the AI to categorize feedback, include examples that cover different types of positive, negative, and neutral responses. This helps the model generalize better.