Lune

ACL Findings2025

Selecting Demonstrations for Many-Shot In-Context Learning via Gradient Matching

Jianfei Zhang, Bei Li, Jun Bai, Rumei Li, Yanmeng Wang, Chenghua Lin, Wenge Rong

Abstract

In-Context Learning (ICL) empowers Large Language Models (LLMs) for rapid task adaptation without Fine-Tuning (FT), but its reliance on demonstration selection remains a critical challenge. While many-shot ICL shows promising performance through scaled demonstrations, the selection method for many-shot demonstrations remains limited to random selection in existing work. Since the conventional instance-level retrieval is not suitable for manyshot scenarios, we hypothesize that the data requirements for in-context learning and finetuning are analogous. To this end, we introduce a novel gradient matching approach that selects demonstrations by aligning fine-tuning gradients between the entire training set of the target task and the selected examples, so as to approach the learning effect on the entire training set within the selected examples. Through gradient matching on relatively small models, e.g., Qwen2.5-3B or Llama3-8B, our method consistently outperforms random selection on larger LLMs from 4-shot to 128-shot scenarios across 9 diverse datasets. For instance, it surpasses random selection by 4% on Qwen2.5-72B and Llama3-70B, and by around 2% on 5 closedsource LLMs. This work unlocks more reliable and effective many-shot ICL, paving the way for its broader application.

Ask Lune

Ask anything. The answer quotes the paper.

Lune reads all of it and the papers around it, then shows you the passages behind every answer.

Questions to start from

Lune answers from

the full paper · 2 top-tier papers that cite it · related work

Get the answer

Free to start. No credit card required.

Cited by top-tier papers2

  1. Learning to Rank for In-Context Example RetrievalYuwen Ji, Luodan Zhang, Ambyer Han, Haoran Que et al.NeurIPS 2025 · 1 citation
  2. Understanding and Leveraging the Expert Specialization of Context Faithfulness in Mixture-of-Experts LLMsJun Bai, Minghao Tong, Yang Liu, Zixia Jia et al.EMNLP 2025

Builds on14

  1. Fantastically Ordered Prompts and Where to Find Them: Overcoming Few-Shot Prompt Order SensitivityYao Lu, Max Bartolo, Alastair Moore, Sebastian Riedel et al.ACL 2022 · 1,494 citations
  2. Efficient Memory Management for Large Language Model Serving with PagedAttentionWoosuk Kwon, Zhuohan Li, Siyuan Zhuang, Ying Sheng et al.SOSP 2023 · 1,016 citations
  3. Dataset Condensation with Gradient MatchingBo Zhao, Konda Reddy Mopuri, Hakan BilenICLR 2021 · 684 citations
  4. True Few-Shot Learning with Language ModelsEthan Perez, Douwe Kiela, Kyunghyun ChoNeurIPS 2021 · 547 citations
  5. Many-Shot In-Context LearningRishabh Agarwal, Avi Singh, Lei Zhang, Bernd Bohnet et al.NeurIPS 2024 · 271 citations

Related papers

  1. Focused Large Language Models are Stable Many-Shot LearnersPeiwen Yuan, Shaoxiong Feng, Yiwei Li, Xinglin Wang et al.EMNLP 2024
  2. Context Tuning for In-Context OptimizationJack Lu, Ryan Teehan, Zhenbang Yang, Mengye RenICML 2026
  3. Unraveling the Mechanics of Learning-Based Demonstration Selection for In-Context LearningHui Liu, Wenya Wang, Hao Sun, Chris Xing Tian et al.ACL 2025 · 13 citations
  4. Learning to Select In-Context Demonstration Preferred by Large Language ModelZheng Zhang, Shaocheng Lan, Lei Song, Jiang Bian et al.ACL Findings 2025 · 5 citations
  5. Revisiting In-Context Learning with Long Context Language ModelsJinheon Baek, Sun Jae Lee, Prakhar Gupta, Geunseob Oh et al.ACL Findings 2025 · 2 citations
Dusk over the sea between two cliffs drawn in fine vertical lines