Code mode for MCP: one script instead of a chain of tool calls

An agent on MCP reads every tool result, and every result passes back through the model. Code mode lets the model write one script instead, so only the answer comes back. We asked Claude Code the same question with and without its new Code Mode, and checked what we saw against the published benchmarks.

Vojin ĐorđevićSenior AI EngineerPublished 25 November 2026 · 4 min read · Cross-industry · JavaScript · MCP

Without Code Mode

Question
Model reads all 112 records
GitHub
Script, retyped by hand

Every record lands in the model. It then retypes them into a script to compute the median.

With Code Mode

Question
Answer
Model writes one script
ScriptPages, counts and finds the median herelist_pull_requests()mediancount by author
GitHub

The records stay in the script. The model reads one small summary back.

01

Where plain tool calling breaks

—

Definitions. Every tool description used to sit in the prompt before the request. Tool search now handles most of this: in our runs, Claude Code loaded GitHub’s 117 MCP tools on demand, and they took 689 tokens.

—

Results. Every result passes through the model, even when the model only needs a slice of it.

—

Long jobs. The model writes out every call and keeps count itself. In Pydantic’s tests, success without code mode fell from 92 to 100% to 62 to 79% as the job grew from 5 items to 200.

02

Who ships code mode now

Cloudflare named the pattern in 2025, and its runtime can now pause and wait for approval when the code reaches a sensitive action. Anthropic’s API lets Claude write code that calls your tools inside a code execution container. OpenAI runs each generated program in a fresh, isolated V8 runtime. Claude Code added Code Mode as an experimental switch: Claude writes JavaScript that makes the tool calls, and Claude Code runs it in your session.

03

One question, two runs

We connected Claude Code 2.1.332, on Sonnet 5.5, to GitHub’s official MCP server. On 25 November 2026, we asked the same question about the MCP Python SDK repository in two fresh sessions: the median number of days since each open pull request was last updated, and the five authors with the most open pull requests.

Without Code Mode, Claude pulled all 112 records into its context. To compute the median, it then retyped every author and date into a shell script, and noted that this was a possible source of error.

With Code Mode, Claude wrote one script. The records stayed in the script, and the model read back a small summary.

Tokens added by the question

15.9k

with Code Mode, against 30.1k without.

Without Code Mode · 30.1k
With Code Mode · 15.9k
Source: our run, 25 November 2026. Claude Code’s own estimates, one run per mode.
Time to answer

15s

with Code Mode, against 45 s without.

Without Code Mode · 45 s
With Code Mode · 15 s
Source: our run, 25 November 2026. Time shown by Claude Code, one run per mode.

Both runs gave the same answer: 112 open pull requests, a median of about 121 days since the last update, and the same top authors. Code Mode sessions also started smaller, at 18.0k tokens against 31.1k, most of the difference being Claude Code’s built-in tool list.

This was one run per mode, on one model and one repository. It shows the mechanism, not a benchmark.

04

What the script did

This is the code Claude wrote in our Code Mode run, in the two blocks it ran. We changed only spacing and line breaks, so it reads more easily.

First, it searched for the right tool:

const r = await searchTools("github list pull requests", 10); r

Then it wrote the script:

const all = [];for (let page = 1; page <= 30; page++) {  const r = await tools.mcp__github__list_pull_requests({    owner: "modelcontextprotocol",    repo: "python-sdk",    state: "open",    perPage: 100,    page,    fields: ["user", "updated_at"]  });  let data = r;  if (r && r.content) data = r.content.map(c => c.text).join("");  if (typeof data === 'string') {    try {      data = JSON.parse(data);    } catch (e) {      self.log(preview(data, 500));      break;    }  }  if (!Array.isArray(data)) {    self.log(preview(JSON.stringify(data), 500));    break;  }  all.push(...data);  if (data.length < 100) break;}const now = new Date("2026-11-25T13:12:00Z");const days = all  .map(p => (now - new Date(p.updated_at)) / 864e5)  .sort((a, b) => a - b);const n = days.length;const median = n % 2  ? days[(n - 1) / 2]  : (days[n / 2 - 1] + days[n / 2]) / 2;const counts = {};for (const p of all)  counts[p.user.login] = (counts[p.user.login] || 0) + 1;const top = Object.entries(counts)  .sort((a, b) => b[1] - a[1])  .slice(0, 8);({ n, median, top, sample: JSON.stringify(all[0]) })

Three things stood out.

—

It guessed at the return shape. The checks after the call handle a reply that might arrive as text, JSON or something else. A stub that declares what it returns removes that guesswork.

—

It hardcoded the time instead of reading the clock.

—

It cut the tie list short. Returning the top eight named four of the seven authors tied for fifth. Claude said so, and the run without Code Mode listed all seven.

05

What the benchmarks show

—

Long jobs. Across 5,276 graded runs, Pydantic found that on long tasks code mode roughly halved cost and wall time. The benchmark code isn’t public, and Pydantic sells the sandbox it used.

—

Small jobs. In the same benchmark, code mode cost 21 to 38% more on three of five tasks at five items.

—

Return types. In a pre-registered study, removing what each tool returns cut code mode’s success by 22.7 points for one model and 39.5 for another.

—

Best cases. Anthropic’s own worked example went from 150,000 tokens to 2,000. For Claude Code’s version, InfoQ notes that Anthropic has published no token or latency figures. Only your own tasks show your number.

06

When not to do this

—

Small jobs. With a few tools and short results, plain tool calls are as good or cheaper.

—

Decisions that need reading. When the model must read a result before choosing the next step, keep that step as a direct call.

—

Writes without approval. Code mode doesn’t make anything safe by itself. As Pydantic puts it, the security boundary is the list of functions you expose. Writes need a person’s approval, either as direct calls or through a runtime that pauses.

—

Code without isolation. Researchers at Ben-Gurion University count sixteen attack classes for code-executing MCP. In Claude Code, the JavaScript and its MCP calls run outside the sandbox, so run it in a container or virtual machine when the data matters.

At Level 2, code mode is a switch the vendor gives you. At Level 3 or 4, it’s a design you own: the sandbox, the tool list and the approval step. Either way, it changes how much the model reads, not who is allowed to do what.

Build Notes · for engineersMore in Insights →

Bring the system you’re trying to make work.

Schedule a call with a partner
Or start on your own: the free AI Investment Diagnostic

Find out where AI could pay in your company. The report is yours either way.