In-Context Learning

In-context learning (ICL) is a language model changing its behavior on a task because of the text in its prompt, with no change to its weights. The prompt can contain an instruction, a few input-output examples, a document, or all three. The model reads them as ordinary context and continues the text accordingly.

A traced example

Suppose a hypothetical prompt is:

Review: "The battery died in a day."  Label: neg
Review: "Setup took two minutes."      Label: pos
Review: "Screen is gorgeous."          Label:

Nothing is trained. The model has one fixed set of weights and computes a next-token distribution for the position after the last Label:. The two worked examples shift that distribution toward a one-word label drawn from pos and neg, in the same format as the examples. A prompt with no examples (zero-shot) or one example (one-shot) works the same way; “few-shot” just means a handful. Brown et al. popularized the term for GPT-3, reporting that performance on many tasks improved as examples were added to the prompt.

What is and is not being learned

The word “learning” refers to the task being specified inside one forward pass, not to a parameter update. Three things follow.

  1. Nothing persists. Remove the examples and the behavior is gone. The next request starts from the same weights.
  2. The capability is already in the weights. Pre-training on text that contains many task patterns is what lets the model use a prompt this way. Why a trained transformer can do it is still being studied; Xie et al. interpret it as inferring a latent task from the examples, and other work analyzes it as an implicit learning algorithm. These are explanations under particular assumptions, not settled mechanisms.
  3. Examples carry more than labels. Min et al. find that, for the tasks and models they studied, the label space, the input distribution, and the format of the demonstrations matter greatly, while randomly replacing the labels in the demonstrations hurt less than one might expect. Treat examples as signals about format and domain as much as about the correct answer.

Sensitivity and limits

Few-shot behavior is brittle in ways ordinary supervised training is not. Lu et al. show that for some tasks the same examples in a different order can swing accuracy from near state of the art to near chance. Changing the wording of the instruction, the choice of examples, or which label appears most often among them can also shift results. Practical consequences:

  • Evaluate a prompt on a held-out set, not on the examples shown in it.
  • Vary the order and sample of examples when comparing prompts, and report the spread.
  • Expect the context window to cap the number of examples, and cost to scale with the prompt length because every request pays for every example token.

ICL does not store new knowledge in the weights. Examples or a retrieved passage can supply a previously unseen fact or label relation for the current request; once that context is removed, the fact is not retained through a parameter update. A prompt that supplies neither the fact nor a way to infer it cannot recover it merely by specifying a format.

Compared with neighbors

  • Fine-tuning updates weights from many examples; the change persists and the examples cost nothing at inference time. ICL needs no training run but pays per request and is less stable.
  • Prompt engineering is the practice of choosing instructions and examples; ICL is the model behavior that makes it work.
  • Retrieval-augmented generation places retrieved documents in the prompt to supply facts, and uses the same mechanism of conditioning on context.
  • Chain-of-thought prompting is a way of writing the examples so they include intermediate reasoning, as an extension of few-shot prompting.

References


Discover more from Insightful Data Lab

Subscribe to get the latest posts sent to your email.

Similar Posts

Questions, corrections, or additional insights?

This site uses Akismet to reduce spam. Learn how your comment data is processed.