What is in-context learning (ICL)?
In-context learning (ICL) is a language model's ability to adapt its behaviour for a request using only the instructions, examples or documents added to the prompt, without any change to its weights.
It differs from fine-tuning in permanence: fine-tuning changes the weights, while in-context learning leaves them fixed and the adaptation lasts only for that request. Retrieval-Augmented Generation relies on it: RAG is the architecture that retrieves documents into the context; the model uses them through in-context learning.
- Topic
- Prompts, context, reasoning and generation
- Also known as
- ICL, in-context adaptation
- Origin
- 2020, Brown et al.
How in-context learning (ICL) works
- A task description, a few input and output examples or reference documents are added to the prompt at inference time.
- The model follows patterns in the context to answer the new input; with instructions only it is called zero-shot, with a few examples few-shot learning.
- No gradients or weight updates are computed; the adaptation disappears when the request ends and must be supplied again each time.
- The choice, order and format of examples affect results noticeably.
In more detail
Mechanistic basis: induction heads and implicit optimisation
Mechanistic interpretability research shows that transformers implement in-context learning through specialised attention circuits known as induction heads, which detect and copy repeating token patterns across the context. Other theoretical studies demonstrate that attention layers can implicitly emulate gradient-descent steps or Bayesian inference over the prompt's examples without modifying stored weights.
Many-shot in-context learning and long contexts
With 128K to 1M+ token context windows, many-shot in-context learning supplies hundreds or thousands of demonstrations directly in the prompt. Combined with prefix/KV caching, many-shot ICL can rival fine-tuning on niche enterprise classification or extraction tasks, though it increases prefill compute and per-query memory pressure if cache hits miss.
Position biases and dynamic example selection
ICL accuracy is highly fragile to demonstration ordering (recency bias), label imbalance (majority-label bias), formatting variations and positional degradation such as the 'lost in the middle' effect. Production pipelines therefore use dynamic semantic example selection (e.g., kNN retrieval of the closest validated examples) and majority voting or calibration to stabilise outputs.
Attack surface: many-shot jailbreaking and prompt injection
Because ICL treats the prompt as a single flat token stream, injecting untrusted user data or external documents alongside few-shot demonstrations opens the door to prompt injection and many-shot jailbreaking. Strict structural delimiters, system/user privilege boundaries and input validation are required to prevent adversarial examples from overriding task rules.
Commonly confused terms
| Term | How it differs |
|---|---|
| Prompt engineering | Prompt engineering is the human craft of structuring instructions and examples, whereas in-context learning is the model's emergent capability to infer patterns and adapt from those inputs at inference time. |
Why in-context learning (ICL) matters for on-premises AI
Organisational documents are used without being written into model weights; updating a document needs no retraining and the model file carries no document content. This also makes permissions easy to apply per request. In return, examples and documents take up context window space on every request; long context raises GPU memory use and latency. For fixed examples that repeat often, prompt caching can cut this cost.
A telecoms operator adds frequently asked questions and current campaign rules to its customer assistant's prompt; when rules change, it updates only the prompt without retraining.
Common questions
Does in-context learning change the model permanently?
No. Weights never change; once the request ends the model keeps nothing from the context and it must be supplied again next time.
Is RAG the same as in-context learning?
No. RAG is an architecture that retrieves relevant content for the model. In-context learning is the model's ability to use such input in the prompt; RAG depends on it.
Related terms
- Few-shot learning
- Zero-shot learning
- Retrieval-Augmented Generation
- Context
- Prompt
- Fine-tuning
- Context window
- Meta-learning
Sources
- Brown et al., Language Models are Few-Shot Learners, 2020
- Dong et al., A Survey on In-context Learning, 2023
In the glossary: Glossary, D to L