Capital & Compute

Code Mode vs Direct Tool Calls in Claude Code and Codex

With Code Mode, the agent writes JavaScript that calls its tools instead of one call per turn. What changes in Claude Code and Codex, and what it saves.

· ai· coding-agents· claude-code· codex· tokens· By Capital & Compute
Code Mode cut input tokens to about 0.6x in one Claude Code test but took 2.5x as long.

Without Code Mode, a coding agent calls one tool, reads the full result, and only then picks the next tool, one model turn per step. With Code Mode, the agent writes a short JavaScript program that makes several tool calls, filters the output, and hands back only what it needs. Claude Code added it as an experimental opt-in on 12 November 2026; Codex has had an off-by-default version since March 2026.

The only public with-and-without test so far cut input tokens by 37% on the same task, but the run took 37 seconds instead of 15, and both sessions made the same four model requests. The saving came from a smaller tool list, not from fewer turns. The verdict: turn it on for repetitive multi-file edits and searches you would otherwise filter by hand, and leave it off if you rely on the VS Code panel, Desktop, Remote Control or per-command review in auto mode.

What changes when Code Mode is on?

The task stays the same and the result usually does too. What changes is who sequences the tool calls.

Here is the example Anthropic’s Code Mode documentation uses: raise a retry limit and run the tests. Without Code Mode, Claude Code works through four separate tool calls, and the model reads each result before deciding the next one:

Bash(rg -l retryPolicy src)          -> src/http/retry.ts
Read(src/http/retry.ts)              -> 24 lines into context
Update(src/http/retry.ts)            -> maxAttempts: 3 becomes 5
Bash(npm test -- retry)              -> 4 passed, 4 total

With Code Mode on, Claude writes one piece of JavaScript that does the same work, and Claude Code runs it in the session:

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() });

The last line is what the model gets back: the path it edited and the test output. The 24 lines of the file never enter the context, because the script edited by string match without reading the file into the conversation. In the terminal you still see the individual tool calls and the diff. The script itself sits in a collapsed Code row you can open with Ctrl+O.

That is the whole mechanism. In Anthropic’s 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.”

Does Code Mode save tokens?

Sometimes, and not for the reason most coverage gives.

Anthropic has not published a benchmark for Code Mode in Claude Code. The one public with-and-without comparison is a hands-on test by wmedia.es, run on 13 November 2026 on Claude Code v2.1.323 with Opus 5.5 at medium effort. The task: find the three customers with the highest paid-order totals across 40 daily JSON files. Both sessions gave the same answer.

Metric Without Code Mode With Code Mode
Requests to the model 4 4
Input tokens, first request 36,404 22,233
Total input tokens, cache included 150,364 94,659
Output tokens 1,172 1,213
Wall time 15 s 37 s
System tools in /context 24.6k 11.1k
Claude Code with Code Mode as a multiple of the same task without itA log-scale chart of five metrics for one task run with and without Code Mode. System tool tokens fell to 0.45 times the baseline, first-request input tokens to 0.61 times and total input tokens to 0.63 times. Model requests, not charted, were four in both runs. Output tokens rose slightly to 1.03 times, and wall time rose to 2.47 times, from 15 to 37 seconds.Better for youWorse for youWithout Code Mode = 1x0.25x0.5x1x2x4xSystem tools tokens0.45xFirst-request input tokens0.61xTotal input tokens0.63xOutput tokens1.03xWall time2.47x
Claude Code with Code Mode as a multiple of the same task without it
MetricWith Code Mode as a multiple of Without Code ModeValues
System tools tokens0.45x11.1k vs 24.6k
First-request input tokens0.61x22,233 vs 36,404
Total input tokens0.63x94,659 vs 150,364
Output tokens1.03x1,213 vs 1,172
Wall time2.47x37 s vs 15 s
One task run once each way. Input tokens fell by roughly a third because the tool list shrank by more than half, while model requests stayed flat and wall time rose 2.5x.Source: wmedia.es hands-on test, Claude Code v2.1.323, 13 November 2026 (one run per condition)

The row that explains the rest is the tool list. With Code Mode on, the model sees a compact scripting interface instead of the full schema for every tool, so every request starts with less fixed context. The number of round trips did not change on this task. The author’s own caveat is the right one: “One run each is not a benchmark, and on this one Code Mode was slower.”

Two bigger numbers travel with Code Mode on social media, and neither measures it:

  • 37% fewer tokens is from Introducing advanced tool use on the Claude Developer Platform, Anthropic’s November 2025 engineering post on programmatic tool calling in the API, where “average usage dropped from 43,588 to 27,297 tokens, a 37% reduction on complex research tasks.” That feature has Claude write Python in a hosted container.
  • 98.7% fewer tokens is from Code execution with MCP (Adam Jones and Conor Kelly, Anthropic, November 2025), describing a drop “from 150,000 tokens to 2,000 tokens” when an agent loads only the MCP tool definitions it needs.

