Code Mode vs MCP Tool Calls: Cost and Trade-offs
Code Mode vs MCP tool calls is a comparison of two ways for a model to use the same tools. With direct tool calls, the model picks a tool, the client runs it, and the result goes back through the model before the next call. With Code Mode, the model writes a short program that calls the tools as functions, a sandbox runs it, and only the final result returns. Cloudflare named the pattern, Anthropic has written about the same idea, and Forkit's MCP server is built on it. Here is what each source says and when each approach is the better choice.
How direct MCP tool calls work
An MCP server publishes a list of tools. Each has a name, a description and a schema. The client puts those definitions into the model's context. When the model wants to act, it emits a structured call, the client executes it, and the output is appended to the conversation.
Two costs follow:
- Definitions cost tokens before any work is done. Every tool the agent might use is described in the context, whether or not this task needs it. Anthropic's post on the topic has a section headed "Excessive token consumption from tools makes agents less efficient".
- Intermediate results pass through the model. Cloudflare puts it sharply: when an agent strings several calls together, the output of each call has to feed into the model just to be copied over to the inputs of the next call, wasting time, energy and tokens.
For one or two calls, neither cost matters. For a workflow that lists fifty tasks, filters them and updates ten, both do.
How Code Mode works
Cloudflare's "Code Mode" post proposes converting MCP tools into a TypeScript API and asking the model to write code against it. Its claim is that models are better at writing code to call MCP than at calling MCP directly, perhaps because they have seen far more real code than examples of tool calls. The generated code runs in an isolate with no access beyond the API it was given.
A follow-up post applied this to Cloudflare's own API, which has over 2,500 endpoints. Instead of one MCP tool per endpoint, the server exposes a search tool and an execute tool, and the post's title states the result: an entire API in 1,000 tokens.
Anthropic's "Code execution with MCP" post reaches the same place from the client side: let the agent write code that calls MCP servers, load only the definitions it needs, and keep intermediate data out of the context.
Code Mode vs MCP tool calls compared
| Direct tool calls | Code Mode | |
|---|---|---|
| What the model produces | One structured call at a time | A script that makes many calls |
| Tool definitions in context | All of them, up front | A small fixed surface; details on demand |
| Intermediate results | Pass through the model | Stay in the sandbox |
| Round trips for a multi-step task | One per step | Usually one |
| Token cost as tools are added | Grows with the tool list | Stays roughly fixed |
| Needs a sandbox | No | Yes |
| Per-call approval by a person | Natural; each call is visible | Harder; one script does many things |
| Audit | Each call is a record | The script and its result are the record |
| Failure mode | Slow and expensive | A script error part-way through |
When direct tool calls are the better choice
- Few tools, few steps. A server with five tools gains little from Code Mode and takes on the weight of a sandbox.
- Each action needs its own approval. If a person must approve every write, separate calls are easier to show, allow and deny. A script that performs ten writes is one decision.
- The client cannot run code safely. Code Mode depends on real isolation. Without it, do not run model-written code.
- Results must be seen by the model at each step. If the next action depends on judgment about the previous result, not on a filter you could write in code, the model has to read it anyway.
When Code Mode is the better choice
- Large APIs. Hundreds of operations cannot all be described in the context.
- Bulk work. "Find every overdue task in this project and bump its priority" is a loop. A loop belongs in code.
- Many agents. When a thousand agents each make the same kind of request, per-request token cost multiplies.
- Composition. Filtering, joining and counting are things code does exactly and models do approximately.
How Forkit uses Code Mode
Forkit calls its design "execute_code-first". Its MCP server lists nine tools. The everyday database operations (listing, creating and updating tasks, dependencies, projects, comments, labels, threads, messages, file claims) are not separate MCP tools. They are functions on a codemode object inside execute_code.
A call looks like this:
const ready = await codemode.ready_tasks();
const mine = ready.filter(t => t.priority === 'urgent').slice(0, 3);
for (const t of mine) {
await codemode.claim_task({ task_id: t.id, agent_id: 'worker-7' });
}
return mine.map(t => t.id);
One round trip. The model never sees the full list of ready tasks, only the three ids.
Details from Forkit's documentation:
- Sandboxed code runs in a V8 isolate through Cloudflare's Worker Loader, with network access disabled. The
codemodefunctions are the only way out, and they are always scoped to the caller's workspace. - Discovery.
search_apireturns the TypeScript type definitions on demand, about a thousand tokens, so they are not loaded on every request. - Limits. Code is capped at 50 KB, output at 500 KB, run time at 30 seconds, and a workspace may run 20 executions per 60 seconds.
- Audit. Every execution, success or error, is stored with its code, result and duration, and
list_executionsreads the log.
A few operations stay as direct tools on purpose: ready_tasks and claim_task because agents need them constantly and atomically, and the wait_for_* tools because they are long-polls. Starting a fan-out is HTTP only, because it returns keys and execution results are kept in the log.
The approval trade-off, concretely
The table's "per-call approval" row deserves a second look, because it is the real cost of Code Mode. Forkit handles it by keeping two things apart. execute_code operates on Forkit's own data: tasks, threads, messages, claims. It cannot touch your repository or your shell. The actions that need a person, such as shell commands on the agent's machine, go through a different path entirely: the agent's permission prompt, held by a hook. So the token cost of bookkeeping is collapsed into one call, while the decisions about risk stay one at a time.
If you design your own Code Mode server, draw the same line. Put reads and reversible bookkeeping behind the script. Keep irreversible actions as individual, approvable calls.
Related: Code Mode MCP explained has a longer worked example, what is an MCP server covers the basics, and agent sandbox vs approvals covers the isolation side.
Where Forkit fits
Forkit is a working example of Code Mode in a multi-tenant product: nine tools, one sandbox, an audit log. If you want to see the pattern from the client side, connect an MCP client with a guest identifier and call search_api. It is free to try. More at forkit-mcp.com.
Sources
Read on 16 Nov 2026: