+140 XP

Few-shot prompting: teaching by example

You can write three sentences trying to explain what "urgent" means, or you can show the model three messages and let it figure out the pattern itself. The second approach almost always wins.

That is few-shot prompting: giving the model a handful of worked examples before asking it to do the real task. "Shot" just means "example." Zero-shot is no examples. One-shot is one. Few-shot is two or more.

The problem with describing

Imagine you want to sort incoming customer messages into "urgent" or "normal." Your first instinct is to describe the rules:

Classify this message as urgent or normal. Urgent means the customer is angry, mentions money being lost, has a deadline, or is threatening to cancel. Normal means general questions, feedback, or anything that can wait.

This works okay. But you will spend ages adding edge cases. What about a calm message that says "my payment failed"? What about an excited message that is actually fine?

Words about rules are slippery. Examples are sharp.

Teaching by example instead

Here is the same task as a few-shot prompt. Notice there are no rules at all, just three examples and then the new message:

Classify each message as "urgent" or "normal".

Message: "My account got charged twice and the payment is for $4,000. I need this fixed today."
Label: urgent

Message: "Hi! Just wondering what your office hours are over the holidays."
Label: normal

Message: "This is the third time I've emailed. If I don't hear back I'm cancelling my subscription tomorrow."
Label: urgent

Message: "Loving the new dashboard, the charts are really clear."
Label:

The model reads the pattern and completes the last line: normal.

It learned from your examples that double charges and cancellation threats are urgent, while friendly questions and praise are normal. You never wrote those rules down. The examples carried them.

Why this works

A large language model (the AI behind ChatGPT, Claude, and Gemini) is a pattern-completion engine. When it sees a clear, repeated structure, it continues that structure. Three examples in the same format create a strong pattern. The model copies the format and the logic baked into your choices.

This is why format matters as much as content. Keep every example identical in shape: same labels, same spacing, same wording for "Message" and "Label."

Choosing good examples

Your examples are your instructions. Pick them carefully.

Cover the range. Include the obvious cases and one tricky one. If a calm message can still be urgent (like a quiet "my payment failed"), make one of your examples exactly that. The model learns your edge cases from the examples you choose.

Balance your labels. If all three examples are "urgent," the model leans toward calling everything urgent. Mix them: two of one, one of the other, or an even split.

Be consistent. Use the exact same label words every time. "urgent" and "Urgent" and "URGENT" look like three different categories to a literal machine. Pick one.

Keep them short. Examples eat into the space the model can read at once (its "context window"). Three tight examples beat ten rambling ones.

Running it with code

If you want to classify hundreds of messages, you will run this through an API (a way for your code to talk to the model). Here is a short, runnable example using OpenAI's Python library. The same idea works with Claude or Gemini, just different library names.

python
from openai import OpenAI

client = OpenAI()

few_shot = """Classify each message as "urgent" or "normal".

Message: "My account got charged twice for $4,000. Fix this today."
Label: urgent

Message: "What are your holiday office hours?"
Label: normal

Message: "Third email. Reply or I'm cancelling tomorrow."
Label: urgent

Message: "{new_message}"
Label:"""

def classify(message):
    prompt = few_shot.format(new_message=message)
    response = client.chat.completions.create(
        model="gpt-4.1-mini",
        messages=[{"role": "user", "content": prompt}],
        temperature=0,
    )
    return response.choices[0].message.content.strip()

print(classify("The app keeps crashing every time I try to pay."))

One detail worth knowing: temperature=0. Temperature controls randomness. For classification you want the same answer every time, so set it to 0 for the most consistent, predictable output.

If you are not a coder, you do not need this. You can do few-shot prompting by hand right inside ChatGPT, Claude, or Gemini. Just paste your examples and your new message into the chat.

Few-shot beyond classification

This trick is not only for sorting things. Use it any time you want a specific format or style.

Formatting data. Show two examples of turning a messy address into clean fields, and the model will do the rest.

Matching a tone. Paste two of your own past emails as examples, then ask the model to write a third in the same voice. It copies your style far better than if you described it.

Extracting information. Give two examples of pulling the name, date, and amount out of an invoice, and the model learns exactly which fields you want and how to lay them out.

The principle is always the same: stop describing, start demonstrating.

For a deeper, well-written reference on this and related techniques, the open Prompt Engineering Guide is free and stays current.

🎬 [VIDEO: "Few-Shot Prompting Explained" - youtube.com - a short, plain-language walkthrough of zero-shot vs few-shot with live examples you can copy]

Knowledge check

1. What best defines few-shot prompting?

