Code Mode in Claude Code: writing JS to chain tool calls, from 150,000 to 2,000 tokens
🔎 Anthropic changes how its agents call their tools
On November 17, 2026, Anthropic shipped version 2.1.323 of Claude Code with a subtle but potentially game-changing addition: Code Mode. An experimental, opt-in feature that radically changes the agent's internal mechanics. Instead of executing MCP tool calls one by one via classic function calling, Claude writes JavaScript that chains multiple calls in a single step.
The idea comes directly from Anthropic's research article on code execution with MCP, published earlier. The number that sums it all up: an agentic workflow that consumed 150,000 tokens with the classic approach drops to 2,000 tokens with the "code-as-action" approach. That's a 98.7% saving, in both time and cost.
Why now? Because agents have become insatiable context consumers. Every tool definition loaded into the prompt, every intermediate result sent back to the model, every reasoning step between two calls: all of it is billed in tokens and latency. At the scale of an agent running hundreds of times a day, the bill explodes. Code Mode is Anthropic's answer to this problem — and a strong signal about the direction agentic engineering is taking.
Key takeaways
- Code Mode is available in Claude Code v2.1.323 (November 17, 2026) as an opt-in experimental feature, enabled via an environment variable.
- The principle: instead of loading all MCP tool definitions into the context and sending every intermediate result back to the model, Claude writes JavaScript that calls the tools and only returns the useful final output.
- Savings measured by Anthropic: from 150,000 tokens down to 2,000 tokens on an example MCP workflow, i.e. 98.7% savings in time and cost (source).
- Three key mechanisms: the code only exposes the tools that are actually needed, intermediate results stay within the script's scope (not in the model's context), and control-flow steps (loops, conditionals) are executed by the runtime, not by the LLM.
- Activation:
CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1 claude— this is explicitly experimental; don't put it into critical production without guardrails.
Recommended Tools
| Tool | Primary use | Price (November 2026) | Best for |
|---|---|---|---|
| Claude Code | Coding agent with experimental Code Mode | Included in Claude subscriptions, check anthropic.com | Developers chaining MCP tools |
| Claude Opus 4.7 (Adaptive) | Complex agentic reasoning | Check anthropic.com | Multi-step workflows with many tools |
| Claude Sonnet 4.6 | Fast, economical agent | Check anthropic.com | High-volume agentic tasks |
To choose the right model for your agentic workflows, our comparison of the best LLMs for coding details the trade-offs between reasoning, latency, and cost. And if you're exploring the broader ecosystem, our guide to the best AI tools for coding positions Claude Code against Cursor, Copilot, and Cline.
What is Code Mode, exactly?
Code Mode is a Claude Code execution mode where the model writes JavaScript to call its tools, rather than calling them one by one via standard function calling. Claude Code executes this code within the session, and only the useful result is returned to the model.
The feature was added on November 17, 2026 in v2.1.323 as an opt-in experimental feature, as reported by Marktechpost. The official documentation on code.claude.com lays out the terms: opt-in, experimental, enabled via an environment variable.
In practice, the difference is invisible to the user — Claude keeps responding normally. But under the hood, the mechanics change completely:
- Classic function calling: the model sees all the tool definitions, issues a call, waits for the result in its context, reasons, issues the next call. Every step consumes tokens.
- Code Mode: the model writes a script that chains the calls together. The runtime executes the script. Intermediate results pass between the tools without ever going back through the model's context. Only the final result is returned.
It's an inversion of responsibility: the LLM decides what to do, the code decides how to do it efficiently.
Activation
A single environment variable is enough:
CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1 claude
No interface, no complex configuration. It's the classic hallmark of a feature that Anthropic is testing in real-world conditions before deciding whether to roll it out more broadly.
The real problem: MCP is expensive in tokens
The Model Context Protocol solves an interoperability problem but creates an efficiency one: the more tools you connect, the more your context fills up before the first question is even asked.
Anthropic's research article breaks the waste down into three layers:
1. Tool definitions. Each MCP server exposes its tools along with their schemas. An agent connected to ten servers can carry tens of thousands of tokens of definitions — loaded with every request, even if only one tool will end up being used.
2. Intermediate results. A typical workflow chains calls together: listing files, reading their contents, extracting data, calling an API. In the classic approach, each intermediate result is returned into the model's context, even when it only serves to prepare the next call. A 10,000-token file that needs to be filtered consumes 10,000 tokens before the filtering happens.
3. The reasoning between steps. Between two tool calls, the model "talks": it plans, rephrases, comments. This transition text is token noise that produces no value.
The result is documented with figures to back it up: 150,000 tokens for a workflow that requires 2,000 with the code-as-action approach. This isn't a marginal optimization, it's a change of order of magnitude. At the scale of a team running agents continuously, it's the difference between a viable bill and an absurd one.
Why the "code-as-action" approach wins
Code-as-action moves control logic from the model to the runtime, and it's this shift that produces the gains. Anthropic identifies three specific mechanisms.
Progressive tool filtering
When Claude writes code, it only needs to see the tools it actually uses. The script acts as an explicit contract: if the workflow only requires three tools out of the forty available, the other thirty-seven don't pollute the context. In classic function calling, the model carries the entire catalog at all times.
Data stays in scope
This is the most elegant point. In a script, the result of a call can be stored in a variable, filtered, transformed, then passed to the next call — without ever entering the model's context. The model wrote the logic once; the runtime executes it with the real data. Only the final output, condensed and relevant, makes its way back up to the LLM.
This is exactly the mechanism that takes a workflow from 150,000 down to 2,000 tokens: the 148,000 tokens saved were data transit, not reasoning.
Control flow becomes deterministic
Loops, conditionals, error handling: in classic function calling, every iteration of a loop is a decision made by the model, with its token cost and its risk of inconsistency. In code-as-action, for and if are executed by the runtime. Zero tokens, zero possible hallucination in the control logic. The model only decides one thing: the structure of the script.
This trajectory aligns with a broader trend in agentic engineering, which we already analyzed in our article on the MIT/Berkeley/Georgia Tech preprint on evolvable infrastructure and agent latency: agent performance increasingly hinges on orchestration, not just on the model.
What this changes in practice for developers
Code Mode transforms the way you structure your MCP workflows — provided you accept a mindset shift.
Parallel calls become natural
In JavaScript, launching three independent calls in parallel is trivial (Promise.all). With classic function calling, the model must explicitly decide to parallelize, and many implementations serialize by default. For a workflow that reads multiple files or queries multiple APIs, the latency gain is immediate.
Results flow between calls
The typical example: fetch data from an API, filter it, then send the filtered result to a second tool. In classic mode, each step loads the entire dataset into the context. In Code Mode, the filtering happens within the script. Anthropic illustrates this pattern with a concrete case: using code to lift a retry limit on a tool call — control logic that, in classic mode, would have cost full round trips between the model and the tool.
Debugging takes on a new form
When something breaks, you're no longer debugging a conversation with a model but a JavaScript script. This is both good news (familiar tools, stack traces, step-by-step execution) and a shift in posture: you're reviewing code generated by the model, with all the vigilance that implies.
For those discovering the agentic ecosystem without going through code, our guide on creating an AI chatbot without writing a single line of code remains a valid entry point — but Code Mode illustrates precisely the boundary: beyond a certain level of complexity, no-code approaches reach their limits, a topic we cover in depth in our no-code vs code comparison.
Anthropic Is Playing on Multiple Fronts at Once
Code Mode doesn't come out of nowhere. It is the product manifestation of Anthropic's research on code-as-action, and it fits into a broader agentic strategy. A few weeks earlier, Anthropic had opened up Claude Managed Agents in self-hosted mode, completing its agentic stack — managed orchestration on one side, efficient tool execution on the other.
The logic is coherent: Anthropic is building a complete stack where the model (Claude Opus 4.7, Claude Sonnet 4.6), the orchestration (Managed Agents), and the execution (Code Mode) are optimized together. That's a competitive advantage that is hard to replicate for players who control only one layer.
The comparison with other code-first approaches to tool calling, detailed by Marktechpost, shows that Anthropic is not alone on this path — but it is the first major vendor to integrate it directly into its mainstream development tool rather than into a separate framework.
My take, no beating around the bush: this is the right direction. Classic function calling was an excellent scaffold, but it conflates "the model decides" with "the model executes". Separating the two is the natural maturation of the field.
Limitations and precautions: what Code Mode doesn't solve
Code Mode is experimental, and it should be treated as such.
The security of executing generated code
Having an agent execute JavaScript means giving it arbitrary execution power. Anthropic executes the code within the Claude Code session, which limits the attack surface compared to full system execution — but an agent that writes code calling tools is still an agent with permissions. In enterprise environments, the question of sandboxing and per-tool permissions must be taken seriously before any deployment.
Readability and auditability
A classic function calling workflow leaves a readable conversational trace: every call is visible, every result is in the history. A Code Mode workflow compacts multiple calls into a script. For auditing and debugging, it's more efficient — but fine-grained traceability of individual calls requires dedicated tools.
The cost of code generation
Writing a script costs output tokens, which are generally more expensive than input tokens. For simple workflows — two sequential calls, no data transformation — classic function calling may remain more efficient. Code Mode shines on complex and repetitive workflows; it adds nothing for trivialities.
The experimental status
The documentation is clear: opt-in feature, experimental. The API and behavior may change. Don't build a critical production pipeline on it without a fallback plan to classic mode.
❌ Common Mistakes
Mistake 1: Enabling Code Mode everywhere by default
It's an opt-in experimental feature for a reason. Enabling it systematically on all your projects exposes your sessions to untested generated code, with no benefit for simple workflows.
The solution: enable it on projects that use MCP intensively — multiple servers, multi-step workflows, data transformations. Measure token consumption before/after on a real use case before generalizing.
Mistake 2: Believing token savings replace tool optimization
The 98.7% savings come from one specific example of a heavy MCP workflow. Your mileage will vary — radically. A workflow with two lightweight calls will see no gain; a workflow that passes large files between tools will see massive gains.
The solution: profile your workflows. Identify the ones where intermediate results dominate context consumption — that's where Code Mode pays off.
Mistake 3: Confusing "fewer tokens" with "less oversight"
Reducing context from 150,000 to 2,000 tokens also means you see less of what the agent is doing. Intermediate results no longer surface. If a tool returns corrupted data in the middle of the workflow, the script can propagate it without anything alerting you.
The solution: add explicit validations to your critical workflows — schema checks, guardrails on extreme values — rather than relying on the model to detect anomalies on the fly.
Mistake 4: Ignoring the version
Code Mode ships with v2.1.323. On an earlier version, the environment variable does nothing — or worse, behaves differently depending on your installation.
The solution: check your Claude Code version before testing, and consult the official release notes for any changes.
❓ Frequently Asked Questions
Is Code Mode available to all Claude Code users?
Yes, but as an experimental opt-in. You need version 2.1.323 (November 17, 2026) or later and must enable the environment variable CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1 at launch. Without activation, Claude Code works with classic function calling, with no changes.
Do you need to know how to program in JavaScript to use Code Mode?
Not for basic usage: Claude writes the code, not you. But understanding JavaScript helps you review the generated scripts, debug when things break, and add safeguards. The more critical your workflow, the more this skill matters.
Does the 98.7% saving apply to all workflows?
No. The figure of 150,000 down to 2,000 tokens comes from a specific example documented by Anthropic on a heavy MCP workflow. The gains depend on the volume of data passing between tools and the number of steps. On simple workflows, the gain can be zero or even negative, since generating code costs output tokens.
Does Code Mode replace classic function calling?
Not yet. Both modes coexist in Claude Code, and Code Mode remains experimental. The trajectory suggested by Anthropic's research indicates that the code-as-action approach will likely become dominant for complex workflows, but function calling retains the advantage for simple, isolated calls.
Does Code Mode work with any MCP server?
Yes, that's the whole point: Code Mode applies to MCP tools already connected to your session. No server modification is needed — it's the calling layer that changes, not the tools themselves. Just check compatibility in the official documentation for special cases.
What's the difference with existing code-first agentic frameworks?
The difference is integration. Existing code-first approaches live in separate frameworks that you have to adopt. Code Mode is natively integrated into Claude Code, enabled by an environment variable, with the runtime managed by the tool. It's less flexible than a dedicated framework, but far more accessible.
✅ Conclusion
Code Mode is the first mass-market product implementation of Anthropic's code-as-action approach, and its headline figure — 150,000 tokens reduced to 2,000 — says it all: the future of agents isn't in bigger models, but in architectures that waste less. Try it as an opt-in on your heaviest MCP workflows, measure the results, and keep classic mode for the rest. To dig deeper into choosing the models that power your agents, check out our comparison of the best LLMs for coding.