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

Executable Code Actions Elicit Better LLM Agents

Xingyao Wang, Yangyi Chen, Lifan Yuan, Yizhe Zhang, Yunzhu Li, Hao Peng, Heng Ji

Introduction

Most AI agents interact with the world through rigid JSON or text formats, which limits their ability to handle complex logic. While Large Language Models (LLMs) are increasingly used as "agents" to control tools or robots, communicating with their environment using structured text creates several bottlenecks:

  1. Rigidity: JSON is great for simple API calls but struggles with complex logic like loops or conditional branching.
  2. Inflexibility: It is difficult to compose multiple tools together in a single step using standard text formats.
  3. Poor Feedback Loops: When a tool fails, the agent often receives a generic error message that doesn't help it "debug" its plan.

To effectively expand an agent's action space to solve complex, real-world problems, this paper proposes moving away from treating tools as isolated functions. Instead, it introduces CodeAct, a framework that treats executable Python code as the primary action space for LLMs.

Comparison of JSON/Text actions versus CodeAct, highlighting fewer required actions and improved success rates.

The CodeAct Framework

The core proposal of CodeAct is to consolidate all agent actions into executable Python code. By pairing the agent with a live Python kernel, the environment executes the emitted code and returns the output (or error traceback) as the next observation.

The CodeAct framework loop, illustrating how the agent uses code to interact with the environment and refine its actions based on observations.

This approach offers several distinct advantages:

  • Unified Action Space: Instead of learning a specific JSON schema for every new tool, the agent simply writes Python. This allows it to use variables to store intermediate results and use standard programming constructs (if statements, for loops) to manage complex data flows.
  • Zero-Shot Tool Integration: Because the agent can import any existing Python library (like pandas or scikit-learn), it doesn't need a human to manually "wrap" every tool into a specific API format. It can simply use the software ecosystem as it exists.
  • Self-Debugging: Python provides detailed error messages (tracebacks). The agent can use these automated feedback signals to autonomously correct its own code in subsequent turns, providing a powerful, low-cost way to improve performance without human intervention.

To train open-source models to use this framework, the authors curated CodeActInstruct, a dataset of 7,000 high-quality multi-turn interactions covering domains like information seeking, software package usage, math, external memory (SQL/Pandas), and robot planning.

Evaluation and Key Results

The researchers evaluated 17 different LLMs using existing benchmarks like API-Bank and a newly curated benchmark called M3ToolEval. Unlike existing benchmarks, M3ToolEval requires intricate coordination of multiple tools over many turns (e.g., complex travel planning or financial analysis).

The results demonstrate clear advantages for the CodeAct approach:

  • Higher Success Rates: CodeAct outperformed JSON/Text actions on 12 out of 17 evaluated models. On the complex M3ToolEval benchmark, it achieved up to a 20% absolute improvement in success rate.
  • Efficiency: CodeAct agents solved problems with up to 30% fewer interaction turns compared to their JSON-based counterparts, largely due to the "control flow" advantage of processing multiple steps in a single turn.
  • Open-Source Performance: Using the CodeActInstruct dataset, the authors fine-tuned CodeActAgent-Mistral-7B. This model significantly outperformed other open-source models and approached the performance of much larger proprietary models on specific agentic tasks.
  • Capability Scaling: The performance gap between CodeAct and JSON widened as the underlying LLM became more powerful, suggesting that better reasoning models benefit more from the flexibility of code.

Limitations and Future Work

Despite its success, the CodeAct framework has limitations. Agents sometimes "hallucinate" the contents of a variable (e.g., assuming a data frame has certain values) instead of printing it to verify, leading to logic errors. Furthermore, executing arbitrary Python code presents a major security risk; deploying such agents in the real world requires robust sandboxing and safety guardrails to prevent the agent from "breaking out."

Finally, while CodeAct helps bridge the gap, there remains a massive performance difference between open-source models and state-of-the-art proprietary models like GPT-4 on complex reasoning tasks. Future directions include "iterative self-improvement," where agents learn from their own successful and failed trajectories to bootstrap their capabilities over time.

Summary updated

BibTeX
@inproceedings{wang2024executable,
  author = {Xingyao Wang and Yangyi Chen and Lifan Yuan and Yizhe Zhang and Yunzhu Li and Hao Peng and Heng Ji},
  title = {{Executable Code Actions Elicit Better LLM Agents}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=jJ9BoXAfFa},
}
Figure