"Our agent's API costs are running three times higher than projected." When transitioning PoCs into production, this has recently become the single most common concern. The vast majority of these overruns stem from an architecture where the entire context is repeatedly sent back to the LLM on every tool invocation. Cloudflare's release of the Code Mode MCP Server in April offers a compelling solution to this structural flaw.
In this article, we break down the core concept of Code Mode MCP, how it contrasts with standard MCP servers, how to design secure sandboxed execution using Workers, and estimated API cost reductions at production scale from an engineer's perspective.
How traditional MCP consumes tokens
Under standard MCP, tool execution follows a model where 1 action equals 1 LLM round trip. For instance, handling a workflow like "check Customer A's invoice from last month and send a reminder email if unpaid" requires four round trips:
| Step | Payload | Approximate tokens |
|---|---|---|
| 1. Call customer search tool | All tool definitions + conversation history | 4,000 |
| 2. Call invoice list tool | All tool definitions + conversation history + search results | 5,500 |
| 3. Check unpaid status + generate email | All tool definitions + conversation history + invoice list | 7,000 |
| 4. Call send email tool | All tool definitions + conversation history + email body | 7,500 |
This totals 24,000 tokens. As use cases grow more intricate, the overhead of repeatedly retransmitting tool definitions compounds, adding hundreds of thousands of yen to monthly bills. This challenge directly builds upon the insights we documented in The complete guide to MCP and Design patterns for turning existing APIs into MCP servers.
The architectural shift introduced by Code Mode
Rather than having the agent call tools one by one, Code Mode MCP prompts the agent to write code for the entire workflow and execute it in a single pass.
// エージェントが生成するコード(例)
const customer = await mcp.findCustomer({ name: "A社" });
const invoices = await mcp.listInvoices({ customerId: customer.id, period: "last_month" });
const unpaid = invoices.filter(i => i.status === "unpaid");
if (unpaid.length > 0) {
await mcp.sendReminderEmail({
to: customer.email,
amount: unpaid.reduce((s, i) => s + i.amount, 0),
});
}
This code runs inside an isolated sandbox on Cloudflare Workers, returning only the final result to the LLM. Four round trips condense into one and tool definition retransmission is eliminated, completing the exact same workflow in approximately 7,000 tokens. A 70% reduction in token consumption is a realistic target.
Three boundaries that guarantee safety
Having AI write and execute code often triggers immediate security concerns. When deploying this in production, security is enforced across three distinct boundaries:
- Sandbox isolation: Execution runs within Cloudflare Workers isolates, restricting filesystem and network access strictly through MCP
- Tool ACLs: Whitelisting the tools accessible to each agent, strictly separating read-only and write permissions
- HITL (human approval): Operations involving financial transactions or external messaging require human visual inspection of generated code prior to execution
The HITL design discussed in our guide on integrating Anthropic Computer Use into business workflows is equally vital for Code Mode. Placing a single checkpoint between code generation and execution reliably keeps operational risks under control.
Cost estimates: an agent processing 100,000 monthly requests
Here is a comparison between traditional MCP and Code Mode for an agent handling 100,000 sessions per month.
| Item | Traditional MCP | Code Mode MCP |
|---|---|---|
| Tokens per session | 24,000 | 7,000 |
| Monthly tokens | 2.4 billion | 700 million |
| Claude Sonnet 4.6 equivalent ($3 input / $15 output, 70% input) | Approx. 1,300,000 JPY/month | Approx. 380,000 JPY/month |
| Cloudflare Workers execution cost | - | Approx. 30,000 JPY/month |
| Total | 1,300,000 JPY | 410,000 JPY |
The difference is 890,000 JPY per month, or 10.68 million JPY annually. Choosing whether to adopt Code Mode during the PoC phase directly impacts your break-even point in production.
Checklist for adoption decisions
If you are evaluating whether to adopt Code Mode, consider the following four criteria:
- The session averages three or more tool calls (if fewer, standard MCP is sufficient)
- Monthly volume exceeds 10,000 sessions (at this scale, token savings justify implementation effort)
- Existing MCP tools are stable (they can be easily wrapped and reused)
- The engineering team is proficient in reading TypeScript/JavaScript (required for reviewing generated code)
If your system meets all four criteria, transitioning to Code Mode offers a high probability of strong ROI. Even if your current volume is below these thresholds, the eight token-saving techniques covered in Claude Code operating cost optimization (such as compacting CLAUDE.md, optimizing /clear, and scoping context reads) can be applied immediately.
Summary — code execution will become standard for production agents
While original MCP specifications targeted an AI that calls discrete tools, Code Mode redesigns the interaction layer around an AI that writes executable code. A 70% reduction in token usage often represents the difference between a viable production deployment and an abandoned PoC.
The scope of migrating to Code Mode depends on round-trip counts, session volumes, and tool maturity in your existing agents. Because the implementation steps and timelines differ significantly depending on whether you simply need to rewire plumbing or re-architect HITL boundaries, evaluate your metrics against the checklist above and tell us about your setup via our inquiry form. We will help you identify the highest-impact architectural changes for your current configuration.








