DS DevShelfHub Projects · AI tools
Tutorials / Prompt Engineering / Zero-shot & Few-shot
Prompt Engineering Beginner · 10 min read Page 3 of 10

Zero-Shot & Few-Shot Prompting: When and How to Use Each

By DevShelfHub

The two most fundamental prompting techniques — how they work, when to use each, and how to write few-shot examples that actually help.

Series progress3 / 10
Zero-shot and few-shot prompting tutorial with practical examples

Zero-shot prompting

Zero-shot means giving the model a task with no examples — just instructions. You rely entirely on the model's training to understand what you want.

Example

Text
Classify the sentiment of this review as Positive, Negative, or Neutral.

Review: "The delivery was late but the product itself is great."
Sentiment:

Zero-shot works well when the task is common and well-defined. Modern frontier models (GPT-4o, Claude 3.5, Gemini 1.5) handle most standard tasks zero-shot reliably.

When zero-shot fails: the task is ambiguous, requires a non-standard output format, or the model's default behaviour doesn't match what you need. This is when few-shot examples help.

Few-shot prompting

Few-shot means including 2–5 input/output examples before your actual query. The model learns the pattern from the examples and applies it to the new input.

Text
Classify the sentiment of product reviews.

Review: "Absolutely love it, works perfectly." → Positive
Review: "Stopped working after two days." → Negative
Review: "It's okay, nothing special." → Neutral

Review: "The delivery was late but the product itself is great."
Sentiment:

The model now has a clear format, a clear label set, and a calibrated sense of what "Neutral" means versus "Positive." This significantly reduces ambiguous outputs.

How many examples?

1–2 examples

Good for simple format fixes — showing the model the output structure you expect. Fast and cheap.

3–5 examples (sweet spot)

Enough to demonstrate variety and edge cases without wasting context. This is where few-shot delivers the best quality-per-token ratio.

10+ examples

Diminishing returns. More examples burn context and increase cost. If you need this many, consider fine-tuning instead.

Writing good few-shot examples

Cover your edge cases

Don't just include easy examples. If "Neutral" is hard to distinguish from "Positive," include a borderline case in your examples so the model understands where you draw the line.

Keep examples consistent in format

The model copies the exact format of your examples. If some examples use "→" and others use ":", the model will alternate unpredictably. Pick one format and stick to it throughout.

Order matters — put hardest last

The model attends more to examples near the end of the prompt. Place your most representative or tricky example closest to the actual query.

Use real data, not made-up examples

Synthetic examples often miss the distribution of your real inputs. Pull examples from your actual dataset — they reflect the phrasing and edge cases the model will actually encounter.

Zero-shot vs Few-shot — when to use each

Situation Use
Standard task, modern frontier model Zero-shot
Custom output format or schema Few-shot
Non-obvious classification labels Few-shot
Smaller / open-source models Few-shot
High token cost, simple task Zero-shot
Inconsistent outputs despite clear instructions Few-shot

Dynamic few-shot selection

In production, you often have a large library of examples and want to dynamically select the most relevant ones for each query — rather than hardcoding the same examples every time.

Python
from langchain_core.prompts import FewShotChatMessagePromptTemplate
from langchain_community.vectorstores import FAISS
from langchain_core.example_selectors import SemanticSimilarityExampleSelector

# Select the 3 most similar examples to the current input
selector = SemanticSimilarityExampleSelector.from_examples(
    examples, embeddings, vectorstore_cls=FAISS, k=3
)

Semantic example selection improves quality and keeps token usage under control. Relevant examples beat random ones every time.

Notes

Few-shot examples encode unintended biases

If all your Positive examples happen to be short reviews and all Negative ones are long, the model learns length as a proxy for sentiment. Review examples for unintended correlations — length, punctuation style, vocabulary complexity — that don't reflect the actual label criteria.

Dynamic selection requires verifying the embedding setup works

LangChain's SemanticSimilarityExampleSelector uses a vector store under the hood. If the embedding model or vectorstore is misconfigured, it silently falls back to random selection. Log which examples are chosen in development to confirm that semantic selection is actually firing.

Example order sensitivity is higher on smaller models

Frontier models (GPT-4o, Claude 3.5) are relatively robust to example ordering. Smaller open-source models (7B–13B parameters) show 10–15% accuracy variation based purely on which example is placed last. If you target a smaller model, benchmark different orderings before shipping.

Needing 10+ examples is a signal to evaluate fine-tuning

If 3–5 shot prompting still produces inconsistent output, you are trying to teach a pattern that requires more examples than prompt engineering can efficiently convey. Fine-tuning on 50–500 labelled examples usually produces better accuracy and lower inference cost than sending 20+ examples with every API call.

Zero-Shot & Few-Shot Prompting FAQ

What is zero-shot prompting?

Zero-shot prompting means giving the model a task with no examples — just instructions. The model relies entirely on its training to understand what you want. It works well for standard, well-defined tasks on modern frontier models like GPT-4o and Claude 3.5.

What is few-shot prompting?

Few-shot prompting means including 2–5 input/output examples before your actual query. The model learns the expected pattern and output format from the examples, then applies it to the new input. It is particularly useful when the output format is custom, labels are non-obvious, or zero-shot outputs are inconsistent.

How many examples should I include in a few-shot prompt?

The sweet spot is 3–5 examples. This is enough to demonstrate variety and edge cases without wasting context. 1–2 examples work for simple format fixes; 10+ examples show diminishing returns and may signal that fine-tuning is a better approach.

How do I write good few-shot examples?

Cover edge cases (not just easy ones), keep the format consistent across all examples, place the hardest example closest to the query (the model attends most to nearby examples), and use real data from your actual dataset rather than synthetic examples that may miss your input distribution.

When should I use zero-shot vs few-shot prompting?

Use zero-shot for standard tasks on frontier models where output quality is already good and token cost matters. Switch to few-shot when you have a custom output format, non-obvious classification labels, are using smaller open-source models, or zero-shot outputs are inconsistent. Dynamic example selection (semantic similarity) is best for production systems with many examples.

Quick summary

  • Zero-shot: task + instruction only. Works for standard tasks on frontier models
  • Few-shot: 2–5 examples before your query. Use when format matters or outputs are inconsistent
  • Sweet spot is 3–5 examples — cover edge cases, keep format consistent, put hardest last
  • Use real examples from your data, not synthetic ones
  • For production with many examples, use semantic selection to pick the most relevant ones