Code Mode: Why AI Coding Agents Now Write JavaScript Instead of Calling Tools
Quick answer: Code mode replaces tool-calling with tool-writing. The server exposes one code-execution tool and the model writes a short program that calls the real tools as functions. Claude Code shipped it as an opt-in in v2.1.323 (November 2026). It cuts schema and round-trip cost, and it moves the security boundary from the permission prompt to whatever sandbox runs the generated program.
For two years the shape of an AI coding agent was fixed: the model emits a JSON tool call, the host executes it, the result returns to context, repeat. That shape has a cost, and in November 2026 five separate vendors shipped the same answer to it. The model now writes code instead of calling tools.
This is the most interesting change in agent tooling since MCP standardised the tool interface, and the least understood part of it is not the API. It is where the generated program is allowed to run.
What is code mode?
Code mode is a tool interface where the model's output is a program, not a call. The MCP server advertises a single tool — codemode, code, or execute_code — and the underlying operations become async functions inside the sandbox the program runs in. The model writes something like const rows = await db.query({ ... }); return summarize(rows);, and only the reduced return value enters the context window.
One tool instead of fifty
The first cost it removes is definition cost. Advertising every operation's schema up front burns context before the agent has done anything, and a large MCP server can spend thousands of tokens on tool descriptions the model never uses. Progressive discovery — a search tool plus an execute tool — or a single typed code tool replaces that catalogue with something fetched on demand.
The round-trip tax it removes
The second is round-trip cost. In classic tool calling, intermediate payloads pass through the model: fetch 200 rows, put them in context, ask for a filter, fetch again. In code mode the loop happens inside the sandbox and the model sees only the result. For an agent working a long task, that is the difference between a context window spent on data and one spent on decisions.
Where it shipped
Claude Code's changelog lists Code Mode in v2.1.323 as an experimental opt-in where Claude writes JavaScript to call its tools. Cloudflare ships @cloudflare/codemode for Agents, and flags it as experimental with possible breaking changes. VoidLLM, AnythingMCP and reShapr all document code-mode MCP servers. A conference idea became a shipped feature in about a year.
How does the code-mode contract work?
The contract is small. The outer tool takes { code } and returns one of three outcomes. Cloudflare's durable-execution version returns completed, paused with a list of pending actions awaiting approval, or error. VoidLLM exposes concrete resource defaults: memory_limit_mb: 16, timeout: 30s, max_tool_calls: 50, and a pool of eight pre-warmed runtimes.
The paused/approve loop
The paused state is the part worth studying. A program that reaches a write does not fail and does not silently succeed: it suspends, the pending actions surface for a human or policy decision, and the run resumes with a recorded outcome. That is a better approval model than a per-call prompt, because it captures intent — "this script wants to delete seven files" — instead of seven yes/no questions a user will click through.
The call limit is a behaviour change
max_tool_calls: 50 is one implementation's default, not a recommendation, but the shape generalises: the sandbox has a budget. When a program hits it, the agent cannot mid-loop course-correct, because the intermediate reasoning never returned to it — the script fails as a unit. Budget for the worst realistic task, not the average one.
Which sandbox should run the agent's code?
This is the only question that matters for security, and the answer differs sharply across implementations.
| Where the program runs | Isolation | Network | What it inherits |
|---|---|---|---|
| Cloudflare Agents | Isolated Worker; stateless executor, separate runtime records approvals and replay | Worker model | The caller's tool permissions |
| VoidLLM | QuickJS compiled to WASM, run via Wazero (Go) | None from the sandbox; tool calls cross a Go host bridge | Explicit host functions only |
| AnythingMCP | One locked-down Deno subprocess per run, in a separate container | No network, no filesystem, no env vars, no secrets | Nothing; egress only through the gateway |
| reShapr | Inside the proxy itself | The proxy's | The caller's backend permissions |
Worker isolation is not process isolation
A Worker is a strong boundary against memory corruption and a weak one against a logic bug in your own tool bindings. If the sandbox exposes a tool that reads files, the program can read files. Isolation bounds the blast radius; it does not decide what is in range.
A separate process per run is the conservative default
AnythingMCP's design — a fresh locked-down Deno subprocess per execution, no filesystem, no environment, no secrets, and a single audited egress point — is the version that survives a bad day. It also states the threat model plainly: code written by an AI agent is untrusted, because a prompt-injected tool result can steer it.
Why is agent-written code hostile input?
Because the model may have read an attacker's text before writing the program. A poisoned document, a hostile issue comment, a tool result carrying instructions: any of these can influence the script that then runs with your credentials. The program is not "the agent's intentions". It is attacker-influenced code executing in your infrastructure.
The permission prompt disappears
Classic tool calling has a natural chokepoint: one call, one approval. A fifty-call script has one approval, at the top, where the user can only see the code. Reviewing generated JavaScript for intent is harder than approving rm -rf build/. Teams that keep the single-call approval UX in code mode have quietly removed the control they thought they had — the failure mode described in over-permissioned agents crossing the authorization line, with far fewer and much larger prompts.
Code mode does not widen authorization — it hides it
Every implementation is explicit that the sandbox limits what the code can reach, not what the caller is authorized to do. reShapr says it "changes how an MCP client works with an Exposition, not which backend permissions it has". AnythingMCP routes calls back through the gateway under the caller's role. That is the correct design, and it means the authorization decision has to be enforced inside the sandbox at each tool boundary. If you only audit the outer execute_code call, you logged one event and authorised fifty actions.
Normalisation bugs are the new bypass
The MCP policy layer is string comparison, and string comparison breaks quietly. AWS CVE-2026-125271 in the AWS API MCP Server (fixed in 1.5.1) normalised operation names before comparing them to denyList and elicitList, so operation names with consecutive capital letters never matched a correctly written deny rule. We covered a similar class of interface-level weakness in the ten MCP SDK advisories. Code mode adds calls per run, not new checks — so every check has to be right.
How do you govern code mode in practice?
Six controls, in order of how much they buy you:
- Run generated code outside the host process. A subprocess or WASM runtime per run. Never
eval, never a shared long-lived interpreter with credentials in scope. - Remove ambient capability. No filesystem, no environment variables, no secrets, no unrestricted egress. Anything the program can reach without an explicit grant is an implicit permission grant.
- Authorise at the call boundary. Every tool invocation inside the sandbox is checked against the caller's role, exactly as if it had arrived as its own MCP call. The sandbox is not an authorization mechanism — the permission prompt is the attack surface, and so is the boundary inside the runner.
- Gate side effects on a pause, not a prompt. Use the
paused/pending-action model so writes surface as a batch with intent, and the run stays resumable and recorded. - Budget the run. Timeouts, memory and call limits turn a runaway loop into a bounded failure. Set them from the harness's real behaviour — the harness, not the model, decides safety.
- Audit the run and keep it replayable. One record per execution, with the code, the calls, the approvals and the outcome. AnythingMCP states every run is audited; Cloudflare separates the executor from the runtime that records execution and controls replay and rollback.
Where the evidence is thin
Every security claim above comes from vendor documentation. There is no independent audit, no published escape suite, and no incident report of a code-mode sandbox being broken in the wild, and Cloudflare labels the feature experimental. So enable it on a read-only tool set first, with the call limit low and the audit log on, and watch the two failures that matter: a run that succeeds while doing something you did not intend, and a run that fails because the budget was set for the average task.
FAQ
Is code mode safe to enable in production?
Not without your own isolation. Cloudflare labels its code-mode server experimental, and no implementation publishes an independent escape test. Enable it behind a separate runner process with no ambient credentials, a call limit and per-run auditing — then treat the sandbox as the component that will eventually fail.
Does code mode give an AI agent more permissions?
No. Every implementation reviewed here keeps the caller's existing authorization: reShapr states it changes how a client works with an Exposition, not which backend permissions it has, and AnythingMCP routes each call back through the gateway under the caller's role. Code mode composes calls; it does not escalate.
What is the difference between code mode and normal MCP tool calling?
In normal tool calling the model emits one JSON call at a time and every result returns to its context. In code mode the model writes a program, the program calls tools as functions inside a sandbox, and only the reduced result returns. That removes definition and round-trip cost, and replaces per-call approvals with one approval over a batch of actions.
Can code mode break my prompt-injection defences?
It changes where they apply. The model may write the program after reading poisoned content, so the generated code is attacker-influenced. Prompt-level defences do not constrain code that is already running; only capability denial in the sandbox and the authorization check at each tool boundary do.
Which AI coding tools shipped code mode in 2026?
Claude Code shipped it as an experimental opt-in in v2.1.323 (November 2026). Cloudflare ships @cloudflare/codemode for Agents, and VoidLLM, AnythingMCP and reShapr all document code-mode MCP servers, each with a different sandbox placement.
Related reading
The Containment Stack: The Three 2026–2028 AI Skills That Make Agent Output Safe to Run
The AI wage premium is now 62%, but it is not paid for calling a model. It is paid for being answerable for what the model does. Three skills produce that answer: execution isolation, OpenTelemetry GenAI telemetry, and deterministic replay. Here is the evidence, the tools and a shipping roadmap for each.
AI Code Generation Is Not Language-Neutral: 56% of Rust Errors Caught, 0% of Python
A November 2026 study ran one model through six language setups. Rust's compiler caught 56% of its wrong first attempts for a semantic reason; Python's caught none. The lesson is not that Rust is safer — it is that your language sets the error floor and the token bill before the model writes a line. Here are the numbers, the caveats, and a per-language acceptance gate you can run in CI today.
The Metered Three: The 2026–2028 AI Skills That Decide What Your Tokens Cost and Whether They're Correct
Four earlier posts on this site named eighteen skills that sit in front of the model. None covered the three seams where an LLM product actually bleeds: the prefix it re-reads on every turn, the contract its output must satisfy, and the route the request travels. Here they are, with the failure modes, the 2026 pay evidence, roadmaps and starting prompts.