Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 53 ยท Prompt Engineering

Zero-Shot, One-Shot and Few-Shot Prompting

Learn in-context learning: when to use zero-shot, one-shot or few-shot prompts, how to pick and order examples, and what examples cost in tokens.

Large language models can pick up a task from a few examples placed right in the prompt, with no retraining at all. This is called in-context learning. How many examples you include gives the technique its name: zero-shot means none, one-shot means one, and few-shot means a handful, usually two to ten.

Examples are one of the most powerful tools you have, because they show the model things that are hard to describe: a tone, a label scheme, an odd output format. They are also not free, since every example costs tokens on every request. This lesson covers when to use each style, how to choose and order examples, and how to measure what they cost. It builds on The Anatomy of a Good Prompt.

The three styles

How many examples?

Zero-shot Instructions only. Cheapest, relies on what the model already knows.
One-shot One example pins down the format.
Few-shot Several examples cover labels and edge cases.

Start with zero and add examples only when the output is wrong.

1 / 2

A zero-shot prompt looks like this:

TEXT
Classify the sentiment as positive, negative or mixed.
Review: The screen is gorgeous but the battery dies by noon.
Sentiment:

Modern chat models often get this right, because they have seen the task type many times during training. Few-shot prompting adds worked examples before the real input, so the model continues the pattern:

TEXT
Review: Arrived on time, works perfectly.
Sentiment: positive

Review: Broke after two days.
Sentiment: negative

Review: The screen is gorgeous but the battery dies by noon.
Sentiment:

The model is not learning in the training sense. Its weights do not change, and the "lesson" is gone when the conversation ends. It is conditioning its next-token predictions on the pattern it sees in the context window.

Assembling a few-shot prompt

A prompt assembler keeps the format of every example identical, which matters because the model copies the pattern, including its mistakes. Run this one.

Python Build a few-shot prompt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing examples

Good examples are the difference between a prompt that works and one that merely looks thorough.

  • Cover every label. If you list three categories, show each at least once.
  • Balance the labels. If four examples say "positive" and one says "negative", the model leans toward "positive". Keep the counts close unless your real data is skewed on purpose.
  • Include a hard case. One borderline example teaches more than five obvious ones.
  • Match real inputs. Use examples that look like what users will send: similar length, similar messiness.
  • Keep one format. Same labels, same spelling, same separators in every example.

Order matters

Models are sensitive to the order of examples. Putting the same label several times in a row at the end can bias the answer toward it, and the example closest to the question often has the most influence. Shuffle the examples, avoid long runs of one label, and if accuracy matters, test two or three orderings rather than trusting one. Evaluation is covered in Evaluating LLM Applications.

Testing the effect of examples

No model can be called here, so this demo uses a fake classifier that stands in for one. It scores a review by word overlap with each example and picks the best label. It is crude, but it shows the real point: the examples you include decide the answer.

Python A fake classifier that learns from examples

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With no examples the fake model can only say "unknown", so it scores 0/4. With one example it can only ever say "positive" or "unknown", so it scores 2/4. With three varied examples it gets all four right (4/4). A real model is far better at zero-shot than this toy, but the pattern holds: examples that cover your labels raise accuracy, and examples that miss a label make that label hard to predict.

The cost of examples

Examples ride along with every request, so their price repeats on each call. This calculator uses a fake price and the four-characters-per-token rule of thumb.

Python What do examples cost?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Ten short examples can multiply the input cost several times over. If you serve many requests, measure whether the extra accuracy is worth it. Caching a fixed prefix of examples can reduce the cost, as covered in Cost, Latency and Caching. When you have hundreds of good examples, fine-tuning may be cheaper than carrying them in every prompt.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a zero-shot prompt?

    Zero-shot means the model gets the task description only and relies on what it already knows.
  2. 2. Does few-shot prompting change the model's weights?

    In-context learning happens at inference time; the weights stay fixed and the examples disappear with the context.
  3. 3. Four of your five examples are labeled 'positive'. What is the likely effect?

    Unbalanced labels bias the model toward the most common one, so keep label counts close.
  4. 4. Why can adding many examples be expensive?

    Each example adds input tokens to every call, so the cost scales with request volume.

Summary

  • Zero-shot uses instructions only, one-shot adds one example, few-shot adds several.
  • Examples teach format, labels, style and edge cases without changing the model's weights.
  • Cover every label, keep them balanced, include a hard case and keep one consistent format.
  • Example order affects results, so shuffle and test.
  • Every example costs input tokens on every request; measure whether it pays off.

Next, see how to get a model to think before it answers in Reasoning: Chain of Thought and Beyond.