What Is Code Mode and Why Companies Are Moving Away From Plain MCP Tool Calling
Code Mode means giving an AI agent a small code interface instead of a long menu of tools, and letting it write a short script to get the job done. Teams are adopting it because the usual way of using MCP burns through context, money and patience.
Where plain MCP starts to hurt
MCP did its job well. It gave AI apps one standard way to talk to tools, so nobody has to build a custom connector for every pairing. The pain shows up when you use it at scale, and it shows up in two places.
First, the menu. Most MCP setups load the description of every tool into the model's context before it has done anything useful. Connect a handful of servers and that menu gets huge. Anthropic's own engineers point out that with thousands of tools, an agent can have to read hundreds of thousands of tokens before it even looks at your request. One number that kept getting passed around this year: three servers eating 143,000 of a 200,000-token window, about 72%, before the first question was answered. (That figure comes from secondhand reports about Perplexity, so treat it as a reported example, not a lab result.)
Second, the middleman problem. Every result from every tool call travels back through the model. Anthropic gives a simple example: "take my meeting transcript from Google Drive and attach it to the Salesforce lead." With normal tool calling, the whole transcript goes into the model, and then the model has to write it all out again into the second call. For a two-hour sales meeting, that's roughly 50,000 extra tokens. The model is basically working as a very expensive photocopier, and the more it copies, the more likely it slips.
So what is Code Mode?
Code Mode flips the way the agent works with tools. Instead of showing the model a list of tools and asking it to pick one at a time, you show it a code interface (usually TypeScript) built from those same MCP tools. The model writes a short script, a sandbox runs it, and only the final answer goes back to the model.
The Google Drive example from above shrinks to a few lines: fetch the document, pass it to the Salesforce update, done. The transcript never enters the model's context at all. Same idea for a 10,000-row spreadsheet: the script filters for pending orders and hands the model five rows, not ten thousand.
Why would this work better? Models have read a mountain of real code, so writing a few lines against an API is familiar ground. Tool calling is a format they were taught more narrowly. Cloudflare's team compares it to making Shakespeare take a crash course in Mandarin and then asking him to write a play in it. He could do it, but it wouldn't be his best work.
There's also a very practical win: loops and conditions. In a Cloudflare demo, an agent had to create 31 weekly calendar events. The tool-calling agent made 30-plus separate calls. The Code Mode agent wrote one loop. And on a simple "create an event tonight" task, the tool-calling agent even put the event a year in the past, because it had no way to check today's date. The code version just asked the runtime for it.
Cloudflare's version: a whole API in two tools
Cloudflare took this furthest. Its API has more than 2,500 endpoints, and turning each one into an MCP tool would cost roughly 1.17 million tokens, more than the context window of even the most advanced models. Their MCP server instead exposes just two tools, search() and execute(), which together cost about 1,000 tokens.
The agent uses search() to write a bit of code that filters the API spec down to the few endpoints it needs, so the spec never lands in the model's context. Then it uses execute() to write code that calls those endpoints, handles pagination and chains calls together. The size of the menu stays fixed, no matter how big the API grows.
What the numbers say
The savings are big, but they depend heavily on the task. Here's what's been reported so far:
| Source | What was tested | Reported result |
|---|---|---|
| Cloudflare MCP server (Feb 2026) | Whole Cloudflare API, 2,500+ endpoints | About 1,000 tokens vs 1.17 million, 99.9% fewer input tokens |
| Anthropic, "Code execution with MCP" (Nov 2025) | One example workflow with many tools | 150,000 tokens down to 2,000, a 98.7% saving |
| WorkOS write-up of a Cloudflare demo (Dec 2025) | One calendar event vs 31 events | 32% fewer tokens vs 81% fewer tokens |
Read those as vendors showing their best cases. The pattern is still clear: the more tools you connect and the more steps a job has, the more Code Mode saves. One simple task saved about a third; the 31-event batch saved four-fifths.
It isn't free either. Code Mode makes the model write code, so output tokens go up. One independent comparison found about 2.2 times more output tokens, and still landed roughly 77% lower overall, though that was an estimate, not a benchmark you can bank on.
Is MCP dead? Not quite
If you've seen the "MCP is dead" posts, here's what actually happened. On March 11, 2026, Perplexity's CTO, Denis Yarats, said at the company's Ask 2026 conference that Perplexity was moving away from MCP internally. The reasons reported were context overhead and awkward authentication, and the company leaned on its own REST API and command-line tools instead. The internet did what it does, and "MCP is dead" trended for a few days.
But look at what the other big names did. Cloudflare kept MCP and swapped the way agents call it: MCP still handles discovery and access, while generated code does the work. Anthropic's post describes the same pattern, with agents writing code against MCP servers, and Cloudflare notes that Goose and Claude's Programmatic Tool Calling use the approach too. Anthropic also handed MCP to the Linux Foundation's Agentic AI Foundation in December 2025. And Pi, a minimalist coding agent whose creator spent a year mocking MCP, made it a core feature in version 0.99.0 on September 29, 2026.
So the honest summary is this. Companies aren't walking away from MCP as a standard. They're walking away from the default way of using it: dump every tool into context and push every result through the model. MCP stays as the plug. Code Mode changes how the agent uses it. Perplexity's move is a fair example of the other branch, where a team with a narrow, high-volume use case decides plain APIs are simpler than any protocol.
The catch
Code Mode isn't a free upgrade. Three things to weigh before you copy it.
- You need a safe place to run the code. Anthropic is upfront that agent-written code needs sandboxing, resource limits and monitoring, and that this adds operational work direct tool calls avoid. Cloudflare runs its code in a lightweight V8 sandbox with no file system, no environment variables to leak, and outside network calls switched off by default. If you build your own, that part is on you.
- It's harder to review. A single tool call is easy to read and approve. A script can bundle a dozen calls. Security folks suggest treating tool calls and generated code as two separate modes with their own controls, and some gateways keep write operations behind an approval step by default.
- Small setups don't need it. One gateway vendor's guidance is that plain tool calling is still fine for narrow agents with under ten tools, or where a human approves each call. Code Mode starts paying off with three or more servers, or jobs that chain four or more calls.
How to decide
Start by measuring, not guessing. Check how many tokens your tool definitions use before the user has typed a word. If it's a few percent of the window and your agent makes one or two calls per task, plain MCP is fine and you can stop here.
If definitions take a big bite, or your agent keeps looping through long chains of calls, try a hybrid. Keep direct tool calls for simple, one-step jobs where you want a clear approval trail. Move the heavy, multi-step work, like batch updates, data filtering and anything touching big result sets, into sandboxed code.
That hybrid is also where the Cloudflare demo pointed, with agents choosing between direct calls and generated code depending on the job. The question for most teams isn't "MCP or Code Mode." It's which jobs deserve a script.
Join the conversation
What do you think?
Write a comment
No comments yet. Be the first to share a thought.