Large Language Model-Aware In-Context Learning for Code Generation

Jia Li, Ge Li, Chongyang Tao, Huangzhao Zhang, Fang Liu, Zhi Jin

TL;DR

LAIL (LLM-Aware In-context Learning) improves code generation by letting Large Language Models (LLMs) choose their own "teachers." By training a neural retriever to select examples that maximize the LLM's internal probability of generating correct code, the authors achieve significant performance gains across multiple programming languages and models without any parameter updates to the generator itself.

What Problem Does This Paper Address?

In-context learning (ICL) has become the dominant paradigm for code generation: you provide an LLM with a few examples of "requirement $\rightarrow$ code" and then ask it to solve a new problem. However, ICL is notoriously unstable. The model's performance can swing from near-perfect to completely random depending on which examples you put in the prompt.

Most current methods for selecting these examples rely on simple heuristics, like random selection or textual similarity (e.g., BM25). The authors argue that these "LLM-blind" approaches fail because they don't account for the model's internal prior knowledge or specific preferences. A requirement that looks textually similar to the target might use a completely different logical structure, providing little help—or even confusing—the model.

Methods

The authors propose LAIL, a learning-based framework that makes the example selection process "LLM-aware." The process follows three main stages:

  1. Estimation and Labeling: For every requirement in a training set, the authors use the LLM to score potential in-context examples. They calculate the probability that the LLM will generate the correct ground-truth code when prompted with a specific candidate example. Examples that lead to a high probability are labeled as positive, while those leading to low probabilities are negative.
  2. Training a Neural Retriever: Because it's too expensive to query the LLM for every new test case, they train a dedicated, lightweight retriever (based on GraphCodeBERT). They use a contrastive learning objective (specifically a SimCLR-style loss) to train this retriever to map requirements and their "preferred" examples close together in a shared embedding space.
  3. Inference: When a new test requirement arrives, the trained retriever quickly finds the most "helpful" examples from the training pool to construct the final prompt for the LLM.
Figure 2: The overview of our LAIL. LAIL use LLMs them- selves to estimate candidate examples and label them as pos- itive and negative (A). Based on the label date, LAIL then trains a retriever to align with the preference of LLMs with a contrastive loss (B). Given a test requirement, the optimized retriever selects several examples as a prompt that is inputted to LLMs for code generation (C).

Datasets

The researchers evaluated LAIL across three programming languages using representative benchmarks:

  • MBPP (Mostly Basic Python Problems): 974 crowd-sourced Python programming tasks.
  • MBJP: A Java version of the MBPP dataset.
  • MBCPP: A C++ version of the MBPP dataset.

These datasets include natural language descriptions, functional code solutions, and unit tests to verify correctness.

Key Insights and Ideas

  • The Model Knows Best: The core "aha" moment is that the LLM's internal probability distribution is a better signal for example quality than external metrics like BLEU or textual overlap. It captures the functional and structural needs of the model.
  • Transferability is Real: Surprisingly, a retriever trained on the preferences of one model (like CodeGen) significantly improves the performance of others (like GPT-3.5 or ChatGPT). This suggests that different LLMs share a common "intuition" about what constitutes a helpful programming example.
  • Language Families Matter: The authors found that a retriever trained on Java transfers well to C++ (and vice versa) due to their similar object-oriented syntax, but transfers poorly to Python.
  • Functional over Lexical: While BM25 might pick examples based on shared words like "write a function," LAIL tends to pick examples that share underlying operations, such as "array traversal," even if the natural language descriptions look different.

Key Results

LAIL consistently outperformed state-of-the-art (SOTA) baselines like AceCoder across different models and languages:

  • On CodeGen-Multi-16B: LAIL improved the Pass@1 score (the probability that the first generated code snippet is correct) by 11.58% on MBJP, 6.89% on MBPP, and 5.07% on MBCPP.
  • On GPT-3.5: It achieved relative improvements of 4.38%, 2.85%, and 2.74% across the three datasets.
  • Human Evaluation: In a study with 10 developers, code generated via LAIL was rated significantly higher in terms of correctness, code quality (less "code smell"), and maintainability compared to baselines.
Performance comparison on CodeGen across three datasets.

Limitations and Future Work

  • Labeling Overhead: The initial stage of labeling the training set requires many LLM queries. While the authors used a two-stage process (filtering with BM25 first) to mitigate this, it remains a computationally heavy pre-processing step.
  • Candidate Pool Constraints: The retriever is currently limited to selecting examples from a fixed training set.
  • Hard Negatives: The authors noted that selecting too many "hard negatives" (examples that look similar but are unhelpful) during contrastive training can sometimes hurt performance, suggesting the need for more refined negative sampling strategies.
  • Future Directions: The team plans to explore how LAIL can be applied to even larger, more diverse datasets and other types of LLM-based tasks beyond code generation.

Summary updated

Abstract

Large language models (LLMs) have shown impressive in-context learning (ICL) ability in code generation. LLMs take a prompt consisting of requirement-code examples and a new requirement as input, and output new programs. Existing studies have found that ICL is highly dominated by the examples and thus arises research on example selection. However, existing approaches randomly select examples or only consider the textual similarity of requirements to retrieve, leading to sub-optimal performance. In this paper, we propose a novel learning-based selection approach named LAIL (LLM-Aware In-context Learning) for code generation. Given a candidate example, we exploit LLMs themselves to estimate it by considering the generation probabilities of ground-truth programs given a requirement and the example. We then label candidate examples as positive or negative through the probability feedback. Based on the labeled data, we import a contrastive learning objective to train an effective retriever that acquires the preference of LLMs in code generation. We apply LAIL to three LLMs and evaluate it on three representative datasets (e.g., MBJP, MBPP, and MBCPP). LATA outperforms the state-of-the-art baselines by 11.58%, 6.89%, and 5.07% on CodeGen, and 4.38%, 2.85%, and 2.74% on GPT-3.5 in terms of Pass@1, respectively.
BibTeX
@article{li2025large,
  author = {Jia Li and Ge Li and Chongyang Tao and Jia Li and Huangzhao Zhang and Fang Liu and Zhi Jin},
  title = {{Large Language Model-Aware In-Context Learning for Code Generation}},
  journal = {ACM Trans. Softw. Eng. Methodol.},
  year = {2025},
  doi = {10.1145/3715908},
}
Figure