Few-Shot Prompting: How to Guide AI with Structured Examples
Few-shot prompting gives the model examples inside the prompt to establish the expected format, tone, and response pattern — no fine-tuning needed.
// 5 min read · ● updated 2026-06
// before reading
Few-shot prompting is the technique of including 1 to 10 examples in the prompt to show the model exactly what format, tone, and response type you expect. Instead of describing what you want, you show it. This works because LLMs are pattern-recognition machines: if they see 3 examples of the same pattern, they replicate it. No fine-tuning, no huge datasets — just well-written examples in the prompt.
Pro Tip: Few-shot is the bridge between basic prompting (zero-shot) and fine-tuning. If zero-shot doesn't work and you can't fine-tune, few-shot is your next step.
What is it
The term "few-shot" comes from meta-learning literature and was popularized by GPT-3 in 2020. The idea is that the model, given a handful of input-output examples, infers the task and generalizes to new cases without updating its weights.
There are three variants based on how many examples you provide:
| Variant | Examples | When to use |
|---|---|---|
| Zero-shot | 0 | Simple tasks where the model already understands the instruction |
| One-shot | 1 | Show a single example of the expected format |
| Few-shot | 2-10 | Complex tasks where the pattern isn't obvious |
The effective range is typically 2 to 10 examples. More than 10 can saturate the context and yield diminishing returns.
Note: Few-shot does not train the model. Examples are included in the prompt and discarded after the response. No persistence, no weight updates.
Mental model
Imagine giving instructions to a new intern. You could describe the format ("write a formal email with a subject, greeting, body, and closing") or you could show them 3 well-written emails. The intern will understand much better by seeing examples.
Few-shot prompting is exactly that: instead of telling the model "classify this text as positive, negative, or neutral," you show it:
Text: "I loved the product, it arrived fast and works perfectly"
Classification: Positive
Text: "Customer service was terrible, they never replied"
Classification: Negative
Text: "The product is okay, nothing special"
Classification: Neutral
Text: "Delivery took a while but the product is good"
Classification: [model completes]How it's used
Basic few-shot: sentiment classification
prompt = """
Classify each text as Positive, Negative, or Neutral.
Text: "Excellent quality, exceeded my expectations"
Classification: Positive
Text: "Doesn't work as expected, very disappointed"
Classification: Negative
Text: "It's a normal product, does its job"
Classification: Neutral
Text: "The battery lasts long but the app is slow"
Classification:"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
print(response.choices[0].message.content)
# → Neutral (because it combines a positive and a negative aspect)Warning: Example order matters. Models tend to weigh examples at the end of the prompt more heavily (recency bias). Put the most representative example last.
Few-shot for structured output formatting
prompt = """
Extract patient data in JSON format.
Patient: John Doe, 34 years old, diagnosis: type 2 diabetes
{"name": "John Doe", "age": 34, "diagnosis": "type 2 diabetes"}
Patient: Jane Smith, 28 years old, diagnosis: hypertension
{"name": "Jane Smith", "age": 28, "diagnosis": "hypertension"}
Patient: Carlos Lopez, 45 years old, diagnosis: bronchial asthma
"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}],
response_format={"type": "json_object"}
)
print(response.choices[0].message.content)
# → {"name": "Carlos Lopez", "age": 45, "diagnosis": "bronchial asthma"}Dynamic few-shot
Instead of always using the same examples, select the most relevant ones for each query using embedding similarity:
import numpy as np
from openai import OpenAI
client = OpenAI()
# Example database with embeddings
examples = [
{"text": "I loved the service", "label": "Positive", "embedding": None},
{"text": "Terrible customer support", "label": "Negative", "embedding": None},
{"text": "The product arrived on time", "label": "Positive", "embedding": None},
]
# Generate embeddings for each example
for e in examples:
resp = client.embeddings.create(
model="text-embedding-3-small",
input=e["text"]
)
e["embedding"] = resp.data[0].embedding
# Find the most similar examples for a given query
def find_examples(query, k=3):
resp = client.embeddings.create(
model="text-embedding-3-small",
input=query
)
query_emb = np.array(resp.data[0].embedding)
similarities = []
for e in examples:
sim = np.dot(query_emb, e["embedding"])
similarities.append((sim, e))
similarities.sort(reverse=True)
return [e for _, e in similarities[:k]]
# Build a dynamic prompt
query = "The shipping was very fast, I recommend it"
best = find_examples(query, k=3)
prompt = "Classify each text as Positive, Negative, or Neutral.\n\n"
for e in best:
prompt += f'Text: "{e["text"]}"\nClassification: {e["label"]}\n\n'
prompt += f'Text: "{query}"\nClassification:'Pro Tip: Dynamic few-shot is the most commonly used technique in production for classification and extraction. It improves accuracy by 5-15% over static few-shot because each query gets examples relevant to its context.
Few-shot for specialized translation
prompt = """
Translate the following marketing terms from English to Spanish,
keeping the persuasive tone and adapting them culturally.
EN: "Unlock your potential"
ES: "Descubre tu verdadero potencial"
EN: "Limited time offer"
ES: "Oferta por tiempo limitado"
EN: "Join thousands of satisfied customers"
ES: "Únete a miles de clientes satisfechos"
EN: "Take your career to the next level"
ES:"""When to use it / when not to
| ✅ Use it when | ❌ Don't use it when |
|---|---|
| You need a very specific output format | The model already gets it right with zero-shot |
| The task has nuances hard to describe in words | The prompt becomes too long (losing useful context) |
| You want consistent tone and style | You need hundreds of examples (that's fine-tuning territory) |
| You can't fine-tune (cost, time, data constraints) | The examples confuse more than they guide |
| You're doing classification or data extraction | The task changes frequently (keeping examples updated is costly) |
Pro Tip: Always measure zero-shot first. Sometimes zero-shot + a clear instruction beats few-shot with poorly chosen examples. Wrong or ambiguous examples hurt performance.
History and evolution
View history
The concept of few-shot learning existed in traditional machine learning before LLMs, but GPT-3 (2020) popularized it in the prompting context. The paper "Language Models are Few-Shot Learners" (Brown et al., 2020) showed that a 175B-parameter model could perform new tasks with just a few in-prompt examples, without fine-tuning.
From there, the community explored variants: dynamic few-shot (similarity-based example selection), flipped-label few-shot (testing whether the model truly understands the task), and few-shot chain-of-thought (examples with step-by-step reasoning included).
Today few-shot is a standard technique in every prompt engineer's toolkit. Its effectiveness has been surpassed in some cases by better-written zero-shot instructions (thanks to more capable models), but it remains indispensable for tasks with very specific formats or specialized domains.
// related