Give a trained language model a few worked examples in its prompt, and it will often solve a new instance of the same task without a single gradient update. Show it three English-to-French translations, then a fourth English sentence, and it translates. Show it a few input-output pairs from an unknown function, and it starts to approximate that function. No weights change, no fine-tuning run, no new training data is stored anywhere except in the prompt itself.
This is in-context learning, and it is stranger than it first looks. The model is not merely retrieving a memorised answer, because the task in the prompt may never have appeared in training. It is not learning in the ordinary sense either, because nothing persists after the prompt ends. Something in the forward pass alone is doing the work of an algorithm: reading examples, inferring a rule, and applying it to a query. Understanding how that happens, and where it breaks, matters for anyone who uses few-shot prompting, builds agents on top of language models, or tries to reason about what these models can and cannot be trusted to do.
What in-context learning is, precisely
In few-shot prompting, the input has a simple structure: a sequence of demonstration pairs, followed by a query.
The model must produce . The demonstrations define a task implicitly. They might be translations, sentiment labels, arithmetic examples, or pairs from a function such as with unknown coefficients. The model receives no explicit description of the task and no gradient signal. All of the adaptation must happen inside one forward pass.
Three properties distinguish this from ordinary supervised learning. First, the adaptation is transient: change the prompt and the behaviour changes, with the weights untouched. Second, the task is specified by examples, not by code or by a task identifier. Third, the same model can switch tasks from prompt to prompt, sometimes within a single conversation. A model that can do this has, in some sense, learned to learn, or at least learned to simulate learning.
It is worth distinguishing in-context learning from two nearby phenomena. Retrieval is not the same thing: a model that copies a demonstration answer verbatim is not generalising. Memorisation is not the same thing either: a model that has seen the exact task in training may simply recognise it. The interesting case is generalisation to a task that is new in its details, where the demonstrations are the only source of task information. That is the case experiments try to isolate, and it is the case that prompts in practice usually approximate.
Why a forward pass can behave like a learning algorithm
A transformer has no explicit training loop at inference time, but its architecture contains machinery that can implement learning-like computation. The key ingredient is attention. An attention head can compare the query to earlier tokens, weight them by similarity, and mix their information into the query representation. Stacked across layers, this becomes a surprisingly general computational substrate.
The cleanest theoretical picture comes from a simplified setting: linear regression in context. Suppose the demonstrations are pairs with for some unknown weight vector , and the query is a new . A model that has learned to do in-context linear regression must, in effect, estimate from the demonstrations and then compute .
It turns out that a single linear attention layer can implement one step of gradient descent on this problem. Consider the least-squares loss over the demonstrations:
One gradient step from an initial with step size is:
The prediction for the query after this step is , which expands to a term depending on plus a sum over demonstrations of terms involving and the inner product . That structure, a weighted sum over past tokens where the weights depend on similarity to the query, is exactly what attention computes. With the right choice of key, query, and value projections, a linear attention head reproduces this gradient step. Stacking layers corresponds, loosely, to taking further steps, or to implementing a more sophisticated estimator.
This is not a claim that real language models literally run gradient descent in their heads. It is a proof of concept: the forward pass of an attention-based model is expressive enough to implement a learning algorithm, and training on many tasks can push it toward doing so. In the linear case, researchers have trained small transformers from scratch on random regression tasks and found that they learn to implement something close to ridge regression or to a Bayesian estimator, depending on the training distribution. The model does not store a weight vector anywhere. The "learning" is in how activations are routed and combined.
A Bayesian view gives complementary intuition. If the pretraining distribution contains many tasks drawn from some prior, then the optimal predictor for a new prompt is the posterior predictive distribution: average over tasks weighted by how well each explains the demonstrations. A transformer trained to minimise next-token loss over such a mixture is, in principle, learning to approximate that posterior predictive computation. In-context learning is then Bayesian inference carried out by a neural network, with the prompt as the observed data and the model weights as the prior. This view predicts several observed behaviours, including sensitivity to the distribution of demonstrations and graceful degradation when demonstrations are noisy, and it explains why the same architecture can appear to implement different algorithms on different task families: the posterior depends on the prior, and the prior is whatever pretraining taught the model.
What emerges during training, and when it does not
In-context learning is not present in a randomly initialised model. It emerges during training, and the conditions for emergence are informative. Models trained on a diverse mixture of tasks, with enough capacity and enough training, start to show it. Models trained on a single task, or on tasks that are too easy to memorise individually, often do not.
One striking finding is that in-context learning can emerge and then fade. In controlled experiments, a model trained on a fixed set of tasks first learns to use the context, then, as training continues, shifts toward memorising the tasks in its weights and ignoring the context. The context-based solution is useful early, when memorisation is hard, but the weight-based solution eventually wins on training loss if the task set is finite. This suggests that the in-context ability of large pretrained models may depend on the sheer diversity of their training data: with effectively endless distinct tasks, memorisation never fully catches up, so the context-based strategy stays competitive.
Scale matters, but not in a simple way. Larger models generally show stronger in-context learning, and the ability appears more reliably above certain sizes for many benchmarks. Yet scale alone does not explain it. The distribution of training tasks, the length of training, and the format of the data all matter. A model trained mostly on documents with no demonstration-like structure may still learn in-context behaviour, because natural text contains many implicit demonstrations, such as lists, parallel sentences, and repeated patterns. But a model trained on data that is deliberately structured as tasks and examples tends to show it more cleanly.
There is also a distinction between learning a task from examples and recognising a task from examples. Much of what looks like in-context learning in large models may be task recognition: the demonstrations remind the model of a task it already knows, and it then applies its stored knowledge of that task. True in-context learning, where the model infers a genuinely novel mapping, is harder to demonstrate and probably rarer. In practice, prompts sit somewhere on a spectrum between recognition and inference, and the distinction matters for reliability: a recognised task will be performed robustly, while a genuinely novel mapping inferred from a few examples will be fragile.
A practical example: learning a function from examples
Consider a concrete, small-scale version of the problem that you can test with any capable model. Pick a function the model is unlikely to have memorised as a named rule, for example
restricted to two-digit inputs, and present it as pairs:
Input: 12 -> Output: 43
Input: 5 -> Output: 22
Input: 31 -> Output: 100
Input: 8 -> Output: 31
Input: ?
A model doing genuine in-context learning should infer the affine rule and answer correctly for a new input. In controlled studies with small transformers trained on random affine functions, this is exactly what happens, and the accuracy improves with the number of demonstrations, much as a statistical estimator improves with more data.
Now change the setup slightly, and the fragility appears. Give the demonstrations in a scrambled order, or with one label flipped, or with inputs drawn from a narrow range, and accuracy drops, sometimes sharply. Give demonstrations that are consistent with two different rules, and the model may pick the rule that is more common in pretraining rather than the one that best fits the examples. These are not bugs in a narrow sense. They are the expected behaviour of a system that is approximating Bayesian inference under a prior shaped by pretraining, rather than running an exact learning algorithm.
The same pattern shows up in real prompting. Adding more examples usually helps, up to a point, then plateaus or even hurts as the prompt gets long and the model attends less reliably to early examples. Ordering matters: examples near the end of the prompt often have more influence than examples at the start, a recency effect. Formatting matters: a model may perform well with one delimiter and poorly with another, because the format changes which pretraining patterns the prompt resembles. None of this is surprising once you see in-context learning as approximate inference under a learned prior, but all of it is easy to miss if you treat few-shot prompting as a clean, deterministic interface.
Limitations and common pitfalls
In-context learning has sharp limits, and prompt engineering that ignores them tends to fail in predictable ways.
- It is not persistent learning. Nothing learned in one prompt carries to the next, except through the conversation history itself. If you need a model to retain a new fact or skill across sessions, in-context learning is the wrong tool. Fine-tuning, retrieval, or an external store is needed.
- It is sensitive to the demonstration distribution. The model infers a task from the examples you show. If those examples are unrepresentative, biased, or mislabelled, the inferred task will be wrong, and the model will apply the wrong rule confidently. A few noisy labels can be worse than fewer clean ones.
- It degrades with prompt length and complexity. Very long demonstration sets, tasks that require many steps of reasoning, and tasks whose rule is complex relative to the number of examples all stress the mechanism. Attention is not perfect memory, and early demonstrations can be effectively forgotten in a long prompt.
- It confuses recognition with inference. A model may appear to learn a task from examples when it is really recognising a familiar task. When you then change the task in a way that breaks the familiar pattern, performance can collapse, even though the examples still define the new task clearly.
- It inherits pretraining biases. The prior that shapes in-context inference is the pretraining distribution. If that distribution associates a pattern with a particular answer, the model may prefer that answer even when the demonstrations point elsewhere. This is a source of systematic, hard-to-debug errors in few-shot classification and in tasks involving stereotypes or skewed label distributions.
- It is not a substitute for evaluation. Few-shot accuracy on a handful of hand-picked prompts is a weak signal. The same prompt with a different example order, a different format, or a different sample of demonstrations can give a materially different result. Report performance across prompt variations, not just the best one.
A related pitfall is treating in-context learning as if it were gradient-based fine-tuning with the same guarantees. Fine-tuning changes weights and can be validated on held-out data from the target task. In-context learning changes only activations, and its behaviour on a new query is a function of the whole prompt, including irrelevant details. Debugging it therefore requires thinking about the prompt as data, not as code.
When to rely on it, and when to use something else
Use in-context learning when the task is well-defined by a handful of examples, when the cost of fine-tuning is not justified, when you need to switch tasks quickly, or when you are prototyping and want fast feedback. It is excellent for formatting tasks, for style transfer, for classification with a small label set, and for tasks where the model already has strong prior knowledge and the examples mainly serve to specify the format or the label mapping.
Prefer fine-tuning when the task is stable, when you have substantial labelled data, when reliability across prompt variations matters, or when the model must learn something genuinely absent from pretraining. Prefer retrieval-augmented generation when the task requires facts that are not in the model's weights and not inferable from a few examples. Prefer a tool, a calculator, or code execution when the task requires exact computation over many steps, because in-context inference is approximate and error-prone for long chains of arithmetic or logic.
A useful rule of thumb: if you can state the rule in words, state it. Examples alone are a noisy channel for specifying a task. Combining a short instruction with a few examples usually beats examples alone, because the instruction narrows the hypothesis space that the model must search. This is why instruction tuning and few-shot prompting work well together, and why pure few-shot prompting without any instruction is often unnecessarily fragile.
Takeaway
In-context learning is best understood not as magic, and not as the model "learning" in the human sense, but as a forward pass implementing an approximate learning algorithm. Attention provides the machinery, pretraining provides the prior, and the prompt provides the data. The gradient-descent picture for linear regression and the Bayesian picture for task mixtures are two lenses on the same phenomenon, and both explain why the ability emerges, why it is sensitive to examples and order, and why it fails when it does.
The practical consequence is simple: treat a prompt as a small dataset, not as a command. Curate its examples the way you would curate training data, test it across variations the way you would test a model, and do not expect persistence, exactness, or robustness that the mechanism cannot provide. Used that way, in-context learning is one of the most useful properties of modern language models. Used as if it were ordinary learning, it will disappoint exactly where reliability matters most.
Connect with me on LinkedIn: linkedin.com/in/umeshtharukaofficial
Top comments (0)