You paste three example emails into a chatbot, ask for a fourth in the same style, and it gets the style right. No one retrained the model. Nothing was installed. The model just read your examples and adjusted. Researchers call this in-context learning, and it is one of the odd, useful behaviors of large language models.
This explainer covers what the term means, what the GPT-3 paper measured, whether the model really learns from your examples, and two theories about how it works inside. Every number comes from one of five papers, and we name the table or experiment it belongs to. For practical templates, see our guide to few-shot prompting with examples and templates. This post covers the research behind it.
What in-context learning means
Later papers cite the GPT-3 paper for the term. That paper is "Language Models are Few-Shot Learners" by Brown et al. at OpenAI (arXiv 2005.14165). The authors describe a model that is conditioned on "a natural language instruction and/or a few demonstrations of the task" and then completes new instances "simply by predicting what comes next." They use the term "in-context learning" for the inner loop of this process, which "occurs within the forward-pass upon each sequence." A forward pass is one run of the model from input text to output text.
A demonstration is a worked example. For translation, it is an English sentence followed by its French version. The paper sorts prompts into three settings by how many demonstrations the model gets:
- Zero-shot: no demonstrations, only an instruction in plain language.
- One-shot: one demonstration, plus the instruction.
- Few-shot: as many demonstrations as fit in the model's context window. The paper says this is typically 10 to 100, because GPT-3's window held 2,048 tokens.

