Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Teaching by example, inside the prompt
You can ask a language model to do a task just by describing it (zero-shot), or by also showing a few worked examples (few-shot). The model doesn’t learn permanently: it reads the examples and works out what you want, like a colleague who has done similar jobs before. That works brilliantly for tasks like ones it has seen, and poorly for truly new ones. The demo shows how a few examples quickly pin down which task is meant.
Few-shot prompting is the fastest way to adapt a model to a domain format (report summaries, fault codes) with no training, but test whether examples actually help on your own data.
- 1. Zero-shot prompting gives…
- 2. In-context learning changes the model's weights?
- 3. Few-shot works best for tasks…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Zero-shot: instruction (+ input); relies on instruction tuning. Few-shot: k demonstrations in a consistent format, then the query.
- What matters: a clear instruction and output format; demonstrations that cover the label space in a balanced way and resemble the query (retrieve similar examples by embedding); consistent formatting; demonstration order (recency bias).
- Biases: majority-label bias, recency bias and common-token bias; contextual calibration divides by the probabilities the prompt gives to a content-free input.
- Do labels matter? Min et al. (2022) found random demonstration labels often hurt surprisingly little, because demonstrations also convey the input distribution, label space and format. Later work found larger models do use the labels (they can even follow flipped labels).
- Cost: each demonstration costs tokens on every call; beyond some k, fine-tuning (F6) is cheaper and better.
- 1. Retrieving similar demonstrations…
- 2. Contextual calibration uses…
- 3. Demonstrations convey…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Prompting in practice
Exercise 1.
- Build a few-shot prompt with an instruction, formatted demonstrations and an empty output slot.
- Compute an idealised learner’s belief in the right task after k demonstrations.
Practice.
- Put the instruction first, then examples, then the input; use clear delimiters and a fixed output format.
- With chat models, demonstrations can be given as alternating user/assistant turns.
- Select demonstrations by similarity to the query and keep labels balanced.
- Evaluate zero-shot vs few-shot on a held-out set (F9-4) before committing to long prompts.
messages = [{"role": "system", "content": "Classify each maintenance note as ELECTRICAL, MECHANICAL or SOFTWARE."}]
for note, label in nearest_examples(query, pool, k=4): # retrieved by embedding similarity
messages += [{"role": "user", "content": note}, {"role": "assistant", "content": label}]
messages.append({"role": "user", "content": query})
reply = client.chat.completions.create(model=MODEL, messages=messages, temperature=0)- 1. Demonstrations in chat APIs are given as…
- 2. For classification prompts use…
- 3. Before using long few-shot prompts…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
In-context learning as Bayesian inference. Xie et al. (2022) model pretraining text as generated by latent ‘concepts’ (tasks). A model that predicts such text well must infer the concept from the context, so a prompt with demonstrations acts as evidence: the model’s prediction is a posterior average over concepts. Demonstrations that fit one concept concentrate the posterior on it; an instruction raises that concept’s prior.
Consequences. (1) Few demonstrations suffice for familiar tasks: each one roughly halves the plausibility of wrong tasks. (2) Performance on unfamiliar tasks is limited by the closest familiar ones; more demonstrations help only slowly. (3) Wrong labels are evidence for wrong tasks, so a learner that relies on labels is misled; real models also use non-label cues, which is why Min et al.’s effect was small.
Few-shot vs training. A learner without a prior needs many examples (and is unstable when examples ≈ parameters: here a dip at k = d = 8), but it is not limited by the prior, so it overtakes in-context learning on new tasks once data grows.
- 1. In the Bayesian view, demonstrations are…
- 2. For unfamiliar tasks ICL is limited by…
- 3. Learning from scratch eventually wins because…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.When do demonstrations help an in-context learner, and when does learning from scratch win?
History.
- T. Brown et al., “Language models are few-shot learners” (GPT-3, NeurIPS 2020).
- Z. Zhao et al., “Calibrate before use” (ICML 2021); J. Liu et al., “What makes good in-context examples for GPT-3?” (2022).
- S. Xie et al., “An explanation of in-context learning as implicit Bayesian inference” (ICLR 2022).
- S. Min et al., “Rethinking the role of demonstrations” (EMNLP 2022); J. Wei et al., “Larger language models do in-context learning differently” (2023).
Exercise 2. An idealised in-context learner, not a real LLM.
- Tasks: linear classifiers on 8 features. The learner knows 16 ‘pretraining’ tasks and predicts by Bayesian averaging over them (10% label noise in its likelihood). An instruction triples the true task’s prior.
- Conditions: 300 trials per point, 50 test queries each.
- Baseline: a from-scratch least-squares classifier on the same demonstrations.
| demonstrations k | 0 | 1 | 2 | 4 | 8 | 16 | 32 |
|---|---|---|---|---|---|---|---|
| seen task, instruction | 0.766 | 0.849 | 0.923 | 0.987 | 1.000 | 1.000 | 1.000 |
| seen task, no instruction | 0.618 | 0.679 | 0.745 | 0.867 | 0.970 | 0.997 | 1.000 |
| seen task, instruction, random labels | 0.766 | 0.685 | 0.655 | 0.608 | 0.567 | 0.564 | 0.560 |
| novel task, in-context | 0.493 | 0.531 | 0.556 | 0.601 | 0.635 | 0.651 | 0.667 |
| novel task, from scratch | 0.504 | 0.585 | 0.632 | 0.672 | 0.618 | 0.815 | 0.882 |
- Familiar tasks need few demonstrations: 4 examples gave 99% with an instruction; without the instruction it took about 8.
- Instructions are worth examples: zero-shot rose from 0.62 to 0.77, about the effect of two demonstrations.
- Random labels misled this learner: each one pulled belief towards wrong tasks. This is stronger than Min et al. found for real LLMs, which also learn format and label space from demonstrations; this learner uses only labels.
- Novel tasks expose the limit: in-context accuracy crept to 0.67, while the from-scratch learner reached 0.88 (after a dip at k = 8, where examples equal features).
- Seeds 1 and 2: within 0.04 everywhere.
Prompting in AI, robotics and aerospace
- Domain extraction and classification: few-shot prompts turn reports, logs and tickets into structured labels without training.
- Robotics: language-model planners are prompted with example task decompositions (SayCan, Code as Policies).
- Rapid prototyping: few-shot prompting tests a task’s feasibility before investing in data collection and fine-tuning.
References
- T. Brown et al. (2020). “Language models are few-shot learners.” NeurIPS.
- S. M. Xie, A. Raghunathan, P. Liang and T. Ma (2022). “An explanation of in-context learning as implicit Bayesian inference.” ICLR.
- S. Min et al. (2022). “Rethinking the role of demonstrations: what makes in-context learning work?” EMNLP.
- Z. Zhao, E. Wallace, S. Feng, D. Klein and S. Singh (2021). “Calibrate before use: improving few-shot performance of language models.” ICML.