The current programmatic tool calling documentation reports a third figure: on the BrowseComp and DeepSearchQA agentic search benchmarks, adding programmatic tool calling “improved performance by an average of 11 percent while using 24 percent fewer input tokens.” All three point the same direction. None of them is a Claude Code measurement. For how tokens add up in an ordinary agent session, see how many tokens a coding agent actually uses.

How do Claude Code, Codex and OpenCode implement Code Mode?

The idea has a common ancestor. Cloudflare’s Kenton Varda and Sunil Pai named it in Code Mode: the better way to use MCP in September 2025, arguing that “LLMs are better at writing code to call MCP, than at calling MCP directly,” because models have seen far more real code than tool-call transcripts. The coding agents have since built their own versions, and they differ most in where the script runs and what it can touch.

Agent Status (24 Nov 2026) How to turn it on Script language Where the script runs
Claude Code Experimental, off by default (v2.1.323+) CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1 JavaScript In the session, outside the sandbox; shell commands it starts are sandboxed
Codex CLI Under development, off by default (0.114.0+) [features.code_mode] enabled = true in config JavaScript A fresh V8 isolate per call: no Node, no filesystem, no network
OpenCode 2.0 Shipped as the execute tool Built in JavaScript Strict sandbox: no filesystem, imports, timers or fetch
Claude API (programmatic tool calling) Stable Code execution tool, code_execution_20260120+ Python Anthropic-hosted code execution container

Sources for each row: Claude Code’s Code Mode documentation and changelog; the Codex 0.114.0 release notes, the Codex configuration reference and the tool description in code-mode-protocol/src/description.rs; this site’s OpenCode 2.0 breakdown; and the programmatic tool calling documentation above.

Claude Code treats the script as a way of driving its normal tools. $ runs shell commands, edit() changes files, and MCP tools are callable by name. Each call inside the script is still a Bash, Edit or MCP call as far as your permission rules are concerned. The JavaScript runs on your machine with the same reach as the session.

Codex is stricter about the script and looser about the gate. Its tool description says code runs “in a fresh V8 isolate as an async module” and runs raw JavaScript with no Node, no file system, no network access and no console. Everything happens through a global tools object, so await tools.exec_command(...) is the only way to touch the machine, and those calls go through Codex’s normal sandbox. Long scripts hand control back with yield_control() and resume through a separate wait tool, and results are capped at 10,000 tokens by default. It is also further from general use: the feature registry marks code_mode as “under development,” a stage the source defines as “not ready for external use.”

OpenCode sits closest to Cloudflare’s original design: a strict sandbox whose only reach is the tool catalog it is given.

Is Code Mode safe to turn on?

The individual tool calls are checked the same way they always were. The review of the script as a whole is where things change.

In Claude Code, “each tool call in the code goes through your deny and ask permission rules and PreToolUse hooks under the tool’s usual name.” A rule that asks before git push still asks. The difference is auto mode: its classifier “reviews each piece of code before it runs, not each tool call,” and “it doesn’t see values computed at run time, such as a path taken from search output.” If a script greps for a file and then deletes whatever it found, the classifier sees the code, not the path. Anthropic’s advice is to keep rules or hooks for any call you must control, and to add an ask rule for REPL (the tool that runs the code) if you want to approve each script yourself.

Two more details matter if you run hooks. A shell command from a script can reach your hook in a compound form (the docs’ example is cd 'app' && git push), so a hook that checks only the start of the string can miss it. And because a script can read a secret and pass it on, Claude Code supports a PostToolUse hook on REPL that redacts what the code returns.

Codex documents the same principle from its side: “hook decisions apply to that nested call. PreToolUse can stop the tool before it runs or rewrite its input.” A blocking PostToolUse cannot undo a side effect, but it can stop the result from reaching the script.

When should you turn Code Mode on?

Anthropic’s own list is the right starting point: “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.” Those are the cases where a script keeps intermediate output out of the context.

  • Turn it on for repo-wide renames and config bumps, link or import audits, log and JSON triage, and anything where you would otherwise watch the agent read twenty files one at a time.
  • Leave it off for short interactive edits, where the wmedia run suggests you trade seconds for tokens, and for any session you need to resume from VS Code, Desktop or a phone.
  • Leave it off in auto mode on sensitive repositories unless you have ask rules or hooks for the destructive commands, because the classifier no longer sees each command’s final arguments.
  • On Codex, treat it as a preview. The flag is real and the release notes show steady work on it, but OpenAI labels it under development.

If tokens are the reason you are looking at Code Mode, it is one lever among several. The token-saving tools roundup for Claude Code and what Claude Code costs per task cover the others.

How do you turn on Code Mode?

