Skip to main content

In-context learning

Adapting from a prompt without updating model parameters

Lesson objective. Distinguish zero-shot, one-shot and few-shot, then explain how behavior can change without a parameter update.

Lesson skeleton
  • Token-ID context → In-context learning · Explain the effect of a supplied example with fixed parameters

See the path

Review this lesson

In-context learning (ICL) is a model’s ability to adapt its response to examples, instructions, or patterns provided directly in the prompt. This adaptation happens during inference: the model’s parameters remain fixed. The model uses the supplied context; it does not thereby perform another fine-tuning step.

Zero-shot, one-shot, and few-shot

These terms describe how many demonstration examples are included in the prompt:

  • Zero-shot: an instruction describes the task, with no solved example.
  • One-shot: one example illustrates the expected format or transformation.
  • Few-shot: several examples provide patterns for the model to continue.

A classification example:

Text: “The parcel arrived late.”
Sentiment: negative

Text: “Delivery was quick and careful.”
Sentiment:

The first example provides a local demonstration. The model can use it to produce a consistent label for the second text even though no weight update occurs.

The term’s scope varies across the literature: some authors reserve in-context learning for demonstrations supplied in the prompt (especially few-shot settings), while others use it more broadly for instruction-conditioned adaptation. This path uses the broader operational definition while keeping zero-shot, one-shot, and few-shot settings distinct.

What does the model do with examples?

The model receives a sequence containing both the task and the demonstrations. At each layer, attention and other network operations transform representations as a function of this context. When generating the continuation, the model conditions its output on the preceding examples. The examples are part of the active input: they steer the computation without changing the weights.

Why this is not fine-tuning

Operation Where is adaptation information supplied? Do parameters change?
Pretraining Training corpus Yes, through weight optimization
Fine-tuning Additional training examples Yes
In-context learning Current prompt No
RAG Retrieved documents added to the context No, not through retrieval itself

ICL is temporary and conditioned on the prompt’s content and layout. A new conversation or a different prompt can lead to different behavior. Fine-tuning changes parameters and can produce adaptation that persists beyond a particular prompt.

Where does adaptation happen?

The diagram separates two operations that are often conflated: supplying information in an individual call’s context and changing model parameters during training. RAG supplies information to the first path; retrieval alone does not update the weights.

flowchart TD
  subgraph inference["Inference: fixed parameters"]
    P["Prompt: instruction + examples"] --> C["Call context"]
    R["Retrieved documents (RAG)"] --> C
    W["Model parameters θ"] --> M["Model at inference"]
    C --> M
    M --> O["Response"]
  end
  subgraph training["Fine-tuning: parameter update"]
    D["Training examples"] --> U["Loss computation and optimization"]
    U --> W2["Updated parameters θ′"]
  end

Key distinction: ICL and RAG change the information available to a call; fine-tuning changes the parameters used by subsequent calls. Pretraining is another optimization phase, generally at larger scale, using a training corpus.

What ICL enables — and its limits

ICL can help change output formats quickly, provide a local convention, demonstrate a task with a few examples, or guide structured extraction without training a new adapter.

However, results depend strongly on:

  • the quality, number, and order of examples;
  • instruction wording and the distribution of examples;
  • context length and information position;
  • similarity between examples and the case being solved;
  • the model’s ability to generalize beyond superficial patterns.

A model may reproduce a format without identifying the relevant rule. It may also be sensitive to example order, contradictory demonstrations, or irrelevant details. ICL therefore guarantees neither correct generalization nor human-like understanding of the task.

Computing a continuation conditioned on the examples describes the observable behavior. Research on the mechanisms behind this adaptation examines pattern recognition, inference about the rule that generated the examples, implicit learning of algorithms during pretraining, and attention circuits that facilitate copying or associations between examples.

Induction heads are one mechanism studied in some Transformers: attention heads can help detect a previously seen sequence and continue its pattern. They illuminate some copying and continuation behaviors, but are not a universal explanation for every form of ICL.

To distinguish rule learning from simple imitation:

  1. compare zero-shot, one-shot, and few-shot settings on the same tasks;
  2. permute example order and measure sensitivity;
  3. compare relevant examples with superficially similar but irrelevant ones;
  4. test novel cases that require applying the rule rather than copying;
  5. control prompt length and position effects;
  6. repeat trials with multiple phrasings and sampling seeds.

A good protocol separates success on seen examples from generalization to unseen cases.

Key points

  • ICL uses the prompt as a temporary adaptation medium.
  • Model weights remain fixed during in-context adaptation.
  • Examples can specify a task, format, or local pattern.
  • The precise mechanisms are still under investigation; induction heads are only one possible component.
  • Success on a few examples does not demonstrate robust generalization.

Relation to context engineering

ICL describes the model’s behavior in response to information in its prompt. Context engineering is the system-design work that selects and organizes that information before the call. It can therefore create better conditions for ICL, but it is not the same phenomenon. See the Context engineering lesson.

Language: Français · English

Practice

The questions and exercises for this lesson are collected on its dedicated practice page.

Supplementary resources Experiment, explore further, and practise the ideas from this lesson.

References