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

CodeCoT and Beyond: Learning to Program and Test like a Developer

Dong Huang, Qingwen Bu, Heming Cui · DOI

Abstract

Large Language Models (LLMs) often struggle with generating executable code despite strong logical reasoning capabilities. This paper introduces CodeCoT, a framework that integrates Chain-of-Thought (CoT) prompting with an iterative self-examination loop. By generating, executing, and debugging its own test cases, CodeCoT significantly reduces syntax and runtime errors. Experimental results show a substantial performance boost, with pass@1 accuracy on the MBPP dataset rising from 69.8% to 89.5%.

Introduction

While Chain-of-Thought (CoT) prompting has revolutionized LLM performance in logic and mathematics, its application to programming reveals a "Logic-Syntax Paradox." Models often produce sound narrative reasoning but fail to generate valid code due to strict syntax requirements. CodeCoT addresses this by bridging the gap between high-level reasoning and low-level execution through a structured pipeline of generation and self-correction.

Methodology: The CodeCoT Pipeline

CodeCoT employs a four-stage process to ensure code quality and executability:

  1. CoT Prompting: Guiding initial reasoning with structured examples.
  2. Code Generation: Translating reasoning into functional code.
  3. Test Case Generation: Creating a suite of tests (typically five) to validate the generated code.
  4. Self-Examination Loop: Executing the code against tests in a local environment. If errors occur, the traceback is fed back to the model for iterative debugging (up to 5 steps).
The CodeCoT framework: from CoT prompting to iterative self-examination.

Experimental Evaluation

The framework was tested on industry-standard benchmarks including HumanEval and MBPP, as well as their enhanced versions (HumanEval-ET and MBPP-ET) which feature more rigorous test suites.

Key Results

CodeCoT achieves state-of-the-art results across multiple benchmarks. Notably, it pushes HumanEval performance to 79.3% and MBPP to 89.5%. When integrated with GPT-4, these figures reach 87.2% and 92.6%, respectively.

Comparison of CodeCoT against various models and prompting strategies across HumanEval and MBPP benchmarks.

Discussion and Insights

The primary driver of CodeCoT's success is the reduction of "trivial" errors. By step 5 of the self-examination loop, syntax errors in HumanEval dropped from 36% to just 2%. This suggests that feedback from a local interpreter is far more effective than simple "wrong/right" signals. Furthermore, applying CoT to the test-generation phase itself ensures higher-quality validation, preventing the model from being misled by faulty tests.

Limitations

The iterative nature of CodeCoT introduces higher computational costs and latency. Additionally, the system's reliance on self-generated tests means that "hallucinated" test cases can occasionally lead the model to break functional code. Future research will focus on improving test case verification and optimizing the loop for smaller open-source models.

Summary updated

BibTeX
@misc{huang2023codecot,
  author = {Dong Huang and Qingwen Bu and Heming Cui},
  title = {{CodeCoT and Beyond: Learning to Program and Test like a Developer}},
  year = {2023},
  eprint = {2308.08784},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2308.08784},
}
Figure