Abstract
Large language models (LLMs) have made significant strides at code generation through improved model design, training, and chain-of-thought. However, prompt-level optimizations remain an important yet under-explored aspect of LLMs for coding. This work focuses on the few-shot examples present in most code generation prompts, offering a systematic study on whether few-shot examples improve LLM's coding capabilities, which few-shot examples have the largest impact, and how to select impactful examples. Our work offers 2 approaches for selecting few-shot examples, a model-free method, CODEEXEMPLAR-FREE, and a model-based method, CODEEXEMPLAR-BASED. The 2 methods offer a trade-off between improved performance and reliance on training data and interpretability. Both methods significantly improve CodeLlama's coding ability across the popular HumanEval+ coding benchmark. In summary, our work provides valuable insights into how to pick few-shot examples in code generation prompts to improve LLM code generation capabilities.
Introduction
When prompting an LLM for code generation, developers typically include a natural language description followed by a few input-output examples (e.g., add(1, 2) == 3). While significant research has focused on model architecture and training data, the selection of these "few-shot" examples has remained largely intuitive or static. Because LLMs are highly sensitive to prompt variations, a poorly chosen example can provide redundant information, while a well-chosen one can clarify complex instructions. This research investigates automated, systematic methods to identify which examples most effectively improve model performance.
Methodology: The CODEEXEMPLAR Framework
The researchers propose two distinct strategies to rank and select examples from a candidate pool, optimizing for Target Perplexity—a metric measuring how much an example increases the model's probability of generating the correct ground-truth code.
- CODEEXEMPLAR-FREE (Model-Free): This approach is based on the concept of "Source Perplexity." It assumes the model learns most from information it cannot already predict. Examples that "surprise" the model (high perplexity) are ranked higher, as they likely contain high-value information that disambiguates the task.
- CODEEXEMPLAR-BASE (Model-Based): This "gray-box" method extracts internal hidden representations (embeddings) from the 16th layer of CodeLlama. A simple 4-layer Multi-Layer Perceptron (MLP) is then trained to predict the performance gain associated with specific examples based on these embeddings.
Experimental Setup and Key Insights
The study utilized HUMANEVAL+, a more rigorous version of the standard HumanEval benchmark that includes a massive volume of unit tests to prevent "false positives" from buggy code. Several key insights emerged from the analysis:
- Complexity Over Simplicity: The most informative examples are not simple edge cases (like empty lists). Instead, complex and varied inputs provide the strongest constraints, helping the model converge on the correct logic.
- Embeddings as Quality Signals: The model’s internal embeddings naturally encode the "helpfulness" of an example. Visualizations of the 16th layer revealed clear clusters correlating with performance gains, suggesting the model "knows" which examples are useful.
- Diminishing Returns: While adding examples generally improves performance, the benefit begins to saturate after approximately six examples. This creates a trade-off between accuracy and inference speed, as larger prompts increase latency.
Results and Performance
Both selection methods significantly outperformed human-defined prompts and random selection across multiple models, including T5, Mistral, and Llama. Specifically, for CodeLlama-7B, the framework improved the Pass@1 score by 5.05% to 5.70% on average. Furthermore, the methods achieved a log-perplexity improvement of up to 5.0, indicating a substantial increase in the model's confidence when generating correct solutions.
Conclusion and Future Work
This work demonstrates that systematic few-shot selection is a powerful, low-cost lever for improving LLM code generation. While the current model-based ranker requires training for specific architectures, the model-free approach offers a generalizable path forward. Future research will explore how these selection methods interact with Chain-of-Thought (CoT) prompting and evaluate performance on more complex, real-world benchmarks like BigCodeBench.
Summary updated