2026.10.16 · 4 min · ai · agents · tooling · javascript
Code Mode: one script instead of ten tool calls.
In a normal agent loop, every tool call is a round trip and every raw result lands in the model's context. With Code Mode, the agent writes one small JavaScript script that calls its tools as functions, and only the final result goes back. OpenCode 2.0 turns it on by default for every MCP server; Cloudflare measured what it saves. As of 2026.10.
If your agent makes ten tool calls, that's ten round trips. The model asks for a call, the raw result lands in its context window, the model reads it, and only then decides on the next call. Every intermediate result piles up in context, whether the model needed it or not. If you use Claude Code, Cursor or OpenCode with a few MCP servers, you've likely hit this limit without knowing why. Code Mode turns that chain into one program: one round trip, and a small context.
Watch the 60-second version on Instagram.
Ping-pong vs one script
On the left is the normal loop: each call goes out, each result comes back into the model's context. On the right, the agent writes a script, the script makes the calls inside a sandbox, and only its return value goes back to the model. (The figure is illustrative: five calls, and block sizes I didn't measure.)
A script can call a tool, map over the results and call another tool "in a single round trip", as OpenCode's announcement puts it. So the plumbing between calls (loops, filtering, passing one result into the next call) moves out of the model's context and into code.
What the script looks like
This is a simplified version of the example in OpenCode's post. It lists GitHub bug issues, files each one in Linear, and returns only the new issue ids. The argument names are illustrative.
// one script, one round trip
const { issues } = await tools.github.list_issues({ labels: ["bug"] });
const created = await Promise.all(
issues.map(i => tools.linear.save_issue({ title: i.title }))
);
return created.map(c => c.identifier); // only this reaches the modelThe issue list never enters the model's context. Only the array of ids does.
OpenCode 2.0 turns it on by default
OpenCode 2.0 was announced this week. It adds a "minimal JavaScript interpreter" in the @opencode/codemode package, which turns MCP servers or OpenAPI endpoints into functions the agent calls from a script. OpenCode says these scripts run natively, without Node.js VMs or an embedded engine like QuickJS; it names no other sandbox.
"All MCP servers in 2.0 are opted into code mode by default." It's a per-server setting, though, not a global switch:
- The v2 docs list a
codemodekey on each MCP server that defaults totrue. "codemode": falseexposes that server's tools directly again.- With Code Mode, tools are grouped by server:
tools.context_7.resolve_library_id(...). Without it, they show up as flatcontext7_*tools.
2.0 is an opt-in major upgrade. GitHub's latest tagged release is still v1.19.2 (2026.10.16), and its notes call OpenCode 2 a "separate, explicit choice" you make with opencode upgrade --major. It also brings a single shared background server, background subagents, and an @opencode/sdk package that is still in development.
What it saves: Cloudflare's numbers
OpenCode hasn't published any savings. Cloudflare has, for its own API. The Cloudflare API has 2,500+ endpoints. A normal MCP server exposing them would take about 1.17M tokens. With Code Mode it takes about 1,000 tokens, across two tools, search() and execute(): 99.9% fewer input tokens. Drawn to scale, the Code Mode bar is under a third of a unit wide.
Cloudflare runs the code in a Dynamic Worker isolate: a V8 sandbox with no file system, no env vars, and outbound fetch off by default.
What people get wrong
- "Code Mode means the AI runs arbitrary code on my machine." The script can only call the tool functions it's given. Cloudflare's sandbox has no file system, no env vars and no network by default.
- "It's about the AI writing better code." It's about moving the plumbing between tool calls out of the model's context.
- "OpenCode saves 99.9% of tokens." That's Cloudflare's measurement, for Cloudflare's API. OpenCode published no number.
- "OpenCode runs scripts in a V8 isolate." That's Cloudflare's sandbox. OpenCode only says it uses its own interpreter, not Node VMs or QuickJS.
- "It's always on." It's the default per MCP server, and
"codemode": falseturns it off for that server.
On a client project, I'd leave Code Mode on for the big API-shaped servers where results are long lists, and turn it off with "codemode": false for a small server whose raw output I actually want the model to read.
The short version
- A normal agent loop pays one round trip per tool call, and every raw result lands in context.
- With Code Mode, the agent writes one script that calls tools as functions; only the return value reaches the model.
- OpenCode 2.0, announced this week as an opt-in major upgrade, turns Code Mode on by default for every MCP server, with
"codemode": falseper server to opt out. - Cloudflare measured its 2,500+ endpoint API at about 1.17M tokens as a normal MCP server, and about 1,000 with Code Mode.
- The sandbox matters: Cloudflare's has no file system, no env vars and no network by default.
Sources: Anomaly, OpenCode 2.0 · OpenCode v2 docs, MCP servers · The New Stack · DiarioBitcoin · GitHub, anomalyco/opencode v1.19.2 · Cloudflare, Code Mode for MCP