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
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).
Every intermediate result must pass through the model.
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.
With code execution environments becoming more common for agents, a solution is to present MCP servers as code APIs rather than direct tool calls.
Multiple research papers have shown that having the LLMs actions written as code snippets is a more natural and flexible way of writing them.
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.
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.
Sources
- Executable Code Actions Elicit Better LLM AgentsWang et al.
- Code execution with MCP: Building more efficient agentsAnthropic, 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.
- Code Mode: the better way to use MCPKenton 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.
- What are agents?Hugging Face
Added Nov 14, 2026