Code Mode: AI agents work better writing code than calling tools
On this page
In September 2025, Kenton Varda and Sunil Pai published a post on the Cloudflare blog with a provocative claim: we have been using MCP the wrong way. Their proposal, called Code Mode, is easy to state. Instead of asking the model to call tools one at a time, you give it a TypeScript API and ask it to write a program that uses that API. The program runs in an isolated environment, and only the final result comes back into the conversation.
In this post we explain the idea in our own words, show why it makes sense in practice, and look at what it changes for anyone who, like us, builds AI agents for clients. The original article is listed in the sources at the end of the page and is well worth reading in full.
#What MCP is, in one sentence
The Model Context Protocol is an open standard for connecting language models to external systems: a database, a CRM, a calendar, a code repository. Each system exposes an MCP server with a list of tools, and each tool has a name, a description and the shape of its parameters.
What MCP brings is uniformity. Once an agent speaks MCP, it can use any server that follows the protocol, with the same way of discovering tools, authenticating and reading the documentation.
#How a tool call works today
In the traditional approach, the model receives the list of tools in its context. When it decides to use one, it generates a special snippet with the tool name and the arguments. The application intercepts that snippet, runs the call and hands the result back to the model, which reads all of it and decides on the next step.
If a task needs five chained calls, that means five round trips. And every intermediate result passes through the model's context, even when the model only needs one number buried in a huge response.
#Where this approach gets stuck
Cloudflare's main argument is about training. The special tokens used for tool calls barely appear in real-world text, so models learn them from synthetic training data created specifically for that purpose. TypeScript code that calls APIs, on the other hand, shows up by the millions in public repositories.
In practice this leads to three problems that any team that has shipped an agent to production will recognize:
- Too many tools cause confusion. With dozens of tools in context, the model picks the wrong one or makes up parameters.
- Chaining is expensive. Every intermediate step spends input and output tokens and adds latency.
- The context fills up with noise. A query that returns 2,000 rows takes over the whole context window, even when the agent only needed to add up a single column.
#How Code Mode solves it
The idea has two parts.
#1. Turn MCP tools into a TypeScript API
Each tool's schema becomes a typed function, with the tool description as a doc comment. The model now sees something like this:
/** Finds customers by name or email. Returns at most 50 results. */
declare function findCustomers(input: { query: string }): Promise<Customer[]>;
/** Lists a customer's invoices within a date range. */
declare function listInvoices(input: {
customerId: string;
from: string; // YYYY-MM-DD
to: string;
}): Promise<Invoice[]>;Instead of dozens of tools, the model gets a single tool: "run this code".
#2. Run the model's code in an isolated box
The model writes a short program that does the whole job at once:
const customers = await findCustomers({ query: "Fazenda Boa Vista" });
const invoices = await listInvoices({
customerId: customers[0].id,
from: "2026-10-01",
to: "2026-10-31",
});
const unpaid = invoices.filter((invoice) => invoice.status === "open");
return {
count: unpaid.length,
total: unpaid.reduce((sum, invoice) => sum + invoice.amount, 0),
};Both queries, the filter and the sum all happen inside the box. The model only gets back { count, total }. The hundreds of invoices never pass through its context.
#Why the isolated box is the key point
Running code written by a model calls for caution. Cloudflare's answer was to use V8 isolates, the same foundation that Cloudflare Workers run on. According to the article, an isolate starts in a few milliseconds and uses a few megabytes of memory. That makes it practical to create a fresh isolate for every piece of code and throw it away right after, with no environment reused between runs.
To make this possible they launched an API, called Worker Loader in the article, that loads code on demand inside an isolate. Two security details stand out:
- No internet by default. The agent's code can have outside access blocked entirely. It can only reach what you have explicitly connected.
- No exposed API keys. Access to the MCP servers reaches the code as a connection that is already authorized (what Cloudflare calls a binding). The program uses the connection but never sees the token. If the model is tricked by malicious text, there is no secret to leak.
#The idea has spread
In November 2025, Anthropic published an engineering post along the same lines that cites Cloudflare's work. In their example, presenting tools as code files that the agent reads only when it needs them cut token usage from 150,000 to 2,000, a saving of 98.7%. The numbers vary from case to case, but the direction is the same: letting the model write code tends to be cheaper and more reliable than having it call tools one at a time.
#What this changes for people building agents
At MEYJE we build agents that talk to real systems, such as the RuralZap financial assistant and IESGO's career guidance agent. Three lessons from Code Mode are already part of how we design them:
- Well-typed tools are worth their weight in gold. A clear name, a short description and strongly typed parameters help the model, whether it is calling the tool or writing code.
- Aggregate close to the data. If the final answer is a total, the sum should happen before the data goes back to the model. Less context means lower cost and fewer mistakes.
- Isolation is not optional. AI-generated code runs without network access, without secrets and with a time limit. Credentials stay outside.
Code Mode doesn't make MCP obsolete. It keeps the best part of the protocol, which is standardization, and only changes how the model consumes it. If you build agents, it is worth a test: pick a task that currently needs several chained calls and compare tokens, time and success rate in both modes.
If you want to put an AI agent to work with your company's systems, talk to us.