Skip to main content

CodeAct

CodeAct is a pattern, formalized in a 2024 paper by Wang et al. ("Executable Code Actions Elicit Better LLM Agents"), in which an AI agent emits executable code — typically Python — as its action, rather than emitting a structured tool-call JSON object. The code runs in a sandboxed interpreter; the agent observes the output and continues. Because the action space is the full expressivity of a programming language, a single action can compose loops, conditionals, library calls, and arbitrary computation that would otherwise require many separate tool-call turns. Several open-source coding agents and data-analysis agents adopt CodeAct or a CodeAct-like action surface, and the pattern often outperforms equivalent JSON-tool agents on multi-step reasoning tasks.

Example

Asked to multiply 47 and 83, a JSON-tool agent might emit {"tool": "calculator", "args": {"a": 47, "b": 83}}. A CodeAct agent instead emits the Python statement print(47 * 83); the runtime executes it in a sandbox and returns 3901 as the observation. For a task like "summarize the top 5 longest entries in this CSV," the CodeAct agent can write a single pandas snippet that reads, sorts, and slices the data — work that would take many turns of structured tool calls.

Put this into practice

Build polished, copy-ready prompts in under 60 seconds with SurePrompts.

Try SurePrompts