TL;DR
In-context learning is the ability of a language model to pick up a new task from examples written into its prompt. You show it a few input-output pairs, then give it a new input, and it continues the pattern. Its weights do not change. The examples live only in the text the model reads, and they shape the output through the same next-token prediction the model was trained on.
Where you'll see it: few-shot prompting of large language models, where a handful of worked examples in the prompt replace a task-specific training run, and chain-of-thought prompting, where the examples show the reasoning as well as the answer.
Intuition
A new job from a sample page
Imagine a skilled translator who has never heard of your company's jargon. You hand over three filled-in examples of the form "product code, then the matching internal name", then a fourth code with the name left blank. The translator does not study a manual. They see the pattern in the examples and complete the blank. The knowledge was already there. The examples only told them which task to do.
A Language Model works the same way. Training taught it to predict the next token across a huge amount of text, and much of that text has patterns: lists, question and answer pairs, translations, code with its documentation. So when a prompt contains examples, the most likely continuation is often the next answer in that same pattern.
Why no weight update is needed
Fine-tuning changes the parameters so that the model does better on a task. In-context learning leaves the parameters alone. Everything the model needs for the task arrives as text in the prompt, read through the same Transformer layers that handled the training data. The prompt is a context window, and the model's attention decides which earlier examples matter for the answer being written.
flowchart LR A["Demonstration 1: input and label"] --> D[Prompt] B["Demonstration 2: input and label"] --> D C["New input, label left blank"] --> D D --> M[Frozen model] M --> O[Predicted label]
The model's weights are the same for every prompt. Changing the examples changes the task, and the same weights can be pointed at many tasks in turn.
The math
Setting
Let be the fixed parameters of a language model that assigns a probability to a sequence of tokens. Let be demonstration pairs, where each is an input and is its desired output. Let be a new input. Write the prompt as the text formed by concatenating the demonstrations with the new input, and let be that text.
The prediction rule
The model answers by scoring each candidate label as a continuation of the prompt. Using the definition of conditional probability, the score is
For a label made of several tokens, the chain rule splits this into one next-token probability per token, each conditioned on everything before it:
The predicted label is the candidate with the highest score:
Here is the set of allowed labels. Nothing in this rule updates . The demonstrations only change the conditioning, so the same model gives different predictions for different .
Zero, one and few shots
The usual terms count the demonstrations. Zero-shot prompts give the task description and the input only. One-shot prompts add a single pair. Few-shot prompts add pairs, with typically between a handful and a few dozen, limited by how much text fits in the context window. The number of pairs is the only thing that changes between these settings.
Why it works in the Transformer
In a Transformer, each new token is computed by attending over all earlier tokens in the context. Attention weights for a query token are a Softmax over scores against every earlier key:
Because the weights are computed from the content, a token in the new input can attend to a demonstration with a similar input and copy its format or label. This copying behaviour is one plausible mechanism. It is a description of what the attention can do, not a full account of what the model learns from the examples.
Worked examples
Prompt:
Review: The plot dragged. -> negative
Review: A warm, funny film. -> positive
Review: I would watch it again. ->Suppose the model's scores for the two label strings, as log-scores before normalising, are 2.1 for "positive" and 0.4 for "negative". Normalise over the two allowed labels with a softmax, so the two scores become probabilities that sum to 1 (this is the usual way to compare candidates when the full sequence probabilities are not reported).
- Exponentiate: and .
- Sum: .
- Probabilities: and .
- Prediction: positive.
Interpretation: the two examples establish the format "review, arrow, label". The model reads the last review as the same kind of item and prefers the label that matches its warm tone. If the demonstrations were removed, the same model could produce a different score, because the conditioning changed. The weights did not move.
A common prompt uses 8 demonstrations of 40 tokens each, plus a 30-token query.
- Total prompt length: tokens.
- Without demonstrations the prompt is 30 tokens long.
- Full self-attention compares every pair of positions. Pair counts are and .
- Ratio: .
Interpretation: the demonstrations are cheap in bytes and expensive in computation, because attention work grows with the square of the context length. Many systems cache the attention keys and values of a shared prefix so that a fixed set of examples does not have to be reprocessed for every query, but the prompt still occupies the window and still costs memory.
Tradeoffs and failure modes
In-context learning is fast to try and needs no training pipeline. It also has limits that are easy to miss.
| Situation | Problem | Better choice |
|---|---|---|
| The task needs many examples to learn | The context window fills, and the examples crowd out the input. | Fine-tune on the task or retrieve only the most relevant examples. |
| The task is stable and high volume | Each request repays the cost of the examples. | Fine-tune, or distil the prompt into the weights. |
| The output format is strict | Small changes in examples shift the format, and results vary between runs. | Use constrained decoding or a schema, and check outputs. |
| The examples are ordered badly | The most recent example can bias the prediction (recency bias). | Shuffle the order, balance the labels, and test several orders. |
| The examples are wrong | The model copies the errors. | Clean the examples first. |
Research on demonstrations has found that the label correctness of the examples matters less than one might expect, while the format, the label space and the input distribution matter more (Min et al., 2022). This is a finding about particular models and tasks, so treat it as a reason to test rather than as a rule. Performance also depends strongly on model size. Small models often gain little from examples, while larger models gain a lot.
History
Early neural language models were trained for next-word prediction, and tasks were handled by training a separate model for each one. Pretraining on large text and then fine-tuning became the common pattern around 2018, with transfer from models such as BERT and GPT.
The clearest demonstration of in-context learning came in 2020. Brown and colleagues trained GPT-3, a model with 175 billion parameters, and reported that it performed many tasks from a few examples in the prompt, without gradient updates. The paper gave the zero, one and few-shot framing that is still in use. The result showed that scale changed what a model could do from a prompt, which made prompt design a skill in its own right.
The ideas were taken further by work on chain-of-thought prompting in 2022, where the examples include step-by-step reasoning. That work is covered in Chain-of-Thought Prompting.
Check yourself
QuestionWhat changes between zero-shot, one-shot and few-shot prompting?Show answer
Only the number of demonstration pairs in the prompt. The model and its weights are the same. Zero-shot has none, one-shot has one, and few-shot has several, limited by the context window.
QuestionA model gives the label 'yes' a score of 1.0 and the label 'no' a score of 0.0 as log-scores. What probability does a softmax over those two labels assign to 'yes'?Show answer
The exponentials are and . The sum is . So , about 73 percent. The prediction is 'yes', but the model is not certain.
QuestionWhy does adding demonstrations not change the model's weights, and where does the adaptation happen?Show answer
The prompt is only input text. The forward pass computes next-token probabilities conditioned on it, and no gradient step is taken. The adaptation happens in the conditioning: the attention layers read the examples and the output reflects the pattern they show. When the prompt is removed, the model returns to its default behaviour.
QuestionYou are classifying ten thousand support tickets a day with a fixed set of twelve examples in the prompt. Why might fine-tuning be worth considering?Show answer
Every request pays the cost of the twelve examples, which takes context and computation, and the examples must be sent each time. A fine-tuned model has the task built into its weights, so it needs only the ticket in the prompt. If the examples change rarely and the volume is high, the saving repays the cost of the training run. The tradeoff should be checked with measured accuracy on held-out tickets.