All posts
Article6 min read

Code Mode for MCP: 99.9% Fewer Tokens

Code Mode for MCP: 99.9% Fewer Tokens
Ghita El Haitmy
Ghita El Haitmy
Software Engineer @ Eli · Oct 27, 2026

What is MCP?

MCP stands for Model Context Protocol. It is a standard way for AI applications to connect to external systems such as databases, SaaS products, developer tools and internal APIs.

An MCP server exposes operations as tools. A tool includes a name, a description and an input schema that tells the model how to call it. This works well when an agent has access to a small number of simple tools. The problem appears when that tool catalogue grows from a handful of actions to hundreds or thousands.

The old way of using MCP

The old way of using MCP is to give the model every available tool definition upfront. Before the agent answers a question or completes a task, the client places the names, descriptions and schemas for those tools into the model’s context.

The model then chooses a tool, receives its result and decides what to do next. If the workflow requires five dependent calls, the model may need to reason between each call. Large intermediate results can also enter the context even when the final answer only needs one field.

This creates two forms of token overhead:

  • Tool-definition overhead: the model receives instructions for operations it may never call.
  • Intermediate-result overhead: data moves through the model between steps instead of being filtered outside the context window.

The model has less room left for the user’s request, relevant company context and the reasoning required to finish the job. Teams also pay that overhead repeatedly across agent runs.

Now, with Cloudflare Code Mode

Now, with Code Mode, the model does not need the complete catalogue of Cloudflare API operations upfront. Cloudflare’s server-side approach exposes a compact interface centred on search() and execute().

The agent writes JavaScript to search the OpenAPI specification for the relevant endpoint. It then writes another small program to call that endpoint, combine operations if necessary, filter the response and return only the information needed for the final answer.

The full specification stays inside the sandbox rather than entering the model’s context. The generated code becomes a compact plan, while intermediate data can remain in variables instead of being copied back through the model after every operation.

So the contrast is simple: before, the AI received every tool just in case; now, it discovers the right operation on demand.

Cloudflare Code Mode token usage: before and after

Cloudflare published the following comparison for its API, which contains more than 2,500 endpoints:

  • Raw OpenAPI specification in the prompt: approximately 2,000,000 tokens, equal to 977% of a 200,000-token context window.
  • Native MCP with full schemas: 2,594 tools and 1,170,523 tokens, equal to 585% of that context window.
  • Native MCP with required parameters only: 244,047 tokens, equal to 122% of the context window.
  • Code Mode: approximately 1,100 tokens, equal to about 0.5% of the context window.

The move from 1,170,523 tokens to roughly 1,100 represents a 99.9% reduction in input tokens for tool definitions. The footprint also stays approximately fixed as the API grows because the model discovers operations progressively rather than loading every endpoint upfront.

These are Cloudflare’s measurements for the Cloudflare API, not a promise that every MCP server will achieve the same reduction. The result is unusually large because Cloudflare has an unusually large API surface. However, the benchmark makes the scaling problem visible.

Why writing code can beat direct tool calling

Code Mode works because modern language models are highly capable at writing and understanding code. They have learned from large volumes of real TypeScript and JavaScript, while direct tool-calling formats are comparatively specialised.

Code also gives an agent familiar control structures that are awkward to express as repeated model turns:

  • Loops for processing many records or checking a status repeatedly.
  • Conditionals for choosing the next action based on an earlier result.
  • Parallel calls for collecting independent data faster.
  • Filtering and aggregation before a large response reaches the model.
  • Reusable functions for workflows that repeat across many tasks.

Anthropic has reported the same general pattern in its work on code execution with MCP. In one example, loading tools on demand reduced token usage from 150,000 to 2,000, a 98.7% reduction. Its current programmatic tool-calling guidance also notes that the benefit is strongest in multi-tool workflows where intermediate results can be processed outside the model context.

What Cloudflare Code Mode does not make free

A smaller MCP footprint does not mean an AI agent runs without cost. The model still consumes tokens to understand the user’s request, write the program and produce the final response. The sandbox uses compute, external APIs may charge for requests, and the final data returned to the model still occupies context.

Code Mode removes avoidable overhead. It does not remove the need to measure the full workflow cost.

This distinction matters because token efficiency and spend governance solve different problems. An agent can use 99.9% fewer tool-definition tokens and still run too often, choose an expensive model, trigger paid actions or operate without a clear owner and budget.

Why this matters for AI agent spend

Your AI bill is your new payroll. As companies add agents, the cost of each workflow is shaped by more than a single model price. It includes input and output tokens, repeated runs, tool calls, API charges, cloud compute, connected software and any purchase the agent is authorised to make.

Code Mode can improve the first part of that equation by reducing unnecessary context and model round trips. The business still needs to answer the operational questions around it:

  • Which agent owns this workload?
  • Which team and business outcome does it support?
  • What is its monthly budget and expected unit cost?
  • Which tools, APIs and payment methods can it access?
  • What happens when usage or spend crosses a threshold?
  • Can Finance and Engineering trace the final cost back to the workflow that created it?

This is where ELI’s view of agent spend becomes important. Efficient infrastructure reduces waste; an operating layer connects that spend to an agent, owner, budget, authority and measurable purpose.

When should teams use Code Mode?

Code Mode is a strong fit when an agent connects to a large API, needs several dependent calls, processes large responses or benefits from loops, branching and filtering. It is especially useful when the available tool catalogue is much larger than the subset used in any one task.

Direct tool calls can still be the better choice for a small and stable tool set or a simple workflow that makes one call and returns one concise result. Cloudflare describes Code Mode as experimental, and Anthropic’s evaluations show that simple sequential workflows do not always become cheaper. Architecture should follow the workload, not the trend.

A practical checklist for adopting Code Mode

  • Measure the tokens consumed by tool definitions before changing the architecture.
  • Identify workflows with several calls or large intermediate results.
  • Run generated code in an isolated sandbox with outbound network access blocked by default.
  • Keep credentials outside the model context and expose only authorised interfaces.
  • Enforce approvals and policy checks inside the host or upstream tool, not inside model-generated code.
  • Log every operation so teams can connect technical activity to spend and business outcomes.
  • Compare total workflow cost, latency and accuracy before and after the change.

The right success metric is not only fewer tokens. It is a workflow that is cheaper, faster, reliable and still governable.

Frequently asked questions

What is Cloudflare Code Mode for MCP?

Cloudflare Code Mode is a pattern in which an AI model writes code to discover and call MCP operations rather than calling every exposed tool directly. The code runs in an isolated environment and returns a focused result to the model.

How many tokens does Cloudflare Code Mode save?

In Cloudflare’s full-API benchmark, native MCP with 2,594 complete tool schemas required 1,170,523 tokens. Code Mode required roughly 1,100 tokens, a 99.9% reduction in input tokens for tool definitions.

Does Code Mode replace MCP?

No. MCP still provides a standard way to connect, describe and authorise tools. Code Mode changes how an agent discovers and uses those capabilities.

Does Code Mode always reduce AI costs?

No. The largest gains appear with large tool catalogues, multi-step workflows and large intermediate results. A simple workflow with one or two calls may see little benefit and can sometimes cost more once code generation and execution are included.

Is Code Mode secure?

It can be designed securely, but code execution needs strong boundaries. Cloudflare runs generated code in isolated Workers, blocks general internet access and connects authorised services through bindings. Teams still need least-privilege access, approval checks, logging, spend limits and careful handling of untrusted data.

The future of MCP is progressive, not exhaustive

Cloudflare’s results point to a broader shift in agent infrastructure. The winning pattern is unlikely to be giving a model every possible capability before it starts. It is giving the model a safe way to discover the right capability, use it and return the smallest useful result.

That makes AI agents more efficient. The next step is making them accountable.

ELI auto-finds and manages every SaaS subscription and AI agent, helping growing companies connect software and agent spend to owners, budgets and purpose. See your software and AI spend in one place with ELI.

See what ELI finds in your stack.

Connect one source. Five minutes. Free to start.

Connect your stack →