NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

On Mitigating Code LLM Hallucinations with API Documentation

Nihal Jain, Robert Kwiatkowski, Baishakhi Ray, Murali Krishna Ramanathan, Varun Kumar

TL;DR

Code Large Language Models (LLMs) frequently hallucinate rare or newly updated APIs, but blindly applying Documentation Augmented Generation (DAG) to fix this can severely degrade performance on common APIs. This paper introduces CloudAPIBench to evaluate this phenomenon and proposes DAG++, a selective retrieval strategy that uses index lookups and model confidence scores to query documentation only when necessary, boosting overall API invocation accuracy.

What Problem Does This Paper Address?

While Code LLMs excel at writing standard boilerplate code, they struggle with APIs that are rare, newly released, or frequently updated. This leads to "API hallucinations"—generating non-existent functions or incorrect argument configurations.

A standard fix is Retrieval-Augmented Generation (RAG), or more specifically, Documentation Augmented Generation (DAG). However, the authors find a major catch: if you always retrieve documentation, sub-optimal retrievers will inject irrelevant context for high-frequency (common) APIs. This distracts the LLM, causing it to fail on tasks it already knew how to handle internally. The core challenge is developing a system that knows exactly when to look at the documentation.

Figure 1: The trade-off between base model performance and DAG on low vs high frequency APIs.

Methods

The authors propose DAG++, an intelligent, selective retrieval pipeline that decides whether to fetch documentation based on two signals:

  1. Index Lookup: The pipeline checks if the API name generated in an initial draft exists in a known index of valid APIs. If it doesn't, the model is likely hallucinating a non-existent API, which triggers a documentation search.
  2. API Invocation Confidence: It measures the LLM's uncertainty by taking the minimum probability among the tokens of the predicted API name. If this confidence falls below a certain threshold (optimized at 0.7–0.8), DAG is triggered.

Additionally, the authors experimented with different Augmentation Designs to see what information is most valuable. They discovered that providing just the "API Description + API Specification" (the function name along with its required and optional arguments) strikes the best balance between token efficiency and model performance, outperforming full docstrings.

Datasets

The paper introduces CloudAPIBench, a new Python benchmark consisting of 622 synthetic tasks evaluating cloud SDK APIs from Amazon Web Services (AWS) and Microsoft Azure.

Figure 2: Composition of CloudAPIBench across AWS and Azure services.

To study how popularity affects hallucinations, the authors annotated each API with its frequency in public code repositories, using The Stack v2 dataset as a proxy. APIs are divided into Low (0–10 occurrences), Medium (11–100), and High (≥101) frequency groups.

To evaluate correctness robustly, they avoid brittle string matching and instead use an API Stub evaluation metric, which tests whether the model's generated code successfully binds to a dummy function mirroring the real API's signature.

Figure 3: Valid API Invocation checking via an API stub.

Key Insights and Ideas

  • LLMs are highly sensitive to irrelevant context: Forcing an LLM to read documentation for an API it already knows well degrades its performance significantly (e.g., a ~39% absolute drop for GPT-4o on high-frequency APIs under standard DAG).
  • API name confidence is well-calibrated: The minimum token probability of the generated API name strongly correlates with whether the invocation is correct, making it an excellent trigger for selective RAG.
  • Syntax over semantics: Including detailed API syntax (arguments and types) is vastly more effective at reducing hallucinations than just providing natural language descriptions of what the API does.

Key Results

  • Base Model Weakness: Without augmentation, state-of-the-art models fail miserably on rare APIs. For instance, GPT-4o achieved only 38.58% validity on low-frequency APIs.
  • The DAG Dilemma: Standard DAG improved GPT-4o's low-frequency API validity to 47.94% but tanked its high-frequency performance from 93.66% down to 54.63%.
  • DAG++ Triumph: DAG++ achieved the best of both worlds. For GPT-4o, it maintained high-frequency accuracy at 94.15% while boosting low-frequency accuracy to 55.43%, yielding an overall absolute improvement of 8.20% over the base model across the entire benchmark. Similar consistent improvements were observed across other model families like StarCoder2, CodeGemma, Granite, and DeepSeekCoder.

Limitations and Future Work

  • Language and Scope: CloudAPIBench is currently restricted to Python and short synthetic prompts, which may not capture the full complexity of multi-lingual or repository-level cloud engineering.
  • Latency Overhead: The iterative "generate-retrieve-regenerate" loop adds computational overhead and latency, making it less suitable for real-time, low-latency applications.
  • Future Directions: The authors suggest extending the benchmark to long-context evaluations and exploring ways to make Code LLMs inherently more robust to irrelevant or noisy retrieved documents.

Summary updated

BibTeX
@inproceedings{jain2025mitigating,
  author = {Nihal Jain and Robert Kwiatkowski and Baishakhi Ray and Murali Krishna Ramanathan and Varun Kumar},
  title = {{On Mitigating Code LLM Hallucinations with API Documentation}},
  booktitle = {SEIP@ICSE},
  year = {2025},
  doi = {10.1109/icse-seip66354.2025.00027},
}
Figure