How do Large Language Models Learn In-Context? Query and Key Matrices of In-Context Heads are Two Towers for Metric Learning
Not on the map. Why?
Abstract (arXiv)
We investigate the mechanism of in-context learning (ICL) on sentence classification tasks with semantically-unrelated labels ("foo"/"bar"). We find intervening in only 1% heads (named "in-context heads") significantly affects ICL accuracy from 87.6% to 24.4%. To understand this phenomenon, we analyze the value-output vectors in these heads and discover that the vectors at each label position contain substantial information about the corresponding labels. Furthermore, we observe that the prediction shift from "foo" to "bar" is due to the respective reduction and increase in these heads' attention scores at "foo" and "bar" positions. Therefore, we propose a hypothesis for ICL: in in-context heads, the value-output matrices extract label features, while the query-key matrices compute the similarity between the features at the last position and those at each label position. The query and key matrices can be considered as two towers that learn the similarity metric between the last position's features and each demonstration at label positions. Using this hypothesis, we explain the majority label bias and recency bias in ICL and propose two methods to reduce these biases by 22% and 17%, respectively.
Key facts. Cited by 18 papers in the map (data through 14 Oct 2026). Builds on 28 of its 50 references that are in the map. First posted on arXiv 5 Feb 2024.
Builds on
28 of 50 references.
References · 28
- 2020Language Models are Few-Shot Learnerscited by 32,419
- 2022Training language models to follow instructions with human feedbackcited by 15,971
- 2023LLaMA: Open and Efficient Foundation Language Modelscited by 14,132
- 2017Axiomatic Attribution for Deep Networkscited by 3,384
- 2022Locating and Editing Factual Associations in GPTcited by 2,691
- 2015Character-level Convolutional Networks for Text Classificationcited by 2,330
- 2022Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?cited by 1,434
- 2022Least-to-Most Prompting Enables Complex Reasoning in Large Language Modelscited by 1,407
- 2021Calibrate Before Use: Improving Few-Shot Performance of Language Modelscited by 1,392
- 2020Transformer Feed-Forward Layers Are Key-Value Memoriescited by 1,336
- 2021What Makes Good In-Context Examples for GPT-3?cited by 1,195
- 2022Interpretability in the Wild: a Circuit for Indirect Object Identification in GPT-2 smallcited by 1,165
16 more
- 2021Fantastically Ordered Prompts and Where to Find Them: Overcoming Few-Shot Prompt Order Sensitivitycited by 1,135
- 2021An Explanation of In-context Learning as Implicit Bayesian Inferencecited by 938
- 2023Progress measures for grokking via mechanistic interpretabilitycited by 839
- 2022What Can Transformers Learn In-Context? A Case Study of Simple Function Classescited by 715
- 2022What learning algorithm is in-context learning? Investigations with linear modelscited by 684
- 2022Transformer Feed-Forward Layers Build Predictions by Promoting Concepts in the Vocabulary Spacecited by 532
- 2023Dissecting Recall of Factual Associations in Auto-Regressive Language Modelscited by 528
- 2023Larger language models do in-context learning differentlycited by 394
- 2017The (Un)reliability of saliency methodscited by 333
- 2013Good Debt or Bad Debt: Detecting Semantic Orientations in Economic Textscited by 310
- 2013Direct and Indirect Effectscited by 280
- 2023Label Words are Anchors: An Information Flow Perspective for Understanding In-Context Learningcited by 236
- 2022Demystifying Prompts in Language Models via Perplexity Estimationcited by 216
- 2023What In-Context Learning "Learns" In-Context: Disentangling Task Recognition and Task Learningcited by 152
- 2023The Closeness of In-Context Learning and Weight Shifting for Softmax Regressioncited by 41
- 2023ICL-D3IE: In-Context Learning with Diverse Demonstrations Updating for Document Information Extractioncited by 38
Built on by
Citing papers per year: 2024: 7; 2025: 7; 2026: 4.
18 papers.
Sort:
By area:
- 2024Attention Heads of Large Language Models: A Surveycited by 67
- 2024Interpreting Arithmetic Mechanism in Large Language Models through Comparative Neuron Analysiscited by 31
- 2026Locate, Steer, and Improve: A Practical Survey of Actionable Mechanistic Interpretability in Large Language Modelscited by 27
- 2024Can In-context Learning Really Generalize to Out-of-distribution Tasks?cited by 23
- 2024Understanding Multimodal LLMs: the Mechanistic Interpretability of Llava in Visual Question Answeringcited by 11
- 2025How Do Large Vision-Language Models See Text in Image? Unveiling the Distinctive Role of OCR Headscited by 9
- 2024Unveiling In-Context Learning: A Coordinate System to Understand Its Working Mechanismcited by 6
- 2025From Compression to Expression: A Layerwise Analysis of In-Context Learningcited by 6
- 2025CAFE: Retrieval Head-based Coarse-to-Fine Information Seeking to Enhance Multi-Document QA Capabilitycited by 4
- 2024From Unstructured Data to In-Context Learning: Exploring What Tasks Can Be Learned and Whencited by 3
- 2025ConText: Driving In-context Learning for Text Removal and Segmentationcited by 3
- 2025BASIC: Boosting Visual Alignment with Intrinsic Refined Embeddings in Multimodal Large Language Modelscited by 2
6 more
- 2025Localizing Task Recognition and Task Learning in In-Context Learning via Attention Head Analysiscited by 2
- 2026Understanding the Dynamics of Demonstration Conflict in In-Context Learningcited by 2
- 2025Quantifying LLM Biases Across Instruction Boundary in Mixed Question Formscited by 1
- 2024Logit Separability-Driven Samples and Multiple Class-Related Words Selection for Advancing In-Context Learningcited by 0
- 2026In-Context Fixation: When Demonstrated Labels Override Semantics in Few-Shot Classificationcited by 0
- 2026AttnLink: Turning Attention into Schema Links for Text-to-SQLcited by 0
- 2026AttnLink: Turning Attention into Schema Links for Text-to-SQL1 Aug 2026
- 2026In-Context Fixation: When Demonstrated Labels Override Semantics in Few-Shot Classification8 May 2026
- 2026Understanding the Dynamics of Demonstration Conflict in In-Context Learning3 Mar 2026
- 2026Locate, Steer, and Improve: A Practical Survey of Actionable Mechanistic Interpretability in Large Language Models20 Jan 2026
- 2025Localizing Task Recognition and Task Learning in In-Context Learning via Attention Head Analysis29 Sep 2025
- 2025Quantifying LLM Biases Across Instruction Boundary in Mixed Question Forms24 Sep 2025
- 2025BASIC: Boosting Visual Alignment with Intrinsic Refined Embeddings in Multimodal Large Language Models9 Aug 2025
- 2025ConText: Driving In-context Learning for Text Removal and Segmentation4 Jun 2025
- 2025From Compression to Expression: A Layerwise Analysis of In-Context Learning22 May 2025
- 2025How Do Large Vision-Language Models See Text in Image? Unveiling the Distinctive Role of OCR Heads21 May 2025
- 2025CAFE: Retrieval Head-based Coarse-to-Fine Information Seeking to Enhance Multi-Document QA Capability15 May 2025
- 2024Understanding Multimodal LLMs: the Mechanistic Interpretability of Llava in Visual Question Answering17 Nov 2024
6 more
- 2024Can In-context Learning Really Generalize to Out-of-distribution Tasks?13 Oct 2024
- 2024Interpreting Arithmetic Mechanism in Large Language Models through Comparative Neuron Analysis21 Sep 2024
- 2024Attention Heads of Large Language Models: A Survey5 Sep 2024
- 2024Unveiling In-Context Learning: A Coordinate System to Understand Its Working Mechanism24 Jul 2024
- 2024Logit Separability-Driven Samples and Multiple Class-Related Words Selection for Advancing In-Context Learning16 Jun 2024
- 2024From Unstructured Data to In-Context Learning: Exploring What Tasks Can Be Learned and When31 May 2024
- 2026Locate, Steer, and Improve: A Practical Survey of Actionable Mechanistic Interpretability in Large Language Modelscited by 27
- 2026Understanding the Dynamics of Demonstration Conflict in In-Context Learningcited by 2
- 2026In-Context Fixation: When Demonstrated Labels Override Semantics in Few-Shot Classificationcited by 0
- 2026In-Context Fixation: When Demonstrated Labels Override Semantics in Few-Shot Classification8 May 2026
- 2026Understanding the Dynamics of Demonstration Conflict in In-Context Learning3 Mar 2026
- 2026Locate, Steer, and Improve: A Practical Survey of Actionable Mechanistic Interpretability in Large Language Models20 Jan 2026
- 2024Understanding Multimodal LLMs: the Mechanistic Interpretability of Llava in Visual Question Answeringcited by 11
- 2025How Do Large Vision-Language Models See Text in Image? Unveiling the Distinctive Role of OCR Headscited by 9
- 2025BASIC: Boosting Visual Alignment with Intrinsic Refined Embeddings in Multimodal Large Language Modelscited by 2
- 2025BASIC: Boosting Visual Alignment with Intrinsic Refined Embeddings in Multimodal Large Language Models9 Aug 2025
- 2025How Do Large Vision-Language Models See Text in Image? Unveiling the Distinctive Role of OCR Heads21 May 2025
- 2024Understanding Multimodal LLMs: the Mechanistic Interpretability of Llava in Visual Question Answering17 Nov 2024
- 2024Attention Heads of Large Language Models: A Surveycited by 67
- 2024Interpreting Arithmetic Mechanism in Large Language Models through Comparative Neuron Analysiscited by 31
- 2024Can In-context Learning Really Generalize to Out-of-distribution Tasks?cited by 23
- 2024Unveiling In-Context Learning: A Coordinate System to Understand Its Working Mechanismcited by 6
- 2025CAFE: Retrieval Head-based Coarse-to-Fine Information Seeking to Enhance Multi-Document QA Capabilitycited by 4
- 2024From Unstructured Data to In-Context Learning: Exploring What Tasks Can Be Learned and Whencited by 3
- 2025ConText: Driving In-context Learning for Text Removal and Segmentationcited by 3
- 2025Localizing Task Recognition and Task Learning in In-Context Learning via Attention Head Analysiscited by 2
- 2025Quantifying LLM Biases Across Instruction Boundary in Mixed Question Formscited by 1
- 2024Logit Separability-Driven Samples and Multiple Class-Related Words Selection for Advancing In-Context Learningcited by 0
- 2026AttnLink: Turning Attention into Schema Links for Text-to-SQLcited by 0
- 2026AttnLink: Turning Attention into Schema Links for Text-to-SQL1 Aug 2026
- 2025Localizing Task Recognition and Task Learning in In-Context Learning via Attention Head Analysis29 Sep 2025
- 2025Quantifying LLM Biases Across Instruction Boundary in Mixed Question Forms24 Sep 2025
- 2025ConText: Driving In-context Learning for Text Removal and Segmentation4 Jun 2025
- 2025CAFE: Retrieval Head-based Coarse-to-Fine Information Seeking to Enhance Multi-Document QA Capability15 May 2025
- 2024Can In-context Learning Really Generalize to Out-of-distribution Tasks?13 Oct 2024
- 2024Interpreting Arithmetic Mechanism in Large Language Models through Comparative Neuron Analysis21 Sep 2024
- 2024Attention Heads of Large Language Models: A Survey5 Sep 2024
- 2024Unveiling In-Context Learning: A Coordinate System to Understand Its Working Mechanism24 Jul 2024
- 2024Logit Separability-Driven Samples and Multiple Class-Related Words Selection for Advancing In-Context Learning16 Jun 2024
- 2024From Unstructured Data to In-Context Learning: Exploring What Tasks Can Be Learned and When31 May 2024