Abstract
Current Large Language Model (LLM) prompting techniques for code generation, such as Chain-of-Thought (CoT), often rely on linear, monolithic reasoning paths. This approach frequently leads to context confusion and error propagation in complex tasks. This paper introduces Modularization-of-Thought (MoT), a novel framework that encourages LLMs to decompose problems into a hierarchical Multi-Level Reasoning (MLR) Graph before code generation. Experimental results across multiple benchmarks demonstrate that MoT significantly improves code accuracy and maintainability while remaining more cost-effective than iterative self-repair methods.
Introduction
While LLMs have shown remarkable proficiency in generating code, complex multi-step programming problems remain a challenge. Existing methods like Chain-of-Thought (CoT) or Structured CoT (SCoT) promote "step-by-step" thinking but maintain a sequential, monolithic structure. In real-world software engineering, problems are solved modularly; a single error in a linear reasoning chain can derail the entire output due to context confusion.
The Modularization-of-Thought (MoT) approach addresses this by applying classic software engineering principles—specifically modularization—to the model's internal reasoning process. By forcing the model to plan a hierarchical structure, MoT mitigates context confusion and enhances the logical coherence of the generated code.
Methodology: The MoT Framework
The MoT framework operates in two distinct phases: the MLR Graph Generation Phase and the Code Generation Phase.
1. MLR Graph Generation Phase
Instead of immediate code production, the LLM first constructs a Multi-Level Reasoning (MLR) Graph. This graph organizes the solution into three tiers:
- High-Level: Outlines primary tasks such as input validation and core algorithmic logic.
- Intermediate-Level: Refines high-level tasks into specific sub-tasks.
- Detailed-Level: Provides granular implementation details or pseudo-code.
Crucially, each node in this graph is enriched with a reasoning block that specifies the task's purpose, decision rationale, and execution strategy.
2. Code Generation Phase
In the second phase, the model translates the MLR graph into modular code. The hierarchical structure of the graph guides the model to produce independent yet interconnected functions or segments, ensuring the final output aligns perfectly with the pre-defined logic.
Experimental Evaluation
The authors evaluated MoT using GPT-4o-mini and DeepSeek-R1 across six benchmarks: HumanEval (including + and ET variants) and MBPP (including + and ET variants). These benchmarks range from basic Python problems to rigorous test cases designed to challenge model robustness.
Key Results
MoT consistently outperformed baseline techniques such as CoT, SCoT, and Self-Planning:
- Performance: MoT achieved Pass@1 scores between 58.1% and 95.1%. Notably, using DeepSeek-R1, MoT reached a 95.1% Pass@1 on HumanEval.
- Efficiency: Unlike iterative "generate-test-fix" loops (e.g., CodeCoT), MoT focuses on structural correctness from the start. This resulted in significantly faster generation times (e.g., 5 seconds for MoT vs. 13 seconds for CodeCoT on HumanEval) and lower token costs compared to methods like Self-Planning.
Discussion and Insights
The success of MoT highlights several key insights:
- Hierarchical Reasoning: LLMs are more effective when viewing problems as a tree of sub-tasks rather than a linear sequence. This prevents the model from getting "lost" in the middle of long sequences.
- Self-Documentation: Requiring the model to articulate its rationale for each sub-task improves both accuracy and code auditability for human developers.
- Structural Priority: Getting the architectural structure right initially is more efficient and cost-effective than post-hoc iterative repairs.
Limitations and Future Work
While effective, MoT is subject to the inherent stochasticity of LLMs, where different seeds may produce varying graph structures. Additionally, the current evaluation is limited to Python. Future research will explore MoT's applicability to other programming languages (e.g., C++, JavaScript) and more complex domains like large-scale system development.
Conclusion
Modularization-of-Thought represents a significant step forward in AI-assisted coding by bridging the gap between LLM reasoning and established software engineering practices. By prioritizing hierarchical planning, MoT delivers more accurate, maintainable, and cost-effective code generation.
Summary updated