AI & LLMs

Few-Shot Learning Explained: Teaching Models With Examples

Traditional machine learning needs thousands of labelled examples per task. GPT-3 has made 'few-shot' learning, where a model picks up a task from a handful of examples in its prompt, the most talked-about idea in AI this summer. We explain how it works, how it differs from fine-tuning, and how to write effective few-shot prompts.

By EthicalHat Research · · 3 min read
Infographic: Fine-tuning vs few-shot prompting

For most of the history of machine learning, teaching a model a new task meant collecting data: thousands or millions of labelled examples, cleaned and split into training and test sets, followed by hours or days of training. That is expensive, slow and often impossible for niche tasks.

This summer, few-shot learning has become one of the most discussed ideas in AI, thanks largely to GPT-3. Its paper is literally titled Language Models are Few-Shot Learners. The idea is appealing: show a model a handful of examples and have it pick up the task.

What "few-shot" means

The terms describe how many examples of a task a model sees:

  • Zero-shot: only a description of the task. "Classify the sentiment of this review as positive or negative."
  • One-shot: a description plus one example.
  • Few-shot: a description plus a small number of examples, typically between two and a few dozen.

Few-shot learning is not new as a research field. It has been studied for years in computer vision, with approaches such as meta-learning and prototypical networks. What GPT-3 popularised is a specific and surprising form of it.

In-context learning

In GPT-3's version of few-shot learning, the examples go into the prompt, and the model's weights never change. The model reads the examples and continues the pattern for a new input. The GPT-3 paper calls this in-context learning.

flowchart LR
  subgraph FT["Fine-tuning"]
    D[(Thousands of<br/>labelled examples)] --> T[Training run] --> M2[New model weights]
  end
  subgraph ICL["Few-shot (in-context)"]
    P["Prompt:<br/>task description<br/>+ 3–10 examples<br/>+ new input"] --> M[Same pre-trained model] --> O[Output]
  end

Why does this work at all? The honest answer is that researchers are still studying it. One intuition: a large language model has seen countless patterns of "instruction, examples, answer" in its training data, and has learned to recognise and continue such patterns. The larger the model, the better it becomes at this. The GPT-3 paper shows few-shot performance improving much faster with model size than zero-shot performance.

A worked example

Suppose you want to extract structured data from short customer messages:

text
Extract the product and the issue from each message.

Message: My X200 router keeps dropping Wi-Fi every few hours.
Product: X200 router
Issue: Wi-Fi drops intermittently

Message: The charging cable that came with my tablet stopped working after a week.
Product: tablet charging cable
Issue: stopped working after one week

Message: Ordered the blue kettle, received a red one.
Product: kettle
Issue: wrong colour delivered

Message: The app crashes whenever I try to upload a photo larger than 5 MB.
Product:

With the GPT-3 API as it works today, you send this prompt to the completion endpoint and read what comes back:

python
import openai

response = openai.Completion.create(
    engine="davinci",
    prompt=prompt,
    max_tokens=40,
    temperature=0,          # deterministic, for extraction tasks
    stop=["\n\nMessage:"],  # stop before the model invents another example
)
print(response.choices[0].text)

Writing better few-shot prompts

Early experiments, including OpenAI's own guidance and community findings, suggest several practical rules:

  • Be consistent with format. Use the same labels, separators and ordering in every example. The model imitates the structure closely.
  • Choose representative examples. Cover the variety of inputs you expect, including tricky cases.
  • Watch the order. Results can change depending on which example comes last. Models are sensitive to recency.
  • Balance the labels. If most examples say "positive", the model will lean towards "positive".
  • Use a stop sequence so the model does not keep generating new examples.
  • Set temperature low for classification and extraction; higher for creative tasks.
  • Mind the context window. GPT-3 can see 2,048 tokens, prompt and output combined, which limits how many examples fit.

Limitations

  • Sensitivity. Small changes to wording or example order can noticeably change accuracy.
  • Cost. Every request carries all the examples, so you pay to process them each time.
  • Ceiling. On many benchmarks, fine-tuned models still outperform few-shot prompting, especially for specialised domains.
  • No memory. The model does not "learn" permanently. The next request starts from scratch.
  • Reliability. The model can still produce plausible but wrong outputs, so validate results.

When to use which

Use few-shot prompting to:

  • prototype an NLP feature quickly,
  • handle many small tasks that do not justify separate models, or
  • work where labelled data is scarce.

Use fine-tuning (or classic supervised models) when:

  • the task is stable and high-volume,
  • accuracy requirements are strict, or
  • you have enough labelled data and want lower cost per request.

The bigger picture

Few-shot, in-context learning changes who can build with machine learning. A product manager or domain expert can now prototype a classifier by writing examples, without a data science team. That is a genuine shift, even if production systems will often still need more traditional approaches.

Our agentic AI security and DevSecOps teams help organisations identify tasks where language models can take on real work, and build them with proper evaluation.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.