[Paper Review] Executable Code Actions Elicit Better LLM Agents
CodeAct enables LLM agents to emit executable Python code as actions, integrated with a Python interpreter to execute, revise, and compose tool calls, yielding better performance than text/JSON action formats, and enabling open-source CodeActAgent fine-tuned from Llama2 and Mistral with CodeActInstruct.
Large Language Model (LLM) agents, capable of performing a broad range of actions, such as invoking tools and controlling robots, show great potential in tackling real-world challenges. LLM agents are typically prompted to produce actions by generating JSON or text in a pre-defined format, which is usually limited by constrained action space (e.g., the scope of pre-defined tools) and restricted flexibility (e.g., inability to compose multiple tools). This work proposes to use executable Python code to consolidate LLM agents' actions into a unified action space (CodeAct). Integrated with a Python interpreter, CodeAct can execute code actions and dynamically revise prior actions or emit new actions upon new observations through multi-turn interactions. Our extensive analysis of 17 LLMs on API-Bank and a newly curated benchmark shows that CodeAct outperforms widely used alternatives (up to 20% higher success rate). The encouraging performance of CodeAct motivates us to build an open-source LLM agent that interacts with environments by executing interpretable code and collaborates with users using natural language. To this end, we collect an instruction-tuning dataset CodeActInstruct that consists of 7k multi-turn interactions using CodeAct. We show that it can be used with existing data to improve models in agent-oriented tasks without compromising their general capability. CodeActAgent, finetuned from Llama2 and Mistral, is integrated with Python interpreter and uniquely tailored to perform sophisticated tasks (e.g., model training) using existing libraries and autonomously self-debug.
Motivation & Objective
- Motivate expanding LLM agents' action space beyond text/JSON to handle real-world tasks.
- Propose a unified action space using executable Python code (CodeAct) integrated with a Python interpreter.
- Demonstrate CodeAct's advantages in control and data flow, reusability, and automated error-driven self-debugging.
- Show benefits on a multi-LLM evaluation across 17 models and introduce CodeActInstruct for instruction tuning.
Proposed method
- Define CodeAct as emitting Python code actions executed by an embedded Python interpreter.
- Compare CodeAct with text/JSON actions on atomic tool use and complex multitool tasks using API-Bank and a new M3 ToolEval benchmark.
- Curate M3 ToolEval with 82 multi-turn, multi-tool tasks to assess tool composition performance.
- Create CodeActInstruct containing 7k multi-turn trajectories to train CodeActAgent and study self-improvement from interactions.
- Fine-tune CodeActAgent (LLaMA-2 and Mistral-7B backbones) on CodeActInstruct plus general conversations to assess performance across agent tasks and general benchmarks.

Experimental results
Research questions
- RQ1Does LLM familiarity with code data provide advantages for CodeAct over text/JSON in atomic tool use (RQ1)?
- RQ2Does Python's control and data flow enhance performance on complex, multi-tool tasks (RQ2)?
- RQ3How do multi-turn interactions and existing software impact CodeAct robustness and capabilities (RQ3)?
- RQ4Can CodeAct be used to train open-source LLM agents that perform sophisticated tasks with self-debugging (RQ4)?
Key findings
- CodeAct achieves comparable or better atomic tool-call correctness than text/JSON, with open-source models showing larger gains.
- CodeAct yields up to 20% absolute improvement on the M3 ToolEval benchmark for complex tasks requiring multiple tools, and uses up to 30% fewer actions.
- CodeActInstruct (7k trajectories) improves agent performance, and mixing CodeActInstruct with general conversations preserves broad capabilities.
- CodeActAgent (fine-tuned on CodeActInstruct) outperforms open-source LLMs on in-domain and out-of-domain agent tasks in MINT and M3 ToolEval, and can generalize to text-action formats.
- CodeActAgent can autonomously self-debug by leveraging Python error messages, import of libraries, and external tools like Pandas, Scikit-Learn, and Matplotlib.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.