2. According to the lesson, why do examples often work better than written rules for a task like classifying messages?

3. Why does few-shot prompting work so well with a large language model?

MULTIPLE CHOICE

4. Select ALL correct statements about the distinction between zero-shot, one-shot, and few-shot prompting.

Select all the correct answers.

MULTIPLE CHOICE

5. Select ALL practices the lesson recommends when writing few-shot examples.

Select all the correct answers.

When few-shot is overkill

Few-shot is powerful, but it is not always needed. Modern models in 2026 are strong enough that simple tasks often work zero-shot, with no examples at all. "Summarize this in one sentence" needs no demonstration.

Reach for few-shot when:

  • The format is specific and you want it exactly right every time.
  • The task has edge cases that rules struggle to capture.
  • You need consistency across many runs (like classifying a whole inbox).
  • The style or voice matters and is hard to describe.

Skip it when the task is common and obvious. Adding examples then just wastes space and your time.

A quick test

Not sure if you need examples? Try zero-shot first. If the model gets it right, you are done. If it drifts off format or misreads your intent, add two or three examples to pin it down. Few-shot is your fix when zero-shot is close but not reliable.

Common Mistakes

Inconsistent examples. If one example uses "urgent" and another uses "high priority," the model gets confused about your categories. Match them exactly.

Too few examples for a hard task. One example might not show the pattern clearly. If results are shaky, add a second and third before doing anything fancier.

Leaking the answer. When testing, do not accidentally include the correct label on your new message. Leave the final Label: empty so the model fills it.

Unbalanced examples nudging the model. Three "urgent" examples in a row teach the model that urgent is the default. Mix your labels to keep it neutral.

Try it yourself

Open ChatGPT, Claude, or Gemini right now. Paste this:

Turn each phrase into a polite, professional sentence.

Phrase: "send me the file"
Sentence: "Could you please send me the file when you have a moment?"

Phrase: "you're late"
Sentence: "I wanted to gently flag that the deadline has passed."

Phrase: "fix the bug"
Sentence:

Watch it match the tone of your two examples. Then change the examples to be casual and funny, and run it again. The output changes to match. That is you teaching by example in under a minute.

Key Takeaways

  • Show, don't tell. Two or three examples in a consistent format usually beat a paragraph of rules.
  • Your examples are your instructions. Cover the range, include a tricky case, and balance your labels so the model stays neutral.
  • Keep the format identical across every example: same labels, same spacing, same wording. Inconsistency confuses the model.
  • Try zero-shot first. Add examples only when the model drifts off format or misreads your intent.
  • Set temperature to 0 for classification and any task where you want the same answer every time.

Frequently asked questions

What is few-shot prompting?

Few-shot prompting means giving a language model two or more worked examples before asking it to handle the real task. Instead of describing rules like "urgent means the customer is angry or mentions money," you show three labelled messages and let the model infer the pattern. Zero-shot means no examples, one-shot means one, few-shot means two or more.

When should I use few-shot instead of just describing the rules?

Use examples when the format must be exact, when the task has edge cases that rules capture badly, when you need consistency across many runs, or when the style matters and is hard to put into words. Rules written in prose are slippery: a calm message saying "my payment failed" is still urgent, and you would spend ages patching that into a description. One example covering that case does the job.

How many examples should I put in a prompt?

Two or three well-chosen examples are usually enough, and three tight ones beat ten rambling ones. Examples consume space in the context window, the amount of text the model can read at once. If results stay shaky with one example, add a second and a third before trying anything more complex.

Should I try zero-shot before adding examples?

Yes. Modern models handle common tasks like "summarize this in one sentence" with no examples at all, and adding them just wastes space. Run zero-shot first: if the output is right, you are done. Add two or three examples only when the model drifts off format or misreads your intent.

Why does my classification prompt label everything urgent?

Most likely your examples are unbalanced. If all three demonstrations carry the label "urgent," the model learns that urgent is the default and applies it everywhere. Mix your labels, two of one and one of the other, or split them evenly, and check that you use the exact same wording each time since "urgent," "Urgent" and "URGENT" read as three distinct categories.

What temperature should I set for a classification task with the OpenAI API?

Set temperature to 0. Temperature controls randomness in the output, so 0 gives the most consistent and predictable answer, which is what you want when classifying the same kind of message hundreds of times. In the OpenAI Python library you pass it directly in the chat.completions.create call alongside your model and messages.

What to do, from this lesson

These actions are compiled in the role's Playbook.

  • Provide two or three consistent worked examples for tricky formats
See the full action playbook →

Related articles

Recent articles from the blog that build on this lesson.