Manuscript Offprint

arXiv:2609.11023

RCL: A Retrieval-Confidence Layer for Detecting Insufficient Context in Enterprise Retrieval-Augmented Code Generation

Chandra Mohan Ravuri

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Retrieval-Augmented Generation (RAG) for code generation has been extensively studied on public repositories, where a large language model's parametric knowledge, acquired during pretraining, often compensates for imperfect or incomplete retrieval. This compensating effect breaks down in enterprise codebases, where private APIs, internal frameworks, and undocumented team conventions fall entirely outside any model's pretraining distribution. Recent work on private-library code generation shows that even oracle (perfect) retrieval does not eliminate errors, but locates the remaining failures downstream, in API usage; separately, confidence-gated retrieval has been studied for general open-domain question answering using model-internal confidence. Neither addresses whether retrieval itself was structurally sufficient for a private-code query before generation begins. We introduce RCL (Retrieval-Confidence Layer), a lightweight module inserted between retrieval and generation that combines a call-graph-derived structural coverage score with a novelty score estimating a query's dependence on knowledge outside the model's prior, to detect insufficient retrieval before generation occurs. When the combined confidence score falls below a calibrated threshold, RCL triggers a targeted follow-up retrieval pass or labels the eventual output for human review, rather than allowing the system to generate silently against incomplete context. We describe RCL's architecture, formalize its scoring functions, and propose an evaluation methodology using a private-code benchmark constructed by injecting synthetic internal APIs and naming conventions into open-source Java repositories, simulating the enterprise condition without requiring access to proprietary code. We report results (Section 7) comparing RCL against similarity-only retrieval baselines on generation correctness. Our position is that retrieval sufficiency, assessed structurally rather than from model-internal confidence, is a distinct and currently underaddressed signal for building safer code-generation systems in private, enterprise settings.

1. Introduction

Large language models (LLMs) integrated with Retrieval-Augmented Generation (RAG) have become a standard approach to code generation over large codebases, where relevant context is distributed across many files and exceeds any practical context window. Systems such as RepoCoder, GraphCoder, and RLCoder each improve on how relevant context is selected — through iterative retrieval-generation cycles, code-structure graphs, and learned retrieval policies, respectively. A common assumption underlies all of them: that retrieval, even if imperfect, is a strict improvement over generation from parametric knowledge alone.

This assumption is reasonable on public repositories, where the LLM has typically seen large amounts of similar code during pretraining. A gap in retrieval is frequently masked by the model's own prior knowledge of common libraries, idioms, and APIs. The assumption does not transfer to enterprise codebases. Internal APIs, proprietary frameworks, legacy in-house patterns, and undocumented team conventions are, by construction, absent from any public pretraining corpus. When retrieval is incomplete in this setting, there is no parametric fallback — the model is forced to either hallucinate a plausible-looking internal API or silently misapply a convention it has never actually seen, producing generations that are fluent, confident, and wrong.

We argue that the missing ingredient is not better retrieval per se, but a mechanism to detect when retrieval is insufficient before generation is allowed to proceed. We term this retrieval sufficiency, and distinguish it from retrieval relevance: a retrieved chunk can be highly relevant by semantic similarity while still leaving the query's actual dependency surface uncovered.

This paper makes three contributions:

• We formalize retrieval sufficiency for code RAG as a measurable property distinct from semantic relevance, combining structural call-graph coverage with a novelty estimate of the query's dependence on out-of-prior knowledge.

• We propose RCL, a retrieval-confidence layer that sits between retrieval and generation, computing a sufficiency score and routing low-confidence queries to targeted follow-up retrieval or human review rather than silent generation.

• We describe a benchmark construction methodology that simulates the enterprise, private-codebase condition using open-source repositories with injected synthetic internal APIs, enabling reproducible evaluation without requiring proprietary code.

References (11)