---
title: "Code actions pattern for AI agents | Agentic Pattern Language"
description: "The agent writes code that calls its tools, and an interpreter runs it and returns the result. Sourced from Wang et al., Anthropic, and Cloudflare."
url: https://agenticpatternlanguage.com/patterns/code-actions/
---

22 [One agent](https://agenticpatternlanguage.com/#one-agent)

# Code actions

The agent writes code that calls its tools, and an interpreter runs it and returns the result.

Also called CodeAct, Code Mode, Code execution with MCP

Wang et al., Anthropic, Cloudflare, Hugging Face

[How to read this](https://agenticpatternlanguage.com/notation/)

## Part of

* [Agent loop](https://agenticpatternlanguage.com/patterns/agent-loop/)

## Builds on

* [Sandboxing](https://agenticpatternlanguage.com/patterns/sandboxing/)

## Alternatives

* [Tool search](https://agenticpatternlanguage.com/patterns/tool-search/)

## The problem

Tool calls written as JSON cannot be combined, and every intermediate result passes through the model.

> 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).

Wang et al. [Executable Code Actions Elicit Better LLM Agents](https://arxiv.org/abs/2402.01030)

> Every intermediate result must pass through the model.

Anthropic [Code execution with MCP: Building more efficient agents](https://www.anthropic.com/engineering/code-execution-with-mcp) Nov 4, 2025

## Therefore

> 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.

Wang et al. [Executable Code Actions Elicit Better LLM Agents](https://arxiv.org/abs/2402.01030)

> With code execution environments becoming more common for agents, a solution is to present MCP servers as code APIs rather than direct tool calls.

Anthropic [Code execution with MCP: Building more efficient agents](https://www.anthropic.com/engineering/code-execution-with-mcp) Nov 4, 2025

> Multiple research papers have shown that having the LLMs actions written as code snippets is a more natural and flexible way of writing them.

Hugging Face [What are agents?](https://huggingface.co/docs/smolagents/conceptual_guides/intro_agents)

## In use

Cloudflare Agents SDK

> Instead of being presented with all the tools of all the connected MCP servers, our agent is presented with just one tool, which simply executes some TypeScript code.

Cloudflare [Code Mode: the better way to use MCP](https://blog.cloudflare.com/code-mode/) Sep 26, 2025

## Where it breaks

Running model-written code needs a sandbox, resource limits and monitoring that direct tool calls avoid.

> Running agent-generated code requires a secure execution environment with appropriate sandboxing, resource limits, and monitoring. These infrastructure requirements add operational overhead and security considerations that direct tool calls avoid.

Anthropic [Code execution with MCP: Building more efficient agents](https://www.anthropic.com/engineering/code-execution-with-mcp) Nov 4, 2025

## Sources

1. [Executable Code Actions Elicit Better LLM Agents](https://arxiv.org/abs/2402.01030) Wang et al.
2. [Code execution with MCP: Building more efficient agents](https://www.anthropic.com/engineering/code-execution-with-mcp) Anthropic, Nov 4, 2025
   > Code execution with MCP enables agents to use context more efficiently by loading tools on demand, filtering data before it reaches the model, and executing complex logic in a single step.
3. [Code Mode: the better way to use MCP](https://blog.cloudflare.com/code-mode/) Kenton Varda and Sunil Pai, Cloudflare, Sep 26, 2025
   > When you connect to an MCP server in "code mode", the Agents SDK will fetch the MCP server's schema, and then convert it into a TypeScript API, complete with doc comments based on the schema.
4. [What are agents?](https://huggingface.co/docs/smolagents/conceptual_guides/intro_agents) Hugging Face

Added Nov 14, 2026
