Code Mode: Why AI Agents Write Code, Not Tool Calls

Diagram of an agent writing a program in a sandbox, illustrating Code Mode

Code Mode replaces the usual loop of agent tool calls with a single step: the model writes a short program against an API, the program runs in a sandbox, and only the values it returns go back to the model. Cloudflare introduced the approach for MCP servers, and Anthropic has shipped a related feature. The savings are real, but they depend heavily on the task.

What the classic tool loop costs

In a standard setup, the model calls one tool at a time. Costs pile up in three places. Every turn carries the tool descriptions, every reply returns a full payload even if one field would do, and calls that depend on each other multiply into many exchanges.

GitHub’s official MCP server (version 2.2.0) gives a sense of scale. Its default set holds 46 tools, and listing them with descriptions and schemas costs roughly 11,000 tokens under the o200k_base tokenizer. That’s about 242 per tool, with issue_write the largest at 803. Issues pulled from the cloudflare/agents repository varied between 384 and 2,572 tokens, averaging 889.

How Code Mode works

Cloudflare’s Kenton Varda and Sunil Pai laid out the idea on September 26, 2025. Their reasoning: models have read a great deal of code and very little tool-call output. On the client side, the MCP server’s tool schemas get turned into a commented TypeScript API, and the model is handed a single tool that accepts code as text. The code then executes in a V8 isolate, using Cloudflare’s Worker Loader API.

In the @cloudflare/codemode package, submitted code is first cleaned up: markdown fences are removed, and arrow functions, named functions or bare statements all work. Console output goes into an array. If a run fails, the model gets the error plus the logged lines, which helps it debug. The sandbox is an interface, so developers can swap in their own.

In one run, a program asked to find unassigned open issues among 20 made 20 host calls in 6 ms and sent back just two issues.

Large APIs need a second design. Instead of loading a whole spec, the model gets two tools instead: search() filters an OpenAPI spec with model-written JavaScript, and execute() calls the API. Cloudflare’s own MCP server uses this pattern.

Anthropic’s version

Anthropic’s Adam Jones and Conor Kelly followed on November 4, 2025 with a close relative. There, each MCP tool is a TypeScript file in a folder tree (./servers/google-drive/getDocument.ts, for example), and the agent opens only what a job requires.

The Claude API also offers programmatic tool calling, built on the code execution tool (version code_execution_20260120 or later) and using Python. A tool opts in with allowed_callers. The limits are specific:

  • Tools from the MCP connector are excluded, as are tools with strict: true.
  • A Python cell may run for 90 seconds, and a waiting tool call expires after roughly 4 minutes.
  • The container is offline, with 5 GiB of memory and one CPU.

Anthropic’s own results show where it helps. A 75-tool project-management benchmark saw billed input tokens drop about 38% at the same accuracy. On tau2-bench, where turns make only one or two sequential calls, accuracy was unchanged and cost went up about 8%.

Where it pays off

Picture a job that reads N issues. Looping means each turn re-reads the definitions, the prompt and all earlier calls and results, so cost climbs with N squared. Under Code Mode the job takes two turns, writing the program and reading its answer, so cost stays flat: the intermediate data never reaches the model.

Prompt caching narrows the gap, since cached rereads cost a tenth of the base input price on the Claude API. With only a few calls, Code Mode shows little benefit.

What to watch next

Teams choosing between the two should count how many calls their typical task chains. Long chains over big payloads favor Code Mode. Short, one-or-two-call turns don’t. Other agent softwarees are likely to add their own variants.

Source: SayBackend

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.