# Cloudflare Code Mode tested: fewer tokens, right answers

Canonical: https://www.kartikaydhar.com/blog/cloudflare-code-mode-mcp-tested
Published: 2026-11-23T12:29:49.802+00:00
Updated: 2026-11-20T13:38:25.071434+00:00
Category: Tool Breakdowns

A small model miscounted in all 3 normal tool-calling runs. With Cloudflare Code Mode it was right every time, on about one-seventh of the input tokens.

Code Mode is a free, open-source way from Cloudflare for an AI model to use tools by writing a short JavaScript program. The program runs in a sealed sandbox and calls the tools for the model. I ran it against normal one-at-a-time tool calling on the same counting task. With normal tool calls, a small model miscounted in all 3 runs. With Code Mode it was right all 3 times and used about one-seventh of the input tokens, the chunks of text a model reads and is billed for.

## What is Code Mode?

Most AI assistants use tools the same way. The model asks for one tool, waits for the answer, reads it, then asks for the next one. Every answer goes back into the model's context, the text it has to read before each reply, even when the model only needs one number from it.

Cloudflare's Kenton Varda and Sunil Pai proposed a different approach in September 2025. Code Mode describes each tool as a function in TypeScript, a common programming language, with the exact inputs each one takes. The model gets a single tool that runs code, and it writes a small function that calls the real tools, filters the results and returns only what it needs. Their argument is that models have read far more real code than made-up tool-call examples, so they are better at writing code.

The code runs inside a Dynamic Worker, a tiny, separate copy of the JavaScript engine that Cloudflare says starts in a few milliseconds. It is thrown away after one use and has no internet access. The global fetch() call is blocked, so the only things it can reach are the tools you hand it. API keys stay on the host side, so the generated code never sees them.

In February 2026 Cloudflare used the same idea for its own MCP server for the Cloudflare API. MCP is the common standard that lets AI assistants plug into outside tools. That server covers more than 2,500 API endpoints with just two tools, search() and execute(), and about 1,000 tokens of tool descriptions. Cloudflare says a normal MCP server with one tool per endpoint would need about 1.17 million tokens. I tested it now because Dynamic Workers, the sandbox Code Mode runs on, have been in open beta since March 2026, and version 0.5.6 of the package shipped on 30 October 2026.

## How does it work?

You install the @cloudflare/codemode package (I used version 0.5.6, MIT licensed) inside a Cloudflare Worker and give it your tools. It gives the model one tool that runs code, plus a short manual of your tools written as functions. When the model sends code, the package runs it in a sandbox and passes each tool request back to your app.

Image: Illustration: How does it work?

The package also has a runtime with approvals. If the code reaches a tool you marked as needing a human, the run pauses until someone approves it, then replays from a saved log. I did not test that part.

## What I tested

- Version and date: @cloudflare/codemode 0.5.6 with AI SDK 7 and wrangler 4.165.0, tested on 20 November 2026.
- Setup: the Code Mode sandbox ran locally with wrangler dev, which supports Dynamic Workers without a Cloudflare paid plan. The models ran on Cloudflare Workers AI. I used GLM-4.7-flash for three runs of each approach and gpt-oss-120b for one run of each. Both approaches made their model calls the same way, through Cloudflare's API MCP server, and every token count comes from Workers AI's own usage numbers, so that route doesn't tilt the result.
- Task I ran: a fake project tracker with 3 projects, 60 tasks and 6 people, and three tools: list projects, list a project's tasks, and look up a person. The question was: across all projects, which open tasks are past their due date, and how many does each person own? I worked out the correct answer with a script first: 22 overdue tasks, with 7, 5, 4, 4, 1 and 1 per person.
- Time to first result: about a minute from npm install to the first sandboxed run, once I had approved the install script for workerd, the local Workers runtime, which npm held back by default.
- What worked: Code Mode got the counts exactly right in all four runs. Each time the model wrote a loop over the projects, filtered the tasks, counted per owner and looked up only the owners it needed. The sandbox ran that code in 7 to 12 milliseconds. A test script that tried to reach the internet from inside the sandbox was blocked.
- What broke or surprised me: normal tool calling with GLM-4.7-flash got the counts wrong in all three runs. In one run it gave one person 11 overdue tasks when the right number was 7. In another it left a person out. The model had all the data in front of it and still miscounted 60 rows of JSON.
- Would I use it at work? Yes, for any assistant that chains several tool calls or reads large results. Not yet for anything customer-facing that writes data, because the package is labelled experimental and I have not tested the approval flow.

### The numbers

With GLM-4.7-flash, normal tool calling took 4 model calls per run and used 13,006 to 13,039 input tokens and 2,434 to 3,163 output tokens. Code Mode took 2 model calls and used 1,768 to 1,788 input tokens and 665 to 703 output tokens. That is about one-seventh of the input tokens and about a quarter of the output tokens.

In a single run, the bigger model showed the gap more clearly. gpt-oss-120b with normal tools called one tool per turn, so it needed 11 model calls, 49,125 input tokens and 11,558 output tokens, and spent 238 seconds waiting on the model. It did get the right answer. With Code Mode it needed 2 model calls, 1,597 input tokens, 625 output tokens and 13 seconds.

Image: Illustration: The numbers

At Workers AI list prices, a normal GLM run cost about $0.002 and a Code Mode run about $0.0004. For gpt-oss-120b it was about $0.026 against $0.001. These runs fit inside the Workers AI free allocation, so I paid nothing.

