Repository logo

The Impact of Few-Shot Examples for LLM-based Code Vulnerability Detection

Abstract

Large language models (LLMs) have shown promise for detecting software vulnerabilities through in-context learning (ICL), where a small number of labeled examples are provided in the prompt to guide model predictions. However, the effectiveness of ICL depends critically on how these few-shot examples are selected, and little is studied about what these models actually attend to when making their decisions. This work addresses both gaps directly. First, how should those few-shot examples be selected to maximize detection performance? Second, when the model makes correct predictions, is it actually focusing on the right tokens of the vulnerable code? The first part studies two selection criteria: Learn-from-Mistakes (LFM), which prioritizes examples on which the model consistently makes errors, and Learn-from-Nearest-Neighbors (LFNN), which retrieves semantically similar examples using a code embedding model. We evaluate these methods and three combined strategies on five datasets spanning C/C++, Python, and JavaScript, using Qwen2.5-Coder-7B-Instruct, Gemma-3-4b-it, and GPT-5-mini. As a single method, LFNN consistently improves performance on Python and JavaScript, while C/C++ datasets remain resistant to few-shot adaptation. Combined methods showed the best performance overall. The second part takes the LFNN pipeline and Qwen model, and audits its internal attribution behavior using Layer Integrated Gradients (LIG). Our experiments reveal that among the top-50 tokens identified by the attribution method, 40 to 52 percent belong to the injected few-shot examples. More strikingly, fewer than 2 percent of those top-50 tokens correspond to actual vulnerable tokens, even on samples the model predicted correctly. These findings show that even when LFNN improves detection accuracy, gradient-based attributions are not always grounded in the actual vulnerable code tokens, revealing a gap between performance and reliability in LLM-based code vulnerability detection.

Description

Rights Access

Subject

few-shot

LLMs

attribution

Vulnerability

LIG

Citation

Collections

Endorsement

Review

Supplemented By

Referenced By