---
title: "In-Context Learning"
source: "https://systems-analysis.info/eng/In-Context_Learning"
wiki: "systems-analysis.info/eng"
article: "In-Context_Learning"
language: "en"
categories:
  - "Category:English"
  - "Category:Large language models"
  - "Category:Machine learning"
  - "Category:Technology"
revision_id: 191
wiki_created_at: 2026-09-06T22:18:41Z
wiki_modified_at: 2026-09-06T22:18:41Z
downloaded_at: 2026-09-07T22:21:44Z
---

# In-Context Learning

**In-Context Learning** (**ICL**) is a fundamental capability of [large language models](https://systems-analysis.info/eng/Large_language_model "Large language model") (LLMs) to learn new tasks "on the fly" using only examples (demonstrations) provided within the context (prompt) of a query. A key feature is that this adaptation process occurs without updating the model's weights (parameters), meaning without traditional [fine-tuning](https://systems-analysis.info/eng/Fine-tuning_(deep_learning) "Fine-tuning (deep learning)")<sup>[\[1\]](https://systems-analysis.info/eng/In-Context_Learning#cite_note-lakera-1)[\[2\]](https://systems-analysis.info/eng/In-Context_Learning#cite_note-hopsworks-2)</sup>.

This mechanism allows models to exhibit remarkable flexibility, solving tasks for which they were not specifically trained. ICL has become one of the key breakthroughs that has made large language models so powerful and versatile<sup>[\[3\]](https://systems-analysis.info/eng/In-Context_Learning#cite_note-gradient_pub-3)</sup>.

## How It Works

A precise understanding of how ICL works remains an active area of research; however, there are several leading theories that explain this phenomenon.

### Transformer as a Meta-Optimizer

One popular theory suggests that the [Transformer architecture](https://systems-analysis.info/eng/Transformer_architecture "Transformer architecture") learns to implement learning algorithms within its forward passes during [pre-training](https://systems-analysis.info/eng/Pre-training_of_large_language_models "Pre-training of large language models"). When the model receives a prompt with examples, it implicitly performs a form of optimization to solve the presented task by adjusting its internal states (activations) rather than its weights<sup>[\[4\]](https://systems-analysis.info/eng/In-Context_Learning#cite_note-arxiv_grad-4)</sup>.

### Bayesian Inference

Another theory views ICL as a form of Bayesian inference. A model pre-trained on vast amounts of data has a prior understanding of numerous concepts. The examples in the context serve as evidence that allows the model to update its posterior probability distribution over the latent concept. In other words, the examples help the model "understand" which specific task, out of the thousands it knows, needs to be solved at that moment<sup>[\[5\]](https://systems-analysis.info/eng/In-Context_Learning#cite_note-stanford-5)</sup>.

## Types of In-Context Learning

Depending on the number of examples provided, ICL is divided into three main types.

- **[Few-shot Learning](https://systems-analysis.info/eng/Few-Shot_and_Zero-Shot_Learning "Few-Shot and Zero-Shot Learning")**: This is the most common and balanced approach. The model is provided with a few (typically 2 to 10) demonstration examples.

*Example (sentiment classification):*

    Text: "What a beautiful day!"
    Sentiment: Positive

    Text: "I hate being stuck in traffic."
    Sentiment: Negative

    Text: "This movie was rather average."
    Sentiment:

**Expected output:**

    Neutral

- **One-shot Learning**: The model is given only one example. This is often sufficient to set the output format and significantly improve performance compared to the zero-shot approach.

<!-- -->

- **Zero-shot Learning**: The model is not provided with any examples, only an instruction or a description of the task. In this case, the model relies entirely on the knowledge acquired during its pre-training.

## Practical Applications

The proper application of ICL allows for solving a wide range of tasks without costly development and fine-tuning.

- **For creative and stylistic tasks** (e.g., generating code in a specific style, writing text in the manner of a particular author):
  - **[Few-shot Learning](https://systems-analysis.info/eng/Few-Shot_and_Zero-Shot_Learning "Few-Shot and Zero-Shot Learning")** is recommended.
  - Examples help the model grasp the required style, format, and output structure.

<!-- -->

- **For simple tasks with clear instructions** (e.g., translation, summarization, answering simple questions):
  - **[Zero-shot Learning](https://systems-analysis.info/eng/Few-Shot_and_Zero-Shot_Learning "Few-Shot and Zero-Shot Learning")** is often sufficient.
  - Modern models handle such tasks quite well if they were part of their pre-training.

<!-- -->

- **For tasks where the output format is crucial** (e.g., generating JSON, entity extraction):
  - **One-shot** or **Few-shot Learning** is recommended.
  - Even a single example can clearly define the required response structure, preventing formatting errors.

## Advantages and Disadvantages

<table class="wikitable">
<caption>Comparison of Advantages and Disadvantages of ICL</caption>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="header">
<th>Advantages</th>
<th>Disadvantages</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><ul>
<li><strong>Flexibility and Speed:</strong> Instant adaptation to new tasks without the need for retraining.</li>
<li><strong>Resource Efficiency:</strong> Does not require data collection, labeling, or the computational resources needed for fine-tuning.</li>
<li><strong>Accessibility:</strong> Allows users without ML expertise to configure models using simple text examples.</li>
</ul></td>
<td><ul>
<li><strong><a href="https://systems-analysis.info/eng/Context_window" title="Context window">Context Window</a> Limitations:</strong> The number of examples is limited by the model's maximum context length.</li>
<li><strong>Sensitivity to Examples:</strong> The result is highly dependent on the quality, order, and format of the provided demonstrations.</li>
<li><strong>High Inference Costs:</strong> Long prompts with many examples increase the cost and time of generation.</li>
<li><strong>Security Risks:</strong> Providing confidential information as examples can be insecure.</li>
</ul></td>
</tr>
</tbody>
</table>

Comparison of Advantages and Disadvantages of ICL

## Comparison with Other Paradigms

### ICL vs. Fine-tuning

Fine-tuning modifies the model's weights, "imprinting" new knowledge into it. This makes the model an expert in a narrow domain but reduces its overall flexibility. ICL, in contrast, does not change the weights and is more flexible, but it may underperform on highly specialized tasks that require deep domain knowledge.

### ICL vs. RAG (Retrieval-Augmented Generation)

Both methods extend the model's context, but for different purposes:

- **ICL** uses examples to **teach** the model *how* to perform a task (demonstrating a skill).
- **[RAG](https://systems-analysis.info/eng/Retrieval-augmented_generation_(RAG) "Retrieval-augmented generation (RAG)")** uses retrieved information to **provide** the model with facts needed for the response (providing knowledge).

In practice, ICL and RAG are often combined to achieve the best results.

## External links

- <a href="https://en.wikipedia.org/wiki/In-context_learning_(natural_language_processing)" class="external text" rel="nofollow">In-context learning (natural language processing) — Wikipedia</a>

## Literature

- Brown, T. B. et al. (2020). *Language Models are Few-Shot Learners*. <a href="https://arxiv.org/abs/2005.14165" class="external text" rel="nofollow">arXiv:2005.14165</a>.
- Dai, D. et al. (2022). *Why Can GPT Learn In-Context? Language Models Implicitly Perform Gradient Descent as Meta-Optimizers*. <a href="https://arxiv.org/abs/2212.10559" class="external text" rel="nofollow">arXiv:2212.10559</a>.
- Panwar, M.; Ahuja, K.; Goyal, N. (2024). *In-Context Learning through the Bayesian Prism*. <a href="https://arxiv.org/abs/2306.04891" class="external text" rel="nofollow">arXiv:2306.04891</a>.
- Müller, S. et al. (2021). *Transformers Can Do Bayesian Inference*. <a href="https://arxiv.org/abs/2112.10510" class="external text" rel="nofollow">arXiv:2112.10510</a>.
- Garg, S. et al. (2022). *What Can Transformers Learn In-Context? A Case Study of Simple Function Classes*. <a href="https://arxiv.org/abs/2208.01066" class="external text" rel="nofollow">arXiv:2208.01066</a>.
- Min, S. et al. (2022). *Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?*. <a href="https://arxiv.org/abs/2202.12837" class="external text" rel="nofollow">arXiv:2202.12837</a>.
- Wang, X. et al. (2023). *Explaining and Finding Good Demonstrations for In-Context Learning*. <a href="https://arxiv.org/abs/2302.13971" class="external text" rel="nofollow">arXiv:2302.13971</a>.
- Xie, S. et al. (2024). *A Survey on In-Context Learning*. <a href="https://arxiv.org/abs/2301.00234" class="external text" rel="nofollow">arXiv:2301.00234</a>.
- Yu, Z.; Ananiadou, S. (2024). *How Do Large Language Models Learn In-Context? Query and Key Matrices of In-Context Heads Are Two Towers for Metric Learning*. <a href="https://arxiv.org/abs/2402.02872" class="external text" rel="nofollow">arXiv:2402.02872</a>.
- Wibisono, K. C.; Wang, Y. (2024). *From Unstructured Data to In-Context Learning: Exploring What Tasks Can Be Learned and When*. <a href="https://arxiv.org/abs/2406.00131" class="external text" rel="nofollow">arXiv:2406.00131</a>.
- Chan, J. K. et al. (2022). *Data Distributional Properties Drive Emergent In-Context Learning in Transformers*. <a href="https://arxiv.org/abs/2205.05055" class="external text" rel="nofollow">arXiv:2205.05055</a>.
- Hahn, M.; Goyal, N. (2023). *A Theory of Emergent In-Context Learning as Implicit Structure Induction*. <a href="https://arxiv.org/abs/2303.07971" class="external text" rel="nofollow">arXiv:2303.07971</a>.

## References

1.  <span id="cite_note-lakera-1">[↑](https://systems-analysis.info/eng/In-Context_Learning#cite_ref-lakera_1-0) <a href="https://www.lakera.ai/blog/what-is-in-context-learning" class="external text" rel="nofollow">What is In-Context Learning (ICL)?</a> // Lakera.ai</span>
2.  <span id="cite_note-hopsworks-2">[↑](https://systems-analysis.info/eng/In-Context_Learning#cite_ref-hopsworks_2-0) <a href="https://www.hopsworks.ai/dictionary/in-context-learning-icl" class="external text" rel="nofollow">In-Context Learning (ICL)</a> // Hopsworks.ai</span>
3.  <span id="cite_note-gradient_pub-3">[↑](https://systems-analysis.info/eng/In-Context_Learning#cite_ref-gradient_pub_3-0) <a href="https://thegradient.pub/in-context-learning-in-context/" class="external text" rel="nofollow">In-Context Learning, In Context</a> // The Gradient</span>
4.  <span id="cite_note-arxiv_grad-4">[↑](https://systems-analysis.info/eng/In-Context_Learning#cite_ref-arxiv_grad_4-0) <a href="https://arxiv.org/abs/2212.10559" class="external text" rel="nofollow">Why Can GPT Learn In-Context? Language Models Secretly Perform Gradient Descent as Meta-Optimizers</a> // arXiv, 2022.</span>
5.  <span id="cite_note-stanford-5">[↑](https://systems-analysis.info/eng/In-Context_Learning#cite_ref-stanford_5-0) <a href="https://ai.stanford.edu/blog/understanding-incontext/" class="external text" rel="nofollow">Understanding In-Context Learning</a> // Stanford Human-Centered AI, 2023.</span>
