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

Tree-of-Code: A Self-Growing Tree Framework for End-to-End Code Generation and Execution in Complex Tasks

Ziyi Ni, Yifan Li, Ning Yang, Dou Shen, Pin Lyu, Daxiang Dong

Abstract

Traditional LLM agents often solve complex tasks through step-by-step tool calling, a process that frequently leads to fragmented reasoning, context bloat, and high failure rates in zero-shot scenarios. We present Tree-of-Code (ToC), a novel framework that shifts the paradigm toward end-to-end code generation. By treating complete, executable programs as nodes in a self-growing search tree, ToC leverages execution success as a primary supervision signal. Our experiments on the M3ToolEval and API-Bank benchmarks demonstrate that ToC significantly outperforms existing step-by-step methods, achieving up to 20% higher accuracy while requiring less than one-fourth of the interaction turns.

Introduction

Current LLM agent frameworks, such as ReAct and CodeAct, typically decompose complex tasks into a sequence of discrete actions. While intuitive, this approach suffers from several critical limitations. First, it often relies on unrealistic termination criteria, such as ground-truth matching, which is unavailable in real-world applications. Second, the incremental generation of code blocks disrupts logical flow and consumes excessive context window space, leading to hallucinations and increased computational costs. Finally, these agents often follow a single reasoning path, making them brittle when initial steps fail.

Tree-of-Code (ToC) addresses these challenges by asking: What if we treat a complete, end-to-end program execution as a single atomic step? By generating global scripts rather than fragmented blocks, ToC maintains logical coherence and enables more robust exploration of the solution space.

The Tree-of-Code Framework

ToC transforms the task-solving process into a search over a tree of complete programs, known as CodePrograms. Each node in the tree represents a full script designed to solve the user's query in one go.

Comparison between CodeAct and Tree-of-Code (ToC) architectures.

The framework operates through three core mechanisms:

  1. Breadth-First Exploration: To ensure diversity, ToC samples from a pool of different LLMs and prompt variations at each level of the tree. This ensemble-like approach prevents the agent from getting stuck in a single suboptimal reasoning path.
  2. Depth-First Reflection: When a program fails to execute, the system captures the error message and uses it as feedback to generate a refined child node. This iterative self-correction allows the tree to "grow" toward a successful solution.
  3. Execution-Based Supervision: ToC is entirely ground-truth-free. It uses the binary signal of execution success to filter valid programs. Once the tree reaches its depth limit or finds successful paths, a majority vote among the successfully executed nodes determines the final answer.

To handle dynamic environments where future inputs depend on current outputs (e.g., web navigation), ToC utilizes helper functions like res_handler and next_action. These allow the generated code to pause and query the LLM for real-time observations without breaking the end-to-end script structure.

Experimental Evaluation

The authors evaluated ToC on two challenging benchmarks: M3ToolEval (M3), featuring 82 multi-tool tasks across diverse scenarios, and a modified version of API-Bank (Level-3) focused on complex API interactions.

A detailed example of ToC layers solving a travel planning task through iterative reflection.

Key Results

ToC demonstrated superior performance across multiple metrics compared to standard baselines:

  • Accuracy: On the M3 dataset, ToC achieved an average accuracy of 67.1%, a significant jump from CodeAct's 49.4%. On API-Bank, the improvement was even more pronounced, with ToC hitting 38.0% compared to 19.2% for CodeAct.
  • Efficiency: ToC drastically reduced the overhead of agent-environment interaction. On M3, it required only 1.7 average turns per task, compared to 7.0 turns for CodeAct. This reduction in turns also led to fewer output words and lower API costs.
  • Peak Performance: Utilizing Claude-3.5-Sonnet with prompt exploration, ToC reached a state-of-the-art accuracy of 84.1% on the M3 benchmark without any task-specific fine-tuning.

Discussion and Conclusion

The success of Tree-of-Code highlights the power of global planning and execution-based feedback in LLM agents. By replacing step-by-step "stalled thinking" with end-to-end program generation, ToC achieves both higher reliability and greater efficiency.

However, limitations remain. ToC is currently less suited for tasks requiring extensive physical exploration or infinitely long sequences. Future work will focus on enhancing the reflection mechanism beyond simple error messages—potentially incorporating full execution histories—and developing systematic methods for automatically constructing optimal prompt pools. Despite these areas for growth, ToC represents a significant step toward more autonomous and cost-effective AI agents for complex, real-world problem solving.

Summary updated

BibTeX
@inproceedings{ni2025tree,
  author = {Ziyi Ni and Yifan Li and Ning Yang and Dou Shen and Pin Lyu and Daxiang Dong},
  title = {{Tree-of-Code: A Self-Growing Tree Framework for End-to-End Code Generation and Execution in Complex Tasks}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure