Abstract
Large Language Model (LLM) agents often struggle with complex tasks due to fragmented reasoning and a lack of exploration in traditional step-by-step interaction paradigms. We present Tree-of-Code (ToC), a framework that treats end-to-end code programs as atomic units of reasoning. By organizing these programs into a self-growing tree structure and utilizing execution success as a ground-truth-free supervision signal, ToC significantly improves task accuracy and interaction efficiency compared to traditional methods.
Introduction
Current LLM agents typically follow the ReAct (Reason + Act) or CodeAct paradigms. While effective for simple tasks, these methods suffer from "fragmented thinking"—generating one tool call or code block at a time and waiting for feedback. This approach leads to several critical flaws: unrealistic termination requirements (assuming ground truth knowledge), context bloat due to excessive interaction turns, a lack of exploration beyond a single linear path, and poor data reusability for fine-tuning.
Methodology
Tree-of-Code (ToC) addresses these limitations by shifting the focus from individual tool calls to complete, end-to-end scripts called CodePrograms.
The CodeProgram Node
A CodeProgram is an atomic unit containing the entire logic required to solve a task, including global planning and error handling. This allows the model to maintain a global logical chain, reducing hallucinations common in multi-turn dialogues.
The Self-Growing Tree
ToC organizes these CodePrograms into a tree structure characterized by breadth and depth:
- Breadth (Exploration): The framework generates multiple candidate CodePrograms in parallel using a "Random Forest" approach, varying LLMs and prompts to ensure diverse reasoning paths.
- Depth (Reflection): If a program fails execution, the error message is fed back to the model to generate a refined version in the subsequent layer.
Ground-Truth-Free Supervision
ToC uses execution success as its primary filter. By collecting all programs that run without errors and applying a majority vote to their outputs, the framework identifies valid solutions without requiring external gold-standard labels.
Evaluation
The framework was evaluated using ten LLMs (including GPT-4, Claude 3, and DeepSeek) on two benchmarks:
- M3ToolEval (M3): 82 tasks involving 100 tools across diverse domains like DNA sequencing and travel planning.
- API-Bank (Level 3): Complex tool-use dialogues modified for end-to-end code generation.
Results
ToC demonstrates a significant "more with less" advantage over state-of-the-art methods:
- Accuracy: On the M3 dataset, ToC achieved 67.1% accuracy, a nearly 20% improvement over CodeAct (49.4%).
- Efficiency: ToC solved tasks in an average of 1.7 to 2.1 turns, compared to 7.0 to 9.5 turns for traditional methods.
- Cost Reduction: By reducing interaction turns and avoiding context repetition, output word counts were reduced by approximately 75%, lowering API costs.
- Model Performance: Claude-3.5-Sonnet emerged as the top performer within the ToC framework.
Conclusion
Tree-of-Code rethinks LLM agent interaction by prioritizing end-to-end code exploration over fragmented tool use. By leveraging execution feedback as a weak supervision signal, ToC provides a robust, efficient, and cost-effective solution for complex task handling. Future work will explore more sophisticated reflection mechanisms and automated prompt engineering to further enhance the diversity of the reasoning tree.
Summary updated