Compressing Many-Shots in In-Context Learning

Devvrit Khatri, Pranamya Kulkarni, Nilesh Gupta, Yerram Varun, Liqian Peng, Jay Yagnik, Praneeth Netrapalli, Cho-Jui Hsieh, Alec Go, Inderjit S. Dhillon, Aditya Kusupati, Prateek JainarXiv 2025 · OpenReview

Abstract: Large Language Models (LLMs) have been shown to be able to learn different tasks without explicit finetuning when given many input-output examples / demonstrations through In-Context Learning (ICL). Increasing the number of examples, called “shots”, improves downstream task performance but incurs higher memory and computational costs. In this work, we study an approach to improve the memory and computational efficiency of ICL inference by compressing the many-shot prompts. Given many shots comprising $t$ tokens, our goal is to generate a $m$ soft-token summary, where $m < t$. We first show that existing prompt compression methods are ineffective for many-shot compression, and simply using fewer shots as a baseline is surprisingly strong. To achieve effective compression, we find that: (a) a stronger compressor model with more trainable parameters is necessary, and (b) compressing many-shot representations at each transformer layer enables more fine-grained compression by providing each layer with its own compressed representation. Based on these insights, we propose MemCom, a layer-wise compression method. We systematically evaluate various compressor models and training approaches across different model sizes (2B and 7B), architectures (Gemma and Mistral), many-shot sequence lengths (3k-6k tokens), and compression ratios (3x to 8x). MemCom outperforms strong baselines across all compression ratios on multiple classification tasks with large label sets. Notably, while baseline performance degrades sharply at higher compression ratios, often by over 20-30%, MemCom maintains high accuracy with minimal degradation, typically dropping by less than 10%.

Introduction

In-Context Learning (ICL) allows Large Language Models (LLMs) to learn new tasks simply by looking at examples (shots) provided in the prompt. Recent research shows that "many-shot" ICL—using hundreds or thousands of examples—can rival the performance of supervised finetuning. However, there is a massive "KV cache" tax. Every example token must be stored in memory and attended to during inference. As the number of shots grows, memory usage and computational latency skyrocket, making many-shot ICL impractical for real-time applications or edge devices. Existing compression methods often lose too much information or fail to scale to long contexts, often performing worse than a "naive baseline" of simply using fewer examples.

Methods

The authors introduce MemCom, a compression architecture consisting of two LLM stacks (a Source-LLM and a Memory-LLM) that work together to create a "soft-token" summary of the prompt.

Architecture of MemCom showing the layer-wise cross-attention between the Source-LLM and Memory-LLM.

The innovation lies in layer-wise cross-attention:

  1. Source-LLM: Processes the original $t$ input tokens.
  2. Memory-LLM: Manages $m$ learnable "memory tokens" ($m \ll t$). At every single transformer layer, these memory tokens perform cross-attention over the representations in the Source-LLM.
  3. Target-LLM: This is the frozen, deployed model. Instead of looking at the original thousands of tokens, it attends only to the $m$ compressed memory tokens at each corresponding layer.

Training happens in two phases: first, training only the cross-attention modules and memory tokens; second, unfreezing the entire compressor stack to fine-tune the distillation process. Crucially, the Target-LLM remains frozen, ensuring the model's general capabilities are never compromised.

Key Insights

  • The "Fewer Shots" Baseline is Strong: The authors discovered that many existing compression papers compare against weak baselines. Simply selecting fewer high-quality examples is often better than complex compression. MemCom is one of the few methods to consistently beat this "naive" baseline.
  • Capacity Matters: Effective many-shot compression requires a "strong" compressor. Lightweight adapters (like standard LoRA) often lack the parameters needed to capture the nuances of thousands of tokens.
  • Layer-wise is Non-Negotiable: Information in an LLM evolves as it moves through layers. Compressing only the final hidden state (a common approach) loses the intermediate logic. By providing each layer of the Target-LLM with its own tailored, compressed view of the prompt, MemCom preserves the model's reasoning chain.

Key Results

MemCom was evaluated on five classification tasks with large label sets: TREC-Coarse, TREC-Fine, HWU64, Banking77, and Clinc150. For training, pretraining corpora like FineWebEdu and SlimPajama were used.

Performance comparison of MemCom against baselines across different compression ratios for Mistral and Gemma models.
  • Robustness at Scale: On Mistral-7B and Gemma2-2B, MemCom maintained high accuracy at 3x, 6x, and 8x compression ratios.
  • Minimal Degradation: While baseline performance often plummeted by 20–30% at high compression ratios, MemCom’s accuracy typically dropped by less than 10%.
  • Efficiency: Because the compression is done "offline" (or once per task), the inference speedup is significant. The Target-LLM only needs to attend to a fraction of the original tokens (e.g., 768 memory tokens instead of 6,000 source tokens).

Limitations and Future Work

Building the compressor is computationally expensive, requiring training on 80–160 billion tokens. Additionally, MemCom struggles with tasks requiring "precise token access," such as low-resource language translation. While the current focus is on classification, future work will explore making compression more impactful for open-ended generation tasks like summarization and math reasoning.

Summary updated

BibTeX
@misc{khatri2025compressing,
  author = {Devvrit Khatri and Pranamya Kulkarni and Nilesh Gupta and Yerram Varun and Liqian Peng and Jay Yagnik and Praneeth Netrapalli and Cho-Jui Hsieh and Alec Go and Inderjit S. Dhillon and Aditya Kusupati and Prateek Jain},
  title = {{Compressing Many-Shots in In-Context Learning}},
  year = {2025},
  eprint = {2510.16092},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.16092},
}
Figure