Claude Code:

  1. Run claude --version and update with claude update if you are below v2.1.323.
  2. Start a session with the variable set: CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1 claude. To keep it on, put it in the env block of ~/.claude/settings.json. A project’s own settings file can’t turn it on.
  3. Select Yes, turn on Code Mode at the prompt. The session header then reads “Code Mode (experimental) is on.”
  4. Give it a task with many related tool calls, then press Ctrl+O and expand a Code row to read the script.
  5. To turn it off, remove the variable or set it to 0. Administrators can block it for everyone with "CLAUDE_CODE_EXPERIMENTAL_CODE_MODE": "0" in managed settings.

You need a Claude subscription or Console account, one of Opus 5.5, Sonnet 5.5, Haiku 5.5 or Fable 5.2, and the native build in a terminal. The documentation still lists Amazon Bedrock, Google Cloud and Microsoft Foundry as unsupported, but the v2.1.331 changelog of 23 November 2026 added “Code Mode support in terminal sessions on Amazon Bedrock, Google Cloud’s Agent Platform, Microsoft Foundry and Claude Platform on AWS.”

Codex CLI: add this to ~/.codex/config.toml and start a new session. The configuration reference describes the flag as “under development and off by default,” so expect changes between releases.

[features.code_mode]
enabled = true

Common questions

Frequently asked questions

What is the difference between Code Mode and normal tool calling?
With normal tool calling the model requests one tool, reads the full result and then picks the next tool, one model turn per call. With Code Mode the model writes a JavaScript program that makes several tool calls, can run them in parallel and passes back only the output it needs.
Does Claude Code Code Mode make Claude faster?
Not necessarily. The one public test, on Claude Code v2.1.323 with Opus 5.5, took 37 seconds with Code Mode against 15 without, while using about 37% fewer input tokens. Anthropic has published no speed or token benchmark for the feature.
Does Codex have Code Mode?
Yes. Codex CLI added an experimental code mode in version 0.114.0 on 11 March 2026. It is turned on with enabled = true under [features.code_mode] in config.toml, runs each script in a fresh V8 isolate with no filesystem or network access, and is documented as under development and off by default.
Is Code Mode the same as programmatic tool calling?
They share the idea but not the product. Programmatic tool calling is a stable Claude API feature in which Claude writes Python that runs in an Anthropic-hosted container. Code Mode in Claude Code has Claude write JavaScript that runs in your local session.

Sources

Anthropic (2026). Run tool calls from code with Code Mode. Claude Code documentation. Vendor documentation. Source of the mechanism, example, requirements, limitations and permission behaviour. https://code.claude.com/docs/en/code-mode Verified 24 November 2026.

Anthropic (2026). Claude Code changelog, versions 2.1.323 (12 November 2026) to 2.1.331 (23 November 2026). Vendor release notes; dates from the npm registry. https://github.com/anthropics/claude-code/blob/main/CHANGELOG.md Verified 24 November 2026.

wmedia.es (2026). Code Mode in Claude Code: turn it on and every request costs fewer tokens. 13 November 2026, updated 24 November 2026. Independent hands-on test, one run per condition, not a benchmark. https://wmedia.es/en/tips/claude-code-code-mode-turn-on Verified 24 November 2026.

OpenAI (2026). codex-cli 0.114.0 release notes. 11 March 2026. Vendor release notes. https://github.com/openai/codex/releases/tag/rust-v0.114.0 Verified 24 November 2026.

OpenAI (2026). Configuration reference and Hooks. Codex documentation. Vendor documentation. Source of the code_mode flag status and nested hook behaviour. https://developers.openai.com/codex/config-reference Verified 24 November 2026.

OpenAI (2026). codex-rs/code-mode-protocol/src/description.rs and codex-rs/features/src/lib.rs. openai/codex source code, main branch. Source of the V8 isolate description, helper list and feature stage. https://github.com/openai/codex Verified 24 November 2026.

Wu, B. et al. (2025). Introducing advanced tool use on the Claude Developer Platform. Anthropic engineering blog, 24 November 2025. Vendor research. Source of the 37% programmatic tool calling figure. https://www.anthropic.com/engineering/advanced-tool-use Verified 24 November 2026.

Jones, A. and Kelly, C. (2025). Code execution with MCP: Building more efficient agents. Anthropic engineering blog, 4 November 2025. Vendor research. Source of the 98.7% figure. https://www.anthropic.com/engineering/code-execution-with-mcp Verified 24 November 2026.

Anthropic (2026). Programmatic tool calling. Claude Platform documentation. Vendor documentation. Source of the 11% and 24% BrowseComp and DeepSearchQA figures. https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling Verified 24 November 2026.

Varda, K. and Pai, S. (2025). Code Mode: the better way to use MCP. Cloudflare blog, 26 September 2025. Vendor essay, no benchmarks. https://blog.cloudflare.com/code-mode/ Verified 24 November 2026.

Get each breakdown before it makes the rounds

You get one email when a new source-backed analysis goes live: what AI agents actually cost, which models are worth running, and what the benchmarks really mean. No hype.

No spam. Unsubscribe anytime.

← Back to Coding agents