# 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.

Source: https://capitalandcompute.net/blog/code-mode-vs-direct-tool-calls-claude-code-codex/
Published: 2026-11-24
Updated: 2026-11-26

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.

Key takeaways

* Code Mode replaces one-tool-per-turn calling with a script the model writes, which can chain, parallelise and filter tool calls before any output reaches the model.
* Claude Code: experimental, off by default, v2.1.323 or later, turned on with CLAUDE\_CODE\_EXPERIMENTAL\_CODE\_MODE=1, terminal sessions only.
* Codex: a code\_mode feature flag since codex-cli 0.114.0 (11 March 2026), documented as under development and off by default.
* One independent run measured 37% fewer input tokens and a 2.5x longer wall time, with the same number of model requests. Anthropic has published no Code Mode benchmark.
* The 37% and 98.7% figures that circulate with Code Mode come from earlier Anthropic API and MCP research, not from Claude Code.

## 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](https://code.claude.com/docs/en/code-mode) 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](https://wmedia.es/en/tips/claude-code-code-mode-turn-on), 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 it__
| Metric                     | With Code Mode as a multiple of Without Code Mode | Values            |
| -------------------------- | ------------------------------------------------- | ----------------- |
| System tools tokens        | 0.45x                                             | 11.1k vs 24.6k    |
| First-request input tokens | 0.61x                                             | 22,233 vs 36,404  |
| Total input tokens         | 0.63x                                             | 94,659 vs 150,364 |
| Output tokens              | 1.03x                                             | 1,213 vs 1,172    |
| Wall time                  | 2.47x                                             | 37 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)](https://wmedia.es/en/tips/claude-code-code-mode-turn-on)

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_](https://www.anthropic.com/engineering/advanced-tool-use), 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_](https://www.anthropic.com/engineering/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](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling) 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](/blog/ai-coding-agent-token-usage/).

## 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_](https://blog.cloudflare.com/code-mode/) 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](https://code.claude.com/docs/en/code-mode) and [changelog](https://github.com/anthropics/claude-code/blob/main/CHANGELOG.md); the [Codex 0.114.0 release notes](https://github.com/openai/codex/releases/tag/rust-v0.114.0), the [Codex configuration reference](https://developers.openai.com/codex/config-reference) and the tool description in [code-mode-protocol/src/description.rs](https://github.com/openai/codex/blob/main/codex-rs/code-mode-protocol/src/description.rs); this site’s [OpenCode 2.0 breakdown](/blog/opencode-2-0-what-changed/); 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.

What Code Mode turns off in Claude Code

A Code Mode session can’t use Remote Control, Claude in Chrome or computer use, can’t move to the Desktop app or become a background session, and resumes only in the terminal with Code Mode still on. The VS Code panel, Desktop app and cloud sessions don’t support it. Code that Claude waits on stops after ten minutes; background code can run for up to two hours if the session started in auto or bypassPermissions mode.

## 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](/blog/claude-code-token-saving-tools-rtk-headroom-caveman/) and [what Claude Code costs per task](/blog/claude-code-pricing-cost-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.
