Skip to main content
Back to Blog

Cloudflare Code Mode: Why AI Agents Should Write Code Instead of Calling Tools

TL;DR

  • MCP lets AI agents use tools. But every tool's description eats space in the AI's memory (its "context window").
  • Cloudflare's Code Mode flips the approach. Instead of calling tools one by one, the AI writes a small program that calls them.
  • The result: Cloudflare's whole API — 2,500+ endpoints — fits in about 1,000 tokens, down from over 1.17 million. That's a ~99.9% cut.
  • The code runs in a locked sandbox. No file system, no secrets, and outbound calls only where allowed.
  • Why you should care: cheaper, faster, more reliable agents — and a clear pattern for connecting AI to your own systems.

First, what is MCP?

MCP (Model Context Protocol) is a standard way to plug tools into an AI agent.

  • A tool might be "create a DNS record," "find an invoice," or "send a Slack message."
  • An MCP server lists its tools. The AI reads the list and calls the ones it needs.

It works well for a handful of tools. It gets painful with hundreds.

The problem: tools are expensive

Every tool comes with a name, a description, and a list of inputs. The AI has to read all of it before it does anything.

  • More tools = more tokens. Tokens cost money and slow responses.
  • More tools = more confusion. Models pick the wrong tool more often when the list is long.
  • Multi-step work is clunky. Each tool call is a round trip. Results flow back through the AI, even when it only needs to pass them to the next step.

Cloudflare's API has over 2,500 endpoints. Listing them all as tools would take more than 1.17 million tokens. That's bigger than most models can even hold.

The Code Mode idea

Cloudflare's insight is simple: AI models are better at writing code than at calling tools.

They've seen millions of real programs during training. They've seen very few examples of tool-calling formats.

So Code Mode turns tools into a typed code API (TypeScript). Then it asks the model to write a short script against it.

The Code Mode MCP server exposes just two tools:

  • `search()` — the AI writes code to look through the API spec and find what it needs. The full spec never enters the AI's context.
  • `execute()` — the AI writes code that calls the real API. One script can loop, paginate, check conditions, and chain several calls.

Two tools. About 1,000 tokens. And that number stays flat no matter how big the API gets.

A simple example

Say you ask an agent: "Block this IP on all 12 of my sites."

Classic tool calling:

  • Call "list zones."
  • Read 12 results.
  • Call "create firewall rule" 12 times, one round trip each.

Code Mode:

  • The AI writes one short loop.
  • The loop runs in the sandbox and makes all the calls.
  • Only the final summary comes back.

Fewer tokens. Fewer round trips. Fewer chances to lose track halfway.

In one public demo, this approach used about a third fewer tokens on a simple task and about 81% fewer on a 31-step batch task.

Is it safe to let AI run code?

This is the right question. Cloudflare's answer is a tight sandbox.

  • The code runs in an isolated V8 isolate (a Dynamic Worker), spun up on demand.
  • No file system access.
  • No environment variables. API keys stay outside the sandbox, in the host. The AI's code can call the API without ever seeing the secret.
  • Outbound network calls are blocked by default and only go through handlers you allow.

The open-source Code Mode SDK (`@cloudflare/codemode`, part of Cloudflare's Agents SDK) also supports pausing a run for human approval and keeping an audit trail.

It's not just for Cloudflare's API

Two things make this bigger than one product:

  • Cloudflare open-sourced the SDK. You can use the same pattern for your own MCP servers.
  • MCP server portals support Code Mode. A portal puts many MCP servers behind one gateway. With Code Mode on, the agent sees a single code tool instead of every tool from every server. Add the tenth or the hundredth server, and the token cost barely moves.

Other AI companies have been exploring the same "code execution with MCP" pattern. The industry is landing on the same answer.

What this means for your business

If you're building AI agents on top of your systems, Code Mode is a pattern worth knowing.

  • Big internal APIs become usable. An ERP, a CRM, or a FileMaker Data API with hundreds of actions no longer has to be squeezed into a short tool list.
  • Multi-step jobs get cheaper. "Find all unpaid invoices over 60 days and draft reminders" becomes one script, not dozens of calls.
  • Security gets clearer. Credentials live in one place. The AI writes code; your sandbox decides what that code can reach.
  • Humans stay in charge. Approval steps and audit logs fit naturally into this setup.

Where to be careful

Code Mode isn't free magic.

  • You need a real sandbox. Running AI-written code without isolation is a bad idea.
  • Small tool sets don't need it. If your agent uses five tools, classic tool calling is fine.
  • Write actions still need guardrails. Anything that touches money, customers, or production data should go through approval.
  • Debugging changes. You're now reviewing AI-written scripts, not just tool calls. Log them.

FAQ

What is Cloudflare Code Mode?

It's a way for AI agents to use tools by writing code against a typed API, instead of calling each tool directly. The code runs in a secure sandbox, and only the results go back to the AI.

How many tokens does the Code Mode MCP server use?

About 1,000 tokens for access to 2,500+ Cloudflare API endpoints. A classic tool-per-endpoint server would need over 1.17 million.

Is Code Mode only for Cloudflare?

No. The SDK is open source, and Cloudflare's MCP server portals can apply Code Mode across many upstream MCP servers.

Does the AI see my API keys?

No. Credentials stay in the host. The sandboxed code calls the API through a function that adds them, so secrets never enter the AI's context or its code.

Should every AI agent use Code Mode?

No. It shines with large APIs and multi-step tasks. For a small, simple tool set, direct tool calls are still fine.

---

Want AI agents that can safely work across your own systems? We build AI automations that connect to your apps, APIs, and databases — with guardrails and humans in the loop. Schedule a free integration audit →

Ready to discuss your project?

Contact us today for a free consultation on your AI, web, or FileMaker project.

Get in Touch