The paper is clear that none of this changes the model. For all tasks, GPT-3 "is applied without any gradient updates or fine-tuning, with tasks and few-shot demonstrations specified purely via text interaction with the model." Fine-tuning, by contrast, updates the weights of the model on a set of labeled examples. In-context learning leaves the weights as they are. The examples live only in the prompt.
The paper also credits earlier work. Its author contributions list says Alec Radford "originally demonstrated few-shot learning occurs in language models," and the introduction points to the GPT-2 paper (Radford et al., 2019) as an earlier attempt that "achieves only 4% on Natural Questions." The 2020 paper tested the idea on GPT-3, a model with 175 billion parameters, which its abstract describes as 10x more than any previous non-sparse language model.
What GPT-3 showed
The paper tested GPT-3 on more than two dozen NLP datasets. It reports that few-shot results are "sometimes competitive with or even occasionally surpasses" the state of the art held by fine-tuned models. The numbers show both the gain and its limits. Here is the paper's Table 3.3, on three closed-book question answering datasets. The first three figures are fine-tuned systems, and the last three are GPT-3 with no fine-tuning (percent accuracy):
- Natural Questions: fine-tuned RAG 44.5, T5-11B+SSM 36.6, T5-11B 34.5. GPT-3 zero-shot 14.6, one-shot 23.0, few-shot 29.9.
- WebQuestions: fine-tuned RAG 45.5, T5-11B+SSM 44.7, T5-11B 37.4. GPT-3 zero-shot 14.4, one-shot 25.3, few-shot 41.5.
- TriviaQA: fine-tuned RAG 68.0, T5-11B+SSM 60.5, T5-11B 50.1. GPT-3 zero-shot 64.3, one-shot 68.0, few-shot 71.2.
Counting every cell, GPT-3 beat a fine-tuned system in these ways:
- TriviaQA, few-shot 71.2: above all three fine-tuned systems, including RAG at 68.0.
- TriviaQA, one-shot 68.0: matches RAG at 68.0 and is above the other two.
- TriviaQA, zero-shot 64.3: above T5-11B+SSM (60.5) and T5-11B (50.1), below RAG.
- WebQuestions, few-shot 41.5: above T5-11B (37.4), below T5-11B+SSM (44.7) and RAG (45.5).
- Every other GPT-3 cell, including all three Natural Questions cells and the WebQuestions zero-shot and one-shot cells, is below all three fine-tuned systems.
The table's caption says the TriviaQA few-shot result was evaluated on the wiki split test server. RAG is the highest-scoring fine-tuned system in each of the three columns, and the paper describes it as using a learned retrieval step over a large document index, while the GPT-3 setting allowed no outside content. Adding examples helped on every dataset. The gain from zero-shot to few-shot was larger on WebQuestions and Natural Questions than on TriviaQA, and the paper says this may suggest a distribution shift, meaning the style of those questions differs from what the model saw in training.
The paper's invented word tasks make the point more sharply, because they are unlikely to appear in training text. Table 3.10 gives results for the full GPT-3 175B model. For the "random insertion in word" task, a random punctuation mark or space is put between each letter of a word, and the model must output the original word. The scores are:
- Zero-shot: 8.26
- One-shot: 45.4
- Few-shot: 67.2
That is three rows, and accuracy rises at each step. (The paper's text, in its discussion of Figure 3.11, gives 66.9 percent for the few-shot score on this task. Table 3.10 lists 67.2. We use the table.) The authors conclude that "the model really does appear to learn these tasks at test time," because it cannot do them zero-shot and the tasks are unlikely to be in its training data, though they add that they cannot confirm this with certainty. They also report that none of the models could reverse the letters in a word. On that task, Table 3.10 shows 0.09, 0.48 and 0.44 across zero-shot, one-shot and few-shot, so examples did not help there.

The authors also flag an open question. In their limitations discussion they write that it is unclear "whether few-shot learning actually learns new tasks 'from scratch' at inference time, or if it simply recognizes and identifies tasks that it has learned during training." They suggest that this may vary by task: invented word puzzles seem more likely to be learned on the spot, while translation "clearly must be learned during pretraining." The next three papers each try to answer part of this question.
Do the examples need to be correct?
It is natural to expect few-shot examples to work like training data, where correct answers matter a great deal. "Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?" by Sewon Min and colleagues (arXiv 2202.12837) tested this directly.
The setup: 12 models (six language models, each run with two inference methods, called direct and channel), 26 datasets, and 16 examples per prompt by default. The tasks were classification and multiple choice. Model sizes ran from 774 million to 175 billion parameters, and the two largest models, fairseq 13B and GPT-3, were tested on a subset of 6 datasets with 3 random seeds because of limited resources.
The main result: replacing the correct labels in the examples with random labels "barely hurts performance." The paper reports a drop of 0 to 5 percent absolute for nearly all models, with an average drop of 1.7 percent for multiple-choice tasks and 2.6 percent absolute for classification tasks. The models still reached non-trivial performance. The authors call this counter-intuitive, because correct pairing is critical in typical supervised training.
The paper then pulled the examples apart to see what does matter. It names four parts of a demonstration: the input-label mapping (is each input paired with the right label), the distribution of the input text, the label space (which answers are possible), and the format (pairs of input and label). Its findings:
- The input text matters. Swapping the example inputs for random sentences from an outside corpus dropped performance by 3 to 16 percent absolute in three of the four model setups the paper used for this test. Direct MetaICL was the exception.
- The label space matters. For direct models, swapping the real label options for random English words dropped performance by 5 to 16 percent absolute. For channel models the drop was 0 to 2 percent, or sometimes a gain.
- The format matters. Removing the input-label pairing gave results about the same as, or worse than, no examples at all. With the format kept, using only the inputs or only the labels could retain up to 95 percent of the gain from full examples.
- More examples did not help much past a point. In the paper's ablation (Figure 5), performance did not rise much once the number of examples reached 8 or more, with gold or random labels.
The paper's discussion is careful. If "learning" means capturing the correct input-label mapping from the examples, then, in the authors' words, their findings suggest that "LMs do not learn new tasks at test time." If learning is read more broadly, as adapting to the input style, the label set and the format, then the model does learn from the demonstrations. The authors suggest the model already holds the skill from pretraining and the examples help it find the task. They also warn that in-context learning may not work on a task whose input-label link is not already captured in the model.
The limits matter. The paper tested classification and multiple-choice tasks only. It reports larger gaps for some dataset and model pairs, up to nearly 14 percent absolute on the financial_phrasebank dataset with GPT-J. And it notes that later work (Kim et al., 2022) found that negated labels substantially lower performance in classification. So "labels do not matter" is not a safe rule.
Bigger models behave differently
"Larger language models do in-context learning differently" by Jerry Wei and colleagues at Google Research (arXiv 2303.03846) picks up the point where Min et al. leave it. The paper separates two ways a model can succeed: use its prior knowledge of what labels mean, or learn the mapping shown in the examples. It tested GPT-3, InstructGPT, Codex, PaLM and Flan-PaLM models on up to seven NLP tasks, with 16 examples per class by default and 100 test examples per dataset.
Test one flipped the labels in the examples. A positive review was labeled "negative," and the reverse. A model that follows the examples should then score below chance. The paper found that small models stayed flat and often did not drop below chance, even with all labels flipped. Large models did drop well below chance. Its example: text-davinci-002 fell from 90.3 percent accuracy with no flipped labels to 22.5 percent with all labels flipped. The paper treats GPT-3 models as "small" for this test, because they could drop to chance but not below it.
Test two replaced the labels with unrelated words, such as "foo" and "bar" instead of "negative" and "positive," so the model has no hint from the label meaning. Small models lost much more accuracy than large ones, and large models lost very little. For some datasets only PaLM-540B scored above chance.
The authors call both abilities emergent with model scale. The paper also found that instruction tuning, which is extra training on tasks written as instructions, strengthens both abilities, "but more of the former," meaning the use of prior knowledge.
The two papers fit together. On a two-class task, random labels are a 50 percent flip, which is noise, while fully flipped labels contradict what the model knows in a consistent way. The abstract says large models "can override semantic priors when presented with in-context exemplars that contradict priors." For small models, the paper calls its result consistent with Min et al.
How it might work inside the model
Two research lines offer mechanisms. Neither is a complete answer, and both papers say so.
Induction heads
"In-context Learning and Induction Heads" by Catherine Olsson and colleagues at Anthropic (published on the Transformer Circuits Thread in March 2022, with an arXiv copy at 2209.11895) starts with a different definition. Instead of few-shot tasks, it measures how the model's loss, a score for prediction error, falls as the text gets longer. Later words are easier to predict than earlier ones, and the paper treats that gain as in-context learning. It notes the two views: Brown et al. focus on few-shot learning of specific tasks, and Kaplan et al. focus on the loss at different positions in the text.
The mechanism it studies is the induction head, an attention head (a part of the transformer that looks back over the text) that completes patterns. If the text so far has the pattern [A][B] ... [A], the head predicts [B]. The paper says this works by a pair of heads: a "previous token head" copies information about the preceding word, and the induction head uses it to find earlier places where the current word occurred and copy what came next. The authors report that these heads occur in 2-layer attention-only models but not in 1-layer models.
The evidence the paper reports:
- In models of every size with more than one layer, in-context learning rises sharply in one narrow window early in training, roughly 2.5 to 5 billion tokens. Before the window the score was under 0.15 nats (a unit of loss), and after it roughly 0.4 nats. Induction heads form in the same window.
- When the authors changed the architecture so that induction heads form at a different time, the jump moved with it.
- When they removed induction heads from small models at test time, in-context learning dropped sharply. For small attention-only models, they say almost all in-context learning appears to come from these heads.
The paper is open about how far this goes. It calls the evidence "preliminary and indirect." It reports strong, causal evidence for small attention-only models and correlational evidence for larger models with MLP layers, and says it lacks ablations for full-scale models. The analysis covers 34 transformers and more than 50,000 attention head ablations. The claim that induction heads drive most in-context learning in large models is a hypothesis the authors propose, not a result they prove.
Gradient descent in the forward pass
"Transformers learn in-context by gradient descent" by Johannes von Oswald and colleagues at ETH Zurich and Google Research (arXiv 2212.07677, ICML 2023) offers a second idea. Gradient descent is the training method that adjusts weights in small steps to reduce error. The authors show that one layer of a simplified attention design can be built to perform one step of gradient descent on a regression problem. They then trained transformers made only of such layers on simple regression tasks. The trained models either converged to the construction or produced linear models that closely matched ones trained by gradient descent. The authors say trained transformers become "mesa-optimizers," that is, they "learn models by gradient descent in their forward pass."
The paper limits itself on purpose. The experiments use simple regression tasks and linear self-attention-only transformers, a simplified design. The authors write that they "expect our findings on linear self-attention-only Transformers to only explain a limited part of a complex process, and it may be one of many possible methods giving rise to in-context learning." They also acknowledge related work on the same hypothesis by Akyurek et al. (2023), done in parallel, and Garg et al. (2022). That in-context learning in Transformers is driven by gradient descent is a hypothesis the paper puts forward, not a result it proves for full-scale language models. The paper also says its mechanism "significantly differs" from the copying mechanism of induction heads, yet elsewhere says induction heads "could be understood as a specific case" of in-context learning by gradient descent, and it names copying through attention as the second crucial mechanism. How the two ideas fit together is therefore still open.
What this means when you write prompts
The papers measured benchmark tasks and simplified models, not your daily use. With that caution, these takeaways are tied to what they found:
- Show the format. Min et al. found that removing the input-label pairing gave results about the same as, or worse than, no examples. If you want a certain output shape, show it in pairs.
- Use realistic inputs and the real set of answers. The same paper found that the input text and the label space both mattered.
- Keep your examples correct anyway. Min et al. found random labels cost little on their benchmarks, but they also report outliers, and Wei et al. found that large models follow consistently flipped labels. If your examples teach the wrong pattern, a large model may learn it.
- Test how many examples you need. Brown et al. found accuracy on their symbol task rose with more examples, while Min et al. saw little change past 8 examples in their setup. Results vary by task and model.
- For problems that need steps, see our chain-of-thought prompting explainer, and for the building blocks behind all of this, our plain-English guide to transformers.
What is still not settled
Four questions stay open. First, whether in-context learning is true learning or task recognition, which Brown et al. and Min et al. both raise. Second, how much of it induction heads explain in large models, which Olsson et al. call circumstantial. Third, whether the gradient descent result carries from simple regression to language models, which von Oswald et al. state as a hypothesis. Fourth, whether findings from classification and multiple-choice tests carry over to open-ended writing, which Min et al. state they leave to future work.
All five papers date from 2020 to 2023 and tested the models and tasks of their time. Treat their numbers as measurements of those experiments, not of any model you use today. For more technical treatments of two ideas above, see our posts on induction heads and in-context learning as implicit gradient descent.
Sources
- Language Models are Few-Shot Learners, Brown et al., OpenAI, 2020
- Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?, Min et al., 2022
- Transformers learn in-context by gradient descent, von Oswald et al., ICML 2023
- In-context Learning and Induction Heads, Olsson et al., Anthropic, Transformer Circuits Thread, March 2022
- Larger language models do in-context learning differently, Wei et al., Google Research, 2023
