Manuscript Offprint

arXiv:2609.01601

Adaptive Critical Token-Aware Retrieval for Repository-Level Code Generation

Kefeng Duan, Dewu Zheng, Yanlin Wang, Terry Yue Zhuo, Mingwei Liu, Jianxing Yu, Jiachi Chen, Ensheng Shi, Xilin Liu, Yuchi Ma, Zibin Zheng

Kefeng Duan*, Dewu Zheng*, Yanlin Wang†, Terry Yue Zhuo, Mingwei Liu, Jianxing Yu, Jiachi Chen, Ensheng Shi, Xilin Liu, Yuchi Ma, and Zibin Zheng

Abstract—The repository-level code generation task requires synthesizing code that satisfies task requirements while remaining consistent with the target repository context. Since real-world repositories often exceed the input length limits of LLMs, existing approaches commonly adopt retrieval-augmented generation (RAG) to provide repository-specific context. Despite improving repository-context retrieval, existing methods typically provide context as task-level support, without explicitly identifying the critical tokens that require fine-grained repository context during generation. During the autoregressive generation process of LLMs, errors often concentrate at a small number of decisive positions: once such tokens are generated incorrectly, subsequent code may follow an incorrect semantic path and eventually lead to functional failure. We refer to these positions as “critical tokens”. In this paper, we propose ACTOR, an adaptive critical token-aware retrieval framework for repository-level code generation. ACTOR identifies critical tokens during generation and triggers targeted retrieval on demand to provide repository context at these decisive positions. In addition, we design a position-aware weighting method for dense retrievers to prioritize context that is more informative for generation. We evaluate ACTOR on two representative repository-level benchmarks, RepoExec and CoderEval. Experimental results show that ACTOR consistently outperforms state-of-the-art methods, achieving relative improvements of 8.4% on RepoExec and 15.4% on CoderEval. Beyond performance gains, we systematically quantify the impact of critical tokens, revealing their central role in major generation failures and highlighting the necessity of targeted retrieval strategies. We provide the code and data at https://github.com/DeepSoftwareAnalytics/ACTOR.

Index Terms—Repository-level code generation, large language models, retrieval-augmented generation, critical token, dense retriever.

I. INTRODUCTION

Recent LLMs are increasingly used in real-world software development [1]–[4]. Unlike standalone code-generation tasks [5], [6], repository-level code generation targets real-world development within existing repositories. In this setting, generated code must not only satisfy task requirements but also remain consistent with project-specific APIs, dependencies, data structures, coding conventions, and the broader repository context [7]–[9].

Therefore, repository context is essential for resolving project-specific dependencies and preserving consistency with the existing codebase. However, real-world code repositories often exceed the input length limits of LLMs, making it infeasible to provide the entire repository as input. To address this challenge, existing studies have widely adopted retrieval-augmented generation (RAG), which first retrieves task-relevant context from the repository and then incorporates it into the generation prompt. Representative repository-level RAG methods have improved contextual support from several perspectives, including context quality and retrieval decision-making. To improve context quality, RepoCoder [10] iteratively constructs new retrieval queries using generated results from previous rounds, Cocomic [11] locates relevant code dependencies based on a project context graph, and DRACO [12] retrieves fine-grained code entities through dataflow analysis. In retrieval decision-making, RepoFormer [13] determines whether retrieval should be triggered for the current task, RLPG [14] identifies the type of dependency context required by the task, and ProCC [15] dynamically selects suitable retrieval prompt construction strategies. Overall, these methods improve repository-level code generation mainly by enhancing how repository context is obtained, selected, and organized.

Although the above methods improve the quality of repository context from the perspectives of retrieval strategy, context source, and retrieval decision-making, they still organize and use context mainly at the task level, lacking explicit modeling of key token-level decisions during generation. Specifically, existing methods typically treat the retrieved context as holistic support for the entire generation task, implicitly assuming that a set of relevant repository contexts can continuously serve the subsequent generation process. However, code generation is a highly path-dependent autoregressive process, and different generation positions may require different contextual information.

In repository-level code generation, an incorrect API name, variable reference, index, or control-flow token can cause subsequent generation to deviate from repository semantics and eventually fail the entire implementation. In other words, code generation errors are often not accumulated uniformly across all tokens, but instead concentrate at a small number of positions that have a decisive impact on the final result. We call these positions critical tokens: a small number of tokens in the LLM inference chain whose correctness plays

*K. Duan and D. Zheng are co-first authors with equal contribution.

†Y. Wang is the corresponding author (e-mail: yanlin-wang@outlook.com).

K. Duan, D. Zheng, Y. Wang, M. Liu, and Z. Zheng are with Sun Yat-sen University, Guangzhou 510006, P.R. China (e-mail: duankf@mail2.sysu.edu.cn; zhengdw5@mail2.sysu.edu.cn; yanlin-wang@outlook.com; liumw26@mail.sysu.edu.cn; zhzibin@mail.sysu.edu.cn).

J. Yu is with the School of Artificial Intelligence, Sun Yat-sen University, Zhuhai 519082, P.R. China (e-mail: yujx26@mail.sysu.edu.cn).

T. Y. Zhuo is with Monash University and CSIRO's Data61 (e-mail: terry.zhuo@monash.edu).

J. Chen is with Zhejiang University (e-mail: chenjiachi@zju.edu.cn).

E. Shi, X. Liu, and Y. Ma are with Huawei Cloud Computing Technologies Co., Ltd. (e-mail: shiensheng@huawei.com; liuxilin3@huawei.com; mayuchi1@huawei.com).

a decisive role in the correctness of the final answer, and whose incorrect generation may cause the entire generated result to fail. Therefore, the core limitation of existing retrieval pipelines is not merely that retrieval occurs before generation or remains at the query level, but that they lack a mechanism to identify these critical tokens and provide targeted repository context support at these key positions.

Based on this observation, we introduce ACTOR (Adaptive Critical Token-aware Retrieval), a dynamic, fine-grained retrieval framework. Unlike static methods, our framework operates during the generation process. It identifies critical tokens where the model requires additional context and performs targeted, on-demand retrieval for each one. This adaptive mechanism ensures that the model always has the precise information it needs, thereby mitigating the quality degradation and functional failures common in existing systems. Besides, we propose a position-aware weighting method for dense retrievers. This allows us to retrieve context that is more informative for generation.

We validate our approach on two standard repository-level code generation benchmarks: RepoExec [16] and CoderEval [7]. ACTOR consistently improves over strong repository-level baselines, with relative gains of 8.4% on RepoExec and 15.4% on CoderEval. Complementing these end-to-end results, we provide a quantitative characterization of critical tokens: they occupy only a small fraction of generated positions (about 5–11% across models), yet their composition and syntactic profiles concentrate much of the model's error and uncertainty, motivating sparse, generation-time retrieval updates rather than repository context supplied only once up front. Our analysis further ties major failures to this small set of pivotal positions, underscoring the necessity of targeted retrieval strategies like ACTOR.

We summarize our contributions as follows:

• We introduce the concept of critical tokens for repository-level code generation. To our knowledge, this is the first work to systematically quantify the decisive impact these tokens have on the quality of the final generated code.

• We propose a novel targeted retrieval framework designed for these critical tokens. This framework performs on-demand retrieval to supply the language model with precise, timely context at the most crucial steps of the generation process.

• We design a position-aware weighted pooling method to enhance our retrieval mechanism. This technique refines the context embedding process, significantly boosting the precision of retrieval.

• We conduct extensive experiments on representative repository-level code generation benchmarks, demonstrating that our approach significantly and consistently outperforms existing SOTA methods.

References (68)