Actions written as Python
In the ReAct and Tool calling tabs an action names one tool and its arguments, which the loop calls once. CodeAct (Wang et al., 2024) lets the action be a block of Python instead. The loop runs it and shows the model what it printed, or its error, as the observation. One action can loop over a hundred numbers, keep a result for the next action or use the standard library, which a single call cannot.
This tab gives Qwen3-1.7B, thinking mode off, eight small tasks about primes, dates and digits, twice: once with code actions and once with the chat template's own JSON tool calls. Both get the same three functions, is_prime, weekday and days_between, the same greedy decoding and at most 8 replies of 512 tokens. Each reference answer comes from running a Python expression with those functions, shown under Every task.
The sandbox
Each task gets one Python interpreter started under macOS's sandbox-exec, with a profile that denies every network operation and every file write outside a fresh empty folder, and as python -I -S -B, so only the standard library and the three functions can be imported. The drawer's check tries both walls: a connection to the internet and a write into the repository fail. An action may run for 10 seconds before the interpreter is killed and restarted, and the model sees at most 2,000 characters of its output.
The interpreter keeps its variables between actions, as a Jupyter notebook does, and like a notebook cell it shows the value of a last line that is a bare expression as well as what the code prints. That matters here: the CodeAct paper's agents run in a Jupyter kernel, and 7 of the model's 8 first code actions end with a bare expression such as count rather than a print. A traceback comes back in full, with the tools' own file under a placeholder name, so the model can read which line failed and why.
Two ways to write the same action
In code mode the system prompt lists the functions as Python signatures with a line of description each. In JSON mode the template writes them as JSON schemas inside a pair of tools tags, so every JSON prompt is 152 tokens longer than the code prompt for the same task, about 380 against 230. Each JSON call names one function with literal arguments, and its return value comes back as a tool message. A reply without code, or without a call, ends the run, and its text after Answer:, or the whole reply if it has none, is the answer.
The next token shows how the model chose between acting and answering. With code, the first token of the first reply was the opening code fence, with probability at least 0.97, on seven tasks. With JSON, the opening tool-call tag came first on only two tasks; on the other six the model began with prose, “To” or “The”, at 0.96 or more.
What happened
Code actions solved 7 of the 8 tasks and JSON calls 5, as Every task, both ways shows. Six of the seven code successes took one action and an answer. On the leap-year task the first action counted the years for which is_prime was true and showed 0; the model read that, wrote that the code used the wrong function, applied the leap-year rule itself and got 2. On three tasks it wrote its own is_prime instead of using the given one. The failures:
- Code, Friday the 13th. The first action built dates such as
2026-1-13without the leading zero, andweekdayraised a ValueError on the invalid date. The model wrote that it needed the format YYYY-MM-DD, sent the same expression again, and from the third reply on repeated one reply word for word until the 8 replies ran out. After the first traceback, the colon that starts a format such as:02dwas its second choice at that token, with probability 0.000014, and by the last reply 0.0000002: each repetition made the next one more likely. - JSON, primes between 1,000 and 1,100. One call checks one number, so the model wrote 18 calls, for 1,001 to 1,018, until it reached the 512-token limit, got 2 primes back and answered 3. The reference is 16.
- JSON, the digits of 2 to the power of 100. It said it would compute the power, then called
is_primeon 2 and on 100, in three replies running, and answered 127 instead of 115. - JSON, Friday the 13th. It described checking each month with
weekdayand ended without a call, so the plan became the answer.
Three JSON successes made no call: the model knew the 100th prime and the first prime after 10,000, and worked through the leap-year rules in text.
What it cost
Over all 8 tasks adds up the cost. Code took 23 replies and 16 actions, JSON 14 replies and 26 calls, 18 of them in one reply. The template re-renders the whole conversation every turn, so the tokens read add up every prompt: 14,663 for code and 6,883 for JSON. Most of the code total is the one failed task, whose 8 replies read 10,229 tokens; the other seven tasks read 4,434. A loop that repeats a mistake pays for the whole conversation again on every reply.
Where it came from
Program-aided language models (PAL, Gao et al., 2023) and Program of Thoughts (Chen et al., 2023) had a model write a program for a word problem and let Python compute the answer. Self-Debugging (Chen et al., 2024) fed execution results and errors back so that the model could repair its code. CodeAct makes code the action of a multi-turn agent like ReAct's; on its M3ToolEval benchmark, code actions reached up to 20% higher success than JSON or text actions and needed fewer of them. OpenHands builds its software agents on the same idea.
Papers
- Executable Code Actions Elicit Better LLM Agents The method: Python code as the agent's action, run in a Jupyter kernel, compared with JSON and text actions on the same tools.
- Qwen3 Technical Report The model recorded here, with its thinking mode switched off, whose chat template writes the JSON tool calls.
- ReAct: Synergizing Reasoning and Acting in Language Models The loop of actions and observations that CodeAct keeps, with text actions instead of code.
- PAL: Program-aided Language Models A model writes a Python program for a word problem and the interpreter computes the answer.
- Program of Thoughts Prompting: Disentangling Computation from Reasoning for Numerical Reasoning Tasks The same split: the model reasons in code and Python does the arithmetic.
- Teaching Large Language Models to Self-Debug Execution results and error messages fed back so that the model repairs its own code.
- OpenHands: An Open Platform for AI Software Developers as Generalist Agents Software agents whose actions are code run in a sandbox, built on CodeAct.