Code Mode in Claude Code: one script, many tool calls

AI-generated illustration.
Code Mode is an experimental setting in the terminal version of Claude Code. With it on, Claude writes a short JavaScript program that makes its tool calls, and Claude Code runs that program in your session. Without it, Claude asks for each call separately and reads each result before deciding on the next. It arrived off by default in v2.1.323 on 12 November 2026, and Anthropic's docs still say it "may change or be removed in any release."
The honest headline after three weeks: it cuts tokens hard when a task makes many small, independent calls, such as a hundred-odd MCP lookups, and it saves little or costs more when Claude would otherwise have done the job in one shell command. It also moves part of your safety net. In auto mode, the default starting mode in the terminal, the classifier reviews the script once instead of reviewing each call inside it. And it's changing fast: 81 changelog lines mention it between v2.1.323 and v2.1.333.
Everything here is as of 1 December 2026, against Claude Code v2.1.333.
What it is
In ordinary mode, every tool call is a model round trip. Claude asks for Bash, Read or Edit, waits, reads the result, and decides what to do next. Code Mode writes those decisions into code. In the docs' words, "One piece of code can run commands in parallel, use one result in the next call, and return the part of the output that Claude needs to read."
Anthropic's example is a four-line script that raises a retry limit:
const files = await $`rg -l "function retryPolicy" src`.text();
const target = files.trim().split("\n")[0];
await edit(target, "maxAttempts: 3", "maxAttempts: 5");
({ edited: target, tests: $`npm test -- retryPolicy`.nothrow() });
Search, edit and test happen in one step, and only the last line goes back to the model. The conversation shows the tool calls as usual, including the edit's diff. The script itself stays out of view until you press Ctrl+O, which opens the transcript viewer with each script in a collapsed Code row.
Under the hood the script runs through a tool called REPL. A practitioner who compared /context with Code Mode on and off, on v2.1.323, found the model's tool list cut down to REPL and a Poll tool, with everything else described as JavaScript functions. The system tools block fell from 24.6k tokens to 11.1k (wmedia.es). That smaller block rides along on every request, which turns out to matter for cost.
You turn it on with an environment variable, CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1, set in your shell for one session or in the env block of ~/.claude/settings.json for all of them. Claude Code asks you to confirm the first time, and the top of the session then reads "Code Mode (experimental) is on". Two of the defaults are sensible. A cloned repository can't switch it on for you, because Claude Code ignores the variable in a project's settings files. And a 0 anywhere beats a 1 anywhere else, so an administrator can block it for a whole organization from managed settings. They can't force it on.
The requirements are narrower than a single variable suggests. From the docs' requirements list:
| Requirement | As of v2.1.333 |
|---|---|
| Version | v2.1.323 or later; v2.1.331 or later on Amazon Bedrock, Google Cloud's Agent Platform or Microsoft Foundry |
| Account | A Claude subscription, a Console account or one of those cloud providers; not through a gateway, and not Claude for Government |
| Organization | Not on the HIPAA configuration or zero data retention |
| Model | Claude Opus 5.5, Sonnet 5.5, Haiku 5.5 or Fable 5.2 |
| Surface | An interactive claude session in a terminal; not the VS Code panel, the Desktop app or cloud sessions |
| Install | The native build, plus Git for Windows on Windows |
Some limits stick to the session once it starts. A Code Mode session resumes only with Code Mode on. Remote Control, Claude in Chrome and computer use are unavailable, and the session can't move to the Desktop app. A script that Claude waits for stops after ten minutes; one that runs in the background can go for two hours.
There's one dispute about the surface. A user reported on 28 November that claude -p and Agent SDK sessions do run Code Mode when the variable is passed through --settings, and quoted a startup notice that names both. The docs page still says terminal only. I'd treat headless use as unsupported until the page changes.
Where the idea comes from
Code Mode brings an idea that has circulated in agent research for almost three years into Anthropic's own coding agent. What's new is where the code runs.
| Date | Who | What | Where the code runs |
|---|---|---|---|
| Feb 2024 | Wang et al., CodeAct | Agents act by writing Python | A Python interpreter |
| Dec 2024 | Hugging Face, smolagents | CodeAgent, agents that write their actions in code |
Locally, or in an E2B sandbox |
| Sep 2025 | Cloudflare, Code Mode | MCP tools turned into a TypeScript API | A fresh V8 isolate with no internet access |
| Nov 2025 | Anthropic, code execution with MCP | MCP servers presented as code files | An execution environment the developer builds and sandboxes |
| Nov 2025 | Docker, Dynamic MCP code-mode |
JavaScript that combines several MCP servers' tools | An isolated container |
| Nov 2025 | Anthropic, Programmatic Tool Calling | Python that calls tools you define on the API | Anthropic's code execution container |
| Nov 2026 | Anthropic, Code Mode in Claude Code | JavaScript that calls Claude Code's own tools | Your session, on your machine |
The research root is CodeAct, submitted in February 2024 and accepted at ICML 2024. It proposed using "executable Python code to consolidate LLM agents' actions into a unified action space", and across 17 models the authors report "up to 20% higher success rate" than the usual JSON and text formats. Hugging Face gave the same idea "first-class support" in its smolagents library at the end of 2024.
The name comes from Cloudflare. Kenton Varda and Sunil Pai's September 2025 post opens with "It turns out we've all been using MCP wrong," and argues that models have seen far more real TypeScript than synthetic tool-call tokens, so they handle more tools, and more complex ones, when the tools look like an API. Cloudflare runs each script in a fresh V8 isolate where fetch() and connect() throw errors. The only way out is through bindings to the connected MCP servers, which also keep API keys out of the code's reach. Cloudflare's docs still label it experimental.
Anthropic followed on 4 November 2025 with an engineering post, Code execution with MCP, which credits Cloudflare's "Code Mode" by name and reports one example falling "from 150,000 tokens to 2,000 tokens". The same post is frank about the price: "Running agent-generated code requires a secure execution environment with appropriate sandboxing, resource limits, and monitoring." Two days later Docker added an experimental code-mode tool to its MCP gateway, and Jim Clark's post made the same point: "If we're going to run this code, we're going to want it to run in a sandbox." Docker's docs still say it "is in early development and is not yet reliable for general use."
The closest relative is Anthropic's own Programmatic Tool Calling, launched on the API on 24 November 2025, again crediting "Cloudflare's Code Mode". There, Claude writes Python that runs in Anthropic's code execution container and calls tools you define. Its docs mark it ineligible for zero data retention and HIPAA, the same two carve-outs Code Mode has. That launch is also the home of the most-quoted number: "Average usage dropped from 43,588 to 27,297 tokens, a 37% reduction on complex research tasks." Since mid-November that 37% has been widely attached to Code Mode. It belongs to the API feature. Code Mode's docs page gives no savings figure at all.
Two other agent tools have versions of their own. Docker Agent has a Code Mode that swaps an agent's tools for a single run_tools_with_javascript tool. OpenAI's open-source Codex carries a code_mode flag in its source, off by default and at the stage the code describes as "not ready for external use".
So here's the real difference. The tool-calling designs of the past year put the generated code somewhere with little reach: Cloudflare's isolate with no network, Docker's container, Anthropic's sandbox. Docker Agent's script runs in an embedded JavaScript engine where "there is no filesystem, network, or process access beyond the tool functions injected into it", and in exchange its inner calls "do not go through a second round of permission checks or approval prompts". Claude Code makes the opposite trade. The script runs in your session, against your working tree, with your shell, and the docs are explicit that "the JavaScript itself and its file, web, and MCP tool calls run outside the sandbox." In return, your permission rules and hooks still check each call inside it. That reach is what makes Code Mode useful for coding, and it leaves the permission system holding the line where its predecessors relied on isolation.
When a script beats one call at a time
Anthropic's docs suggest three shapes of task: "A change that repeats across many files", "A search whose results need filtering before Claude reads them" and "Several independent commands that can run at the same time". The API team's guidance for Programmatic Tool Calling also names the losers. It calls the approach less beneficial for "simple single-tool invocations", for "quick lookups with small responses" and for work where "Claude should see and reason about all intermediate results" (Anthropic). That was written for the API feature, but the reasoning carries over.
Anthropic hasn't published a measurement for Code Mode itself. Five people outside Anthropic have. All five are small, and all are best read as reports:
| Who | Setup | Code Mode off | Code Mode on |
|---|---|---|---|
| wmedia.es, 13 Nov | Opus 5.5, top three customers across 40 JSON files, one run each | 150,364 input tokens, 15 s | 94,659 input tokens, 37 s |
| A dev.to post, 29 Nov | Sonnet 5.5, find saved keys across about 850 Swift files, two runs each | About 530k tokens read | About 240k tokens read |
| Issue #111033, 18 Nov | Opus 5.5, dead links across 432 Markdown files, one run each | 3 Bash calls, 40 s, $0.58 | 742 Read calls, 1 min 36 s, $0.62 |
| Issue #111575, 21 Nov | 11 sessions of the reporter's own work | Median 13.2 turns per prompt | Median 6.0 turns per prompt |
| Issue #112957, 29 Nov | 4 models, 5 task types, 5 paired runs per cell | Cost 1.0× | 0.13–0.28× for 150 MCP calls; no consistent change on the other four tasks |
The last one is the most careful. Its author made 200 runs on v2.1.332, randomized the order and checked every answer with a script. On a task that made 150 sequential calls to a local MCP server, Code Mode brought cost down to between 0.13 and 0.28 of ordinary mode on every model, and the median turn count fell from about 155 to between 4 and 11. On the other four tasks (a batch count over about 400 files, a multi-step lookup, a fix-and-test loop and filtering a 3.2 MB log), 12 of the 16 cells showed no difference. Two cost more: Sonnet on the batch task at 1.71× and Haiku on the log filter at 1.65×. One caveat: it ran in claude -p, outside the documented surface.
Time is less clear. wmedia's run took more than twice as long with Code Mode on, the dev.to runs were about even, and #112957's MCP task finished in 0.14 to 0.31 of the time.
The token pattern across all five is clear enough to act on. Code Mode saves in two ways. The fixed tool context is smaller on every request, which wmedia's run shows even though its turn count stayed at four. And many small calls collapse into a few turns, which is where the MCP numbers come from. It loses when Claude uses the script to fire hundreds of single Read calls at a job it would otherwise have done with one python3 -c or rg. That's what #111033 caught, at 742 Reads against 3 Bash calls and 2.4 times the wall time, and what #112957 saw in Sonnet's batch runs, where the median script made 441 inner calls. Each of those calls also passes through permission checks and hooks.
My read: turn it on for MCP-heavy work and for fan-out searches across a large codebase, and expect little from it on small edits. The dev.to author saw tokens halve on a one-line change, but both runs finished in seconds.
Running the comparison yourself
The request behind this article asked for an on/off test of the same multi-file change, comparing turns, tokens and time. I have no numbers of my own to report, so here's how I'd set the test up so that its numbers mean something.
- Pick a real change that touches several files, and freeze the starting point on a branch or in a worktree, so every run begins from the same commit.
- Name the model in full, such as
--model claude-opus-5-5. The docs warn that aliases can select older models on some providers. - Start each run in a fresh terminal session. For the Code Mode runs, start with
CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1 claudeand check that the session header says Code Mode is on. - Alternate the order (off, on, on, off and so on) and do at least three runs of each. The dev.to author's two Code Mode runs made 58 and 13 inner calls on the same task, so a single run tells you little.
- Record
/costand/contextat the end of each run, wall time from the terminal, and turns from the session transcript. If you export OpenTelemetry, calls made from code carrytool_use_idvalues starting withrepl_ortoolu_cc_, which confirms the run really was in Code Mode (docs). - Check the work as well as the bill. Run the tests and diff the branches, because a cheaper run that skipped a file is a false saving.
Security: one approval, many calls
This is where Code Mode asks the most of you. The docs put it in one sentence: "In auto and bypassPermissions modes, code can run from start to finish without a permission prompt." Auto mode is the common case. Since v2.1.283 it has been "the built-in starting permission mode for interactive terminal and VS Code sessions" (permission modes).
In auto mode, a second model, the classifier, reviews risky actions in place of you. With Code Mode on, "the code that Claude writes goes to the classifier once before it runs, and the tool calls inside it don't" (permission modes). The docs add the limit that matters: the classifier "doesn't see values computed at run time, such as a path taken from search output, so keep rules or hooks for calls you must control." If a script takes a path from search output and passes it to rm, the classifier sees the code that does that, and the path stays invisible to it.
In Manual and the other modes that ask, little changes: you're prompted for each call in the script that would have prompted you anyway. In every mode, your rules and hooks still run per call. Deny and ask rules match each call inside the script under the tool's usual name, so an ask rule for Bash(git push *) still prompts you in auto mode, for the forms of the command it matches. PreToolUse hooks fire for each inner call too. Two details matter for anyone who has written hooks. Shell commands can arrive in a different shape: the docs' example is a git push in a folder named app, which reaches your hook as cd 'app' && git push, so a hook that checks only the start of the string misses it. And rules match text, so "Claude can run a denied command in a form the rule doesn't match, with or without Code Mode."
The sandbox covers less than you might assume. Your sandbox settings apply to the shell commands a script runs, while the JavaScript and its file, web and MCP calls run outside it. If you want those isolated too, the docs' answer is to run Claude Code in a container or a virtual machine.
To keep a human on every script, add "ask": ["REPL"] to your permissions, and read the fine print. The prompt doesn't show the code; you press Ctrl+O and expand the Code row to read it. In auto mode, approving a script means "the code starts even when the classifier would have blocked it, and its project edits and read-only commands run without a prompt." Your approval stands in for the classifier's on that script.
Stopping is partial. Answer No at a prompt for one of the script's calls, or press Esc, and "the code stops, and changes from calls that already finished stay in place." A script that edits twenty files and fails on the twenty-first leaves twenty edited files, and the reporter of #111575 has asked for an all-or-nothing mode for that reason.
Hooks have the most sharp edges, and their defaults have already moved once:
| Hook behaviour in Code Mode | Source |
|---|---|
For command and HTTP hooks, a PreToolUse hook that times out refuses the call; one that fails another way, such as a missing script, lets the call go on to permission checks unless it sets "onFailure": "block" |
Docs |
| Before v2.1.329 a hook that errored refused the call; v2.1.329 changed that to match ordinary mode | Changelog |
From v2.1.328, UserPromptSubmit hooks, managed ones included, stop seeing script output; redaction moves to a PostToolUse hook on REPL that returns updatedToolOutput |
Changelog, docs |
| A redaction hook that crashes, times out or breaks the output's shape hands Claude the original output | Docs |
A "*" PreToolUse matcher sees the inner calls but misses the REPL call itself; only a "REPL" matcher gets the code |
Issue #112954, user report |
Reading a file through the script's file() helper fires no Read hook event |
Issue #111575, user report |
| A PostToolUse redaction keeps only about 30,000 characters of a large output, cutting the script's data short | Issue #112065, user report |
The fail-open default is the one I'd deal with first. If a hook guards something you care about, give it "onFailure": "block" in every settings file that defines it.
Telemetry needs a decision as well. Code "can read a value, such as a token in a file, and pass it to another call", so the docs advise leaving OTEL_LOG_TOOL_DETAILS unset if your backend shouldn't receive such values. They also advise auditing from exported events, since spans can be lost after a script makes many calls.
My read: in auto mode, treat your deny rules and fail-closed hooks as the real boundary for anything destructive, and the classifier's single look at the script as a second opinion. If you run hooks for compliance, test each one in a Code Mode session before anyone on the team turns it on.
What it costs
Code Mode has no price of its own. The docs list no charge for it, and it runs on whatever already pays for your Claude Code use: a subscription, a Console account or a cloud provider. The cost question is tokens, and the evidence above says the answer depends on the task.
Anthropic has worked on the token side through the three weeks. v2.1.328 "Improved Code Mode's token use with shorter tool descriptions" and improved prompt caching for new sessions with MCP servers (changelog). One open report points the other way. Filed on 30 November, it found that workflow subagents launched from a Code Mode session were sent every tool schema inline. With 44 MCP tools connected, a subagent's first call grew from 37,956 tokens to 86,578, and with 347 tools it reached 358,814 (#113159). /context in the main session shows none of it. If you pay per token and run workflows, that's the line to watch.
Is it ready for daily use?
For one person who opts in and reads the transcript, I think it's close. As a team default it's too early, for three reasons.
The first is churn. From v2.1.323 on 12 November to v2.1.333 on 30 November, 81 changelog lines mention Code Mode: 50 fixes, 19 improvements, 8 behaviour changes and 4 additions (changelog). Every release section from v2.1.325 onwards has some, and v2.1.328 alone has 22. The fixes are substantive. They include "a runaway loop in Code Mode filling the machine's memory" (v2.1.329), scripts losing the middle of a long output (v2.1.331), and Code Mode "silently skipping a tool call whose input has a list of over 4,096 items" (v2.1.332). The additions widened its reach: agent teams in v2.1.326, OpenTelemetry events in v2.1.329, and Bedrock, Google Cloud, Microsoft Foundry and Claude Platform on AWS in v2.1.331, with tracing following in v2.1.333.
That pace has a practical cost. The stable release channel serves a version that is "typically about one week old, skipping releases with major regressions" (setup docs), and on 1 December npm's stable tag pointed at v2.1.330, three releases behind latest. A team on stable is still waiting for the cloud-provider support and the long-output fix.
The second is the open bug list. On 1 December, a GitHub search for Code Mode in issue titles returned 44 issues opened since launch, 36 of them still open, and all but one are about this feature. Some are about the model's habits more than the code. One reporter watched Claude wait on a 20-minute CI run with sleep() calls inside scripts, each holding the session for up to ten minutes. Another found Claude reading files one call at a time from inside a script, where a single command would have scanned them (#111033).
The third is that it can disappear under you. The docs' troubleshooting table includes the reasons "Anthropic has turned it off for now" and "it isn't open to your account yet", and for the second one the advice is to try again another day. My read is that a team workflow shouldn't rest on a switch that can go dark between sessions.
The counterweight is real. The reporter of #111575, working across 11 sessions, saw median turns per prompt fall from 13.2 to 6.0 and median context tokens from 2.22 million to 1.15 million. Their failure rate dropped from 19% to between 0 and 3% within a week as fixes landed. That's the encouraging trend, and the changelog backs it: a request filed on 20 November about scripts losing the middle of long output (#111373) is credited in the v2.1.331 fix, released three days later.
Where I'd start
If you want to try it this week, this is the order I'd go in:
- Turn it on per shell with
CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1 claude, and keep it out ofsettings.jsonuntil you've seen it work on your own tasks. - Stay on the latest channel while you try it, since stable is still on v2.1.330 and three releases of fixes have landed since.
- Start with a task that makes many independent calls, such as an MCP-heavy job or a search across a large codebase, and run the comparison above before you believe a saving.
- Before the first session, add ask rules for commands you'd never want run unseen, such as
Bash(git push *), and set"onFailure": "block"on any hook that guards something. - If you administer Claude Code for a team, decide now whether to put
"CLAUDE_CODE_EXPERIMENTAL_CODE_MODE": "0"in managed settings until it leaves experimental. Anyone already in a Code Mode session keeps it until they restart.
Sources
- Anthropic, Run tool calls from code with Code Mode, Claude Code docs, read 1 December 2026
- Anthropic, Permission modes, Claude Code docs, read 1 December 2026
- Anthropic, Set up Claude Code, release channels, read 1 December 2026
- Anthropic, Claude Code changelog, v2.1.323 to v2.1.333
- Anthropic, Code execution with MCP: building more efficient agents, 4 November 2025
- Anthropic, Introducing advanced tool use on the Claude Developer Platform, 24 November 2025
- Anthropic, Programmatic tool calling, Claude Platform docs
- Kenton Varda and Sunil Pai, Code Mode: the better way to use MCP, Cloudflare, 26 September 2025
- Cloudflare, Code Mode, Agents docs
- Jim Clark, Dynamic MCPs with Docker: stop hardcoding your agents' world, Docker, 6 November 2025
- Docker, Dynamic MCP, Docker docs
- Docker, Code Mode, Docker Agent docs
- OpenAI, Codex feature registry, source at commit 997204b, 1 December 2026
- Xingyao Wang et al., Executable Code Actions Elicit Better LLM Agents, arXiv, 2024
- Hugging Face, Introducing smolagents, 31 December 2024
- wmedia.es, Code Mode in Claude Code, tested 13 November 2026
- dev.to, I turned on Claude Code's experimental Code Mode. Token reads dropped by half, 29 November 2026
- i10x, Claude Code's experimental Code Mode batches tool calls in JavaScript, updated 1 December 2026
- Claude Code issues: #111033, #111373, #111575, #112065, #112152, #112894, #112954, #112957, #113159