Two results cut the other way. Code Mode's first prompt was bigger: 643 tokens against 446, because the typed tool description is longer than three plain tool definitions. The savings only appear once tool results start coming back. Speed also varied a lot. Normal GLM runs took 48 to 68 seconds of model time, while Code Mode runs took 10, 19 and 58 seconds. The slow one came from a single model call that took 52 seconds, so timing depends on the model service as much as on the approach.

This was one small task with fake data. A task with one tool call and a short answer would gain little.

## Why does writing code help the model?

In normal tool calling the model is the pipe. Every task list went into its context, about 5,800 tokens by the third call, and the model then had to count by reading. Counting rows by reading is easy to get wrong, and that is where GLM made its mistakes.

In Code Mode the counting happened in code. Code counts exactly, as long as the logic is right, and in all four runs the model's logic was right. The model's job shrank to writing 20 to 40 lines of code that described the plan. Only the six finished rows entered its context.

I saw the same effect when Scrapling's MCP tool sent 19 times less text to the model.

## What does it cost to run in production?

The package is free and open source. Running the sandbox on Cloudflare needs the Workers Paid plan, which starts at $5 a month. Dynamic Workers are still in open beta. Each month includes 1,000 unique Dynamic Workers, then each one costs $0.002 per day, plus normal Workers request and CPU charges. Code Mode makes a fresh Worker for each piece of generated code, so in practice that is $0.002 per code run after the free 1,000.

The README says the Dynamic Worker executor only works on Cloudflare Workers, and the sandbox runs JavaScript only. You can write your own executor for another sandbox, but then the security work is yours.

## How do you try it?

- Install @cloudflare/codemode in a Cloudflare Worker and hand it the tools your assistant already uses.
- Run it free on your own machine with wrangler dev, Cloudflare's local test tool.
- If npm holds back the install scripts for workerd (the local Workers runtime) and esbuild, approve them with npm install-scripts approve workerd esbuild. That happened in my run.
- Point it at one assistant task that pulls big lists, then compare the token counts with your current setup.

If you want to offer Code Mode to any MCP client, Cloudflare has a guide to building a Code Mode MCP server.

## What this means for product and GTM teams

If your product has an AI assistant that calls your own API, Code Mode is worth a short engineering trial. The win grows with the size of the tool results. Picture a help center assistant that pulls 200 articles to find the five that mention a feature, or a marketing ops assistant that pulls a month of campaign results to find the three weakest pages. With Code Mode the model reads only the short list.

The accuracy result matters more to me than the token savings. A PM can't easily tell when an assistant has miscounted, because the answer looks clean and confident. Moving the arithmetic into code makes the answer checkable: you can read the function the model wrote and rerun it.

The catch is that it is experimental, the managed sandbox needs a paid Cloudflare plan, and the model now writes code that runs for real. For anything that writes data, I would want the approval flow on and tested first. I wrote about the browser side of Cloudflare's agent tools in my Kitesurf test, and about typed tool contracts for websites in the WebMCP explainer.

## Frequently asked questions

### Is Cloudflare Code Mode free?

The @cloudflare/codemode package is free and MIT licensed, and it runs locally with wrangler dev at no cost. Running it on Cloudflare needs the Workers Paid plan, plus $0.002 per Dynamic Worker per day after 1,000 a month and normal Workers usage.

### Does Code Mode work with any MCP server?

Yes. The package can wrap existing MCP tools, AI SDK tools or plain functions into one code tool.

### Is the generated code safe to run?

It runs in a fresh, separate sandbox Worker with internet access blocked by default, and it can only reach the tools you pass in. The code can still call your tools in ways you did not expect, so mark tools that write data as needing approval.

### Does it only help with big models?

Both gained. The small model went from wrong to right. The bigger one (one run) was right both ways but used about one-thirtieth of the input tokens with Code Mode, because without it, it called one tool per turn.

## Sources

- Cloudflare blog, "Code Mode: the better way to use MCP" (26 Sep 2025): https://blog.cloudflare.com/code-mode/
- Cloudflare blog, "Code Mode: give agents an entire API in 1,000 tokens" (20 Feb 2026): https://blog.cloudflare.com/code-mode-mcp/
- Cloudflare docs, Cloudflare's own MCP servers: https://developers.cloudflare.com/agents/model-context-protocol/cloudflare/servers-for-cloudflare/
- Cloudflare docs, Build a Code Mode MCP server: https://developers.cloudflare.com/agents/model-context-protocol/guides/build-codemode-mcp-server/
- Cloudflare docs, Code Mode API reference: https://developers.cloudflare.com/agents/api-reference/codemode/
- @cloudflare/codemode on npm: https://www.npmjs.com/package/@cloudflare/codemode
- Source code in the Cloudflare Agents repo: https://github.com/cloudflare/agents/tree/main/packages/codemode
- Cloudflare blog, "Sandboxing AI agents, 100x faster" (24 Mar 2026): https://blog.cloudflare.com/dynamic-workers/
- Cloudflare docs, Dynamic Workers pricing: https://developers.cloudflare.com/dynamic-workers/pricing/
- Cloudflare docs, Workers pricing: https://developers.cloudflare.com/workers/platform/pricing/
- Cloudflare API MCP server repo: https://github.com/cloudflare/mcp
