This paper introduces CodeAct, a framework that enables Large Language Models (LLMs) to generate executable Python code as actions, allowing them to interact with environments and perform complex tasks. CodeAct integrates with a Python interpreter, enabling dynamic adjustment of actions based on observations and the execution of code actions. It offers several advantages over traditional text and JSON action formats, including the ability to leverage existing software packages, support for control and data flow, and the use of automated feedback for self-debugging. The framework is evaluated on 17 LLMs, showing significant improvements in task success rates and efficiency. CodeActInstruct, a dataset of multi-turn interactions, is introduced to enhance LLMs' ability to improve through interaction. CodeActAgent, a model fine-tuned from Llama2 and Mistral, demonstrates superior performance in both CodeAct tasks and general LLM tasks. The study highlights the potential of CodeAct in real-world applications, including autonomous agents that can interact with environments and collaborate with humans through natural language. The research also discusses the importance of safety mechanisms for autonomous agents and the potential societal impact of advanced LLM-based agents.This paper introduces CodeAct, a framework that enables Large Language Models (LLMs) to generate executable Python code as actions, allowing them to interact with environments and perform complex tasks. CodeAct integrates with a Python interpreter, enabling dynamic adjustment of actions based on observations and the execution of code actions. It offers several advantages over traditional text and JSON action formats, including the ability to leverage existing software packages, support for control and data flow, and the use of automated feedback for self-debugging. The framework is evaluated on 17 LLMs, showing significant improvements in task success rates and efficiency. CodeActInstruct, a dataset of multi-turn interactions, is introduced to enhance LLMs' ability to improve through interaction. CodeActAgent, a model fine-tuned from Llama2 and Mistral, demonstrates superior performance in both CodeAct tasks and general LLM tasks. The study highlights the potential of CodeAct in real-world applications, including autonomous agents that can interact with environments and collaborate with humans through natural language. The research also discusses the importance of safety mechanisms for autonomous agents and the potential societal impact of advanced LLM-based agents.