Uncertainty-Guided Chain-of-Thought for Code Generation with LLMs

Yuqi Zhu, Ge Li *, Xue Jiang, Jia Li, Hong Mei, Zhi Jin, Yihong Dong · 2025

Read Uncertainty-Guided Chain-of-Thought for Code Generation with LLMs by Yuqi Zhu, Ge Li *, Xue Jiang, Jia Li, Hong Mei, Zhi Jin, Yihong Dong (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2503-15341.txt.

Introduction

Yuqi Zhu and co-authors present in 2025 a way to stop language models from overthinking easy code. Step by step chain of thought reasoning boosts code generation, but models apply it blindly, burning compute on trivial tasks and sometimes reasoning themselves into wrong answers. Their method, UnCert-CoT, first asks how unsure the model is.

Two confidence gauges do the asking, one based on the entropy of the model's choices and one on probability gaps. When uncertainty runs high, the system opens full reasoning, samples several paths, and picks the code most likely correct. When uncertainty is low, the model just writes the code directly. The routing spends effort where errors actually breed. Across code benchmarks the approach improved both accuracy and efficiency in the authors' tests. The broader lesson travels beyond code: reasoning is a resource, and a model that knows its own doubt can budget it.

Catalog record

CREATORYuqi Zhu, Ge Li *, Xue Jiang, Jia Li, Hong Mei, Zhi Jin, Yihong Dong
YEAR2025
COLLECTIONPAPERS
FORMATTXT
FILE SIZE48 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2503-15341

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

Uncertainty-Guided Chain-of-Thought for Code Generation with LLMs Yuqi Zhu, Ge Li *, Xue Jiang, Jia Li, Hong Mei, Zhi Jin, Yihong Dong Abstract . Chain-of-Thought (CoT) reasoning has been demonstrated as an effective technique for improving the problem-solving capabilities of large language models (LLMs) in the context of code generation. However, existing CoT methods often exhibit a tendency toward "overthinking", where the LLM consistently applies reasoning strategies without adequately considering the task’s underlying complexity. This results in the LLMs allocating excessive computational resources, in terms of tokens, to relatively simple tasks or problems where the correct answer is already evident. Additionally, this overthinking may lead LLMs down incorrect reasoning paths, resulting in incorrect code generation. In this paper, we introduce UnCertainty-Aware Chain-of-Thought (UnCert-CoT), an LLM-based approach designed to enhance code generation by incorporating an uncertainty-aware CoT reasoning mechanism, which focuses computational resources on targeting points where LLMs are more prone to error. We propose two confidence-based uncertainty measures: Entropy-based and Probability Differential-based methods. When uncertainty is high, UnCert-CoT activates CoT-decoding to generate multiple reasoning paths and selects the final code that exhibits the highest likelihood of correctness. In contrast, LLM directly generates the code when uncertainty is low. This uncertainty judgment mechanism allows LLMs to prioritize complex tasks and avoid unnecessary steps in simpler cases, thereby improving overall efficiency and accuracy in code generation. Our experimental results demonstrate that UnCert-CoT significantly enhances code generation accuracy on challenging benchmark MHPP(Mostly Hard Python Problems), it achieves improvements up to 6.1% on PassRate accuracy, particularly in situations where traditional LLMs are prone to errors. DOI: XXXXXXX.XXXXXXX Conference: Make sure to enter the correct conference title from your rights confirmation email; June 03–05, 2018; Woodstock, NY ISBN: 978-1-4503-XXXX-X/2018/06 CCS: Software and its engineering CCS: Computing methodologies Artificial intelligence 1. Introduction Recent advances in Chain-of-Thought (CoT) reasoning have revolutionized the approach of large language models (LLMs) to tackle complex reasoning tasks. By decomposing problem-solving into sequential intermediate steps, CoT enables LLMs to emulate human-like logical deduction processes, significantly enhancing performance in problem-solving ( Zhou et al., 2023 ) and reasoning ( Wei et al., 2022 ) . The success of LLMs like DeepSeek-R1 ( Guo et al., 2025a ) has demonstrated CoT’s potential to address sophisticated reasoning challenges. The application of CoT has recently extended to the domain of code generation, with methods like Structured-CoT ( Li et al., 2025a ) and Self-Planning ( Jiang et al., 2024b ) offering innovative ways to enhance reasoning in code generation. Figure 1. An illustration of the overthinking phenomenon of CoT Code Generation methods. The LLM could have answered a question correctly, however, utilizing CoT Code Generation methods makes the LLM produce an incorrect reasoning chain and generate incorrect code answers. A key challenge of current CoT methods lies in the overthinking phenomenon identified in the work ( Chen et al., 2024 ) . Overthinking occurs when LLMs, despite having sufficient knowledge to generate correct answers directly, produce unnecessary reasoning steps. These unnecessary reasoning steps may distort the reasoning process and paradoxically raise the error rate. As demonstrated in Figure 1 , a LLM that could have answered a question correctly ends up producing an incorrect result after applying the CoT method, due to the generation of an erroneous reasoning chain. Moreover, this issue results in inefficient allocation of computational resources during the generation process. Resources should be allocated to areas that require deeper reasoning, while the counterparts, that can directly obtain the correct results, should not consume additional resources. To address these challenges, we introduce UnCertainty-Aware Chain-of-Thought (UnCert-CoT), a decoding method that dynamically invokes the CoT reasoning process based on the model’s uncertainty during generation. The key challenge lies in identifying difficult points within the LLM’s generation process and selectively applying CoT to guide the model through these complexities. Prior research ( Zhu et al., 2024 ) has highlighted that the prediction difficulty is particularly high at the beginning of a new line of code, as this position determines the subsequent logical structure the LLM construct next. Effectively handling this step places substantial demands on the LLM’s reasoning capabilities. Therefore, we conduct uncertainty detection at the critical juncture where the LLM generates a new line of code. When the LLM’s uncertainty exceeds a pre-defined threshold, we invoke a CoT-decoding reasoning method which can guide the LLM to generate code that exhibits a relatively high likelihood of correctness. To quantify uncertainty, we introduce two uncertainty measures: Entropy-based method and Probability Differential-based method. The Entropy-based approach estimates uncertainty by computing the information entropy ( Shannon, 1948 ) of LLM’s prediction for the first non-indentation token in each new line of code. The Probability Differential-based method quantifies uncertainty based on the difference between the highest and second-highest predicted probabilities. These measures enable the LLM to identify situations where the LLM has high uncertainty levels, thereby triggering the reasoning process. In cases of high uncertainty, UnCerT-CoT utilizes a CoT-decoding method to generate the subsequent line of code through a reasoning process. The CoT-decoding approach generates multiple reasoning steps, guiding the LLM to produce several potential solutions. Previous work ( Spiess et al., 2024 ) has demonstrated a calibration between the confidence value and the correctness of the generated code. Therefore, we select the result with high confidence values among the reasoning results. On the other hand, when uncertainty is low, the LLM proceeds directly with code generation, thus ensuring efficiency and avoiding overthinking. The UnCerT-CoT offers three key advantages: 1) Targeted reasoning: By dynamically invoking the reasoning method, UnCerT-CoT alleviates errors stemming from the LLM’s difficulty in accurately predicting the first token of a code line. 2) Efficiency preservation: UnCerT-CoT allows the LLM to proceed directly with code generation when uncertainty is low, thereby maintaining efficiency. 3) Error mitigation: UnCerT-CoT addresses the issue of error accumulation in extended reasoning chains. For more complex tasks, it decomposes the reasoning process into line-step-level reasoning, generating multiple reasoning paths and selecting the most confident outcome at each stage, ensuring that each reasoning step is optimized and reducing the risk of error propagation. We evaluated the performance of UnCerT-CoT and baseline approaches on two datasets: HumanEval ( Chen et al., 2021 ) , and MHPP ( Dai et al., 2024 ) . Our method demonstrates significant improvements, outperforming the baseline by 3.5% and 6.1% on the HumanEval and MHPP benchmarks, respectively. Additionally, we tested UnCerT-CoT on LLMs of varying sizes across different series, including DeepSeek-Coder ( Guo et al., 2024 ) , CodeLlama ( Rozière et al., 2023 ) , and Qwen-Coder ( Hui et al., 2024 ) , observing consistent performance improvements. Extensive experiments confirm that UnCerT-CoT is robust to changes in hyperparameter configurations. Additionally, we evaluated the effectiveness of the proposed CoT-decoding method. In summary, this paper makes the following contributions: • We identify the issue of overthinking in existing Chain-of-Thought (CoT) methods for code generation. • We propose a novel code generation approach called UnCert-CoT, which strategically incorporates uncertainty quantification during the code generation process to identify challenging aspects and selectively activates CoT to scale the reasoning process based on the identified challenges. • We introduce two uncertainty computation approaches, including Entropy-based and Probability Differential-based methods, to detect high-uncertainty scenarios and trigger

Read the complete text