Claude Code's Experimental "Code Mode" Saves a Huge Amount of Tokens
Claude Code's experimental Code Mode lets the agent orchestrate tool calls through a script instead of calling them one by one. Learn how it works, how to enable it, and why it can cut token consumption significantly—without sacrificing task quality.
A Real-World Scenario
When using an AI coding assistant in the terminal, we often ask it to handle compound tasks. For example: “Search the source code for the config loading logic and summarize it in a brief.” The assistant would then call a whole series of tools in sequence: a text search tool to find keywords, a file-matching tool to locate relevant files, a file-reading tool to view contents, and a command-line tool to run statistics or verification commands.
I ran a small experiment: I had the assistant complete the same task twice. The first run, with an experimental feature disabled, consumed roughly 10,000 tokens; the second run, with it enabled, used only about 6,000. The only variable between the two runs was the experimental Code Mode.
What exactly does it change? Let’s break it down across three aspects: how it works, how to enable it, and the actual benefits.
The Problem with the Default Mode: The Hidden Cost of Sequential Calls
One Tool per Turn
By default, a coding assistant works in a “serial, single-step” fashion. Every tool call goes through a full round:
- Read the entire accumulated conversation context;
- Decide on and invoke a single tool;
- Read the tool’s returned result;
- Append the result to the context;
- Move to the next step—which re-reads the whole context again.
A single round’s overhead seems minor. The problem is that compound tasks often require many consecutive query rounds, and two costs compound quickly.
Where the Cost Comes From
The first is repeated reading. At every step, the model has to process all previously accumulated content, including earlier conversation and prior tool outputs. The context grows like a snowball, and token consumption grows cumulatively.
The second is hauling large chunks of tool output. For example, a search command might return hundreds of lines of matches, all of which enter the context verbatim—even though only a few lines are actually useful for subsequent reasoning. The more round trips, the worse this “transport waste” becomes.
Third, serial execution also slows things down. Multiple independent queries could run in parallel, but in a sequential-call mode they can only queue up and wait.
How Code Mode Works: Delegating Orchestration to Code
The Core Idea
With Code Mode enabled, the assistant’s execution strategy shifts in one key way: instead of calling tools itself one at a time, it first writes a small script (usually JavaScript) and hands a batch of tool calls to that program to execute.
// Illustrative: batch-dispatch multiple independent queries in a scriptconst [files, matches] = await Promise.all([glob("**/config/*.ts"),grep("loadConfig", { type: "ts" }),]);// Pick only what's actually needed downstreamconst picked = matches.filter(m => m.score > 0.8);return picked;
This illustrative code highlights three key points:
- Parallel execution: independent calls can be issued simultaneously, eliminating serial waiting;
- Result preprocessing: the script processes tool results locally, filtering out just the useful parts of large outputs;
- On-demand handoff: only the distilled, essential information is returned to the model and enters the context.
The Essence of the Savings
What gets saved is mainly two kinds of overhead: the processing cost of repeated round trips, and the context space consumed by large tool outputs. The direct effects are:
- Noticeably lower token consumption—the context is leaner, so each round processes less content;
- More room in the context window—the same session can accommodate longer task chains;
- Some tasks run faster—parallel calls reduce total waiting time.
That said, the “bonus speedup” doesn’t always materialize. If a task is mostly model reasoning with few tool calls, Code Mode offers limited benefit. Its real value lies in exploratory tasks that require multiple consecutive queries with large tool outputs—code research, cross-file retrieval, batch information aggregation, and the like.
How to Enable It
This is an experimental feature enabled via an environment variable. Edit your user-level settings file (typically ~/.claude/settings.json, or the equivalent location for your tool) and add the following to the env field:
{"env": {"CLAUDE_CODE_EXPERIMENTAL_CODE_MODE": "1"}}
If the file already has an env section, just merge this entry in—no other changes are needed.
If you’d rather not edit the file manually, you can simply instruct the assistant in a session: “Please add this entry to the env section of my settings file to enable Code Mode for terminal sessions.” The assistant will make the change for you, and you’ll see a confirmation once it’s done.
The next time you start a session, keep an eye on the terminal output—the new session will display a warning indicating that the experimental Code Mode is active. Since it’s experimental, it’s wise to try it first on routine, low-risk tasks, monitor changes in token usage and task quality, and then decide whether to keep it enabled long term.
Final Thoughts
Code Mode embodies a general principle in agent design: let deterministic program logic handle mechanical orchestration and filtering, and reserve the model’s capabilities for genuine reasoning and decision-making. For tasks like tool-call orchestration and result filtering, a script is far more efficient than turn-by-turn dialogue. This idea isn’t limited to any specific tool—it’s a direction worth considering for any team building agent workflows. Moving from “asking the model at every step” toward “model orchestrates, code executes” is likely to be one of the important trends in the evolution of AI coding tools.
