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

CodeChain: Towards Modular Code Generation Through Chain of Self-revisions with Representative Sub-modules

Hung Le, Hailin Chen, Amrita Saha, Akash Gokul, Doyen Sahoo, Shafiq Joty

TL;DR

CodeChain is a novel inference framework that significantly improves Large Language Model (LLM) performance on complex programming tasks by prompting the model to generate modular code. By iteratively clustering generated sub-modules and using the most representative ones to guide self-revision, CodeChain achieves relative performance boosts of up to 76% on challenging competitive programming benchmarks.

What Problem Does This Paper Address?

While LLMs have mastered simple coding tasks (like those in HumanEval), they often stumble on complex, competitive-level problems. The authors argue this is because LLMs typically generate code as a "monolithic block"—one giant chunk of logic.

In contrast, experienced human programmers solve complex problems by decomposing them into logical sub-tasks and reusable modules. Current LLM prompting methods don't naturally encourage this decomposition. Even when they do, models often struggle to maintain correctness across multiple interconnected functions. CodeChain aims to bridge this gap by mimicking the human "create-verify-reuse" development cycle.

Methods

CodeChain introduces a multi-step inference process that moves beyond simple one-shot generation:

CodeChain Framework
  1. Modular Chain-of-Thought (CoT) Prompting: Instead of asking for a solution directly, the model is first instructed to outline the necessary sub-modules (function headers and docstrings) and then implement them.
  2. Sub-module Extraction & Clustering: The system generates multiple solution candidates. It extracts the sub-modules from these candidates and maps them into an embedding space.
  3. Representative Selection: It performs K-means clustering on these embeddings. For each cluster, it selects the "centroid" sub-module—the implementation that is most representative of that specific functional logic.
  4. Chain of Self-Revisions: The original prompt is augmented with these representative sub-modules. The LLM is then instructed to "reuse or adapt" these verified pieces to generate a new, more robust solution. This process is repeated for several rounds.

This approach is "supervision-free," meaning it doesn't require fine-tuning the model on specialized datasets; it works entirely through clever prompting and algorithmic selection during inference.

Datasets

The authors evaluated CodeChain on two high-difficulty benchmarks:

  • APPS: Contains 5,000 problems ranging from "Introductory" to "Competition" levels.
  • CodeContests: A challenging dataset curated from competitive programming platforms like Codeforces.

Unlike simpler benchmarks, these datasets require complex algorithmic reasoning and handle a wide variety of edge cases.

Key Insights and Ideas

  • The Modularity Paradox: Interestingly, simply asking an LLM to write modular code can actually decrease performance in the first round. LLMs aren't always "trained" to be good software architects out of the box. However, this modular structure is exactly what enables the massive gains seen during subsequent revision rounds.
  • Collective Intelligence: By clustering sub-modules across multiple independent samples, CodeChain identifies "correct" logic even if it appeared in a program that was otherwise failing. It essentially harvests the best parts of many failed attempts to build a successful one.
  • Exploration to Exploitation: The most effective strategy for revision involves starting with a higher number of clusters (to capture diverse logic) and decreasing the number of clusters in later rounds (to converge on the most reliable implementations).

Key Results

CodeChain demonstrated impressive gains across both closed-source (OpenAI) and open-source (WizardCoder) models:

Performance across difficulty levels
  • APPS: Achieved a relative 35% improvement in pass@1 accuracy. For example, GPT-3.5 jumped from 22.33% to 30.24%.
  • CodeContests: Achieved a massive 76% relative improvement.
  • State-of-the-Art (SoTA): When combined with GPT-4, CodeChain reached a pass@1 of 61.5% on a subset of APPS, even outperforming "Self-repair" methods that use human-provided feedback.
  • Modularity Quality: Qualitative evaluations showed that CodeChain-generated solutions were consistently rated higher by human judges for both modularity and reusability compared to standard CoT.

Limitations and Future Work

  • Dependency on Filtering: The method's success relies on having at least some public test cases to filter out obviously broken modules before clustering. In "zero-test" scenarios, the clustering might be noisier.
  • Overfitting Risks: The authors noted a slight performance dip after five rounds of revision in some cases, suggesting the model might begin to overfit to the specific public test cases provided in the prompt.
  • Computational Cost: Generating multiple samples and performing clustering adds overhead compared to a single generation, though the authors argue the performance gain justifies the cost for complex tasks.

Future work could involve exploring better embedding models specifically tuned for functional semantics or integrating the modularity-first approach directly into the pre-training or fine-tuning stages of LLM development.

Summary updated

BibTeX
@inproceedings{le2024codechain,
  author = {Hung Le and Hailin Chen and Amrita Saha and Akash Gokul and Doyen Sahoo and Shafiq Joty},
  title = {{CodeChain: Towards Modular Code Generation Through Chain of Self-revisions with Representative Sub-modules}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=vYhglxSj8j},
}
Figure