Pular para o conteúdo
Todos os artigos
TUTORIAL·November 29, 2026·6 MIN DE LEITURA

Claude Code's Code Mode: Run a Safe First Trial Before You Trust a Script to Call Your Tools

Por EndOfCoding

Claude Code has an experimental feature called Code Mode. Normally the model asks for one tool call, such as Bash, Read or Edit, reads the result, then decides what to do next. With Code Mode, Claude instead writes a piece of JavaScript that makes the tool calls, and Claude Code runs that code in your session (official docs). The docs label it experimental and off by default, and say it may change or be removed in any release.

That changes what you are reviewing. A single script can search, read and edit before you see an intermediate result. This tutorial is a safe first trial: turn it on for one session, decide what needs your approval, and know where the sandbox stops. Details below come from the documentation as read on November 29, 2026. Check it again before you rely on them.

What You'll Learn

  • What Code Mode changes about the agent loop
  • The requirements, and how to enable it for a single session
  • How permission rules, auto mode and hooks apply to code
  • Why the shell sandbox does not cover everything
  • How to turn it off for yourself or your organization

Step 1: Check that you can run it

According to the docs, you need:

  • Claude Code v2.1.323 or later. Cloud providers need v2.1.331 or later, and agent teams need v2.1.326 or later. If you use mods that hook tool calls, they need v2.1.327 or later.
  • One of these models: Claude Opus 5.5, Sonnet 5.5, Haiku 5.5 or Fable 5.2.
  • An interactive terminal session. The VS Code panel, the Desktop app and cloud sessions are not supported.
  • The native build. On Windows, Git for Windows must be installed.

It is also unavailable through a gateway, with Claude for Government, or when your organization uses a HIPAA configuration or zero data retention.

Step 2: Enable it for one session only

Start with an environment variable on the command line, so nothing persists.

CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1 claude

In PowerShell:

$env:CLAUDE_CODE_EXPERIMENTAL_CODE_MODE = "1"
claude

The first session asks you to confirm. Look for "Code Mode (experimental) is on" in the session header. The PowerShell variable stays set for that terminal window, so close it or remove the variable when your trial ends. You can also set it in the env block of your user settings file, but the docs say project settings files are ignored for this variable.

Do the trial in a throwaway branch of a project you know well. Also note that the docs say a Code Mode session cannot be resumed with Code Mode off, so finish the experiment before you switch back.

Step 3: Try a bounded task

Pick something with an answer you can check, such as finding every place a retry limit is defined and changing one of them. The documentation's example has this shape: search with a shell command, then edit the chosen file.

const files = await $`rg -l "function retryPolicy" src`.text();
// ...choose the target file, then:
await edit(target, "maxAttempts: 3", "maxAttempts: 5");

You do not write this code. Claude does. Your job is to read it and compare the diff with what you asked for.

Step 4: Decide what needs your approval

This is the part to read twice. The docs say:

  • In auto and bypassPermissions modes, code runs without a prompt, although some calls still prompt you.
  • Permission rules and hooks for individual tools must target those tool names, such as Bash or your MCP tool names.
  • In auto mode, the classifier reviews each piece of code before it runs, not each tool call, and it does not see values computed at run time.

That last point matters. If a script builds a path or a command from something it read a moment earlier, a pre-run review cannot see the final value. Keep deny and ask rules on the tools that can cause damage.

The code itself runs as a tool named REPL. To approve every script while you learn how it behaves, add an ask rule:

{
  "permissions": {
    "ask": ["REPL"]
  }
}

Step 5: Add a hook that reads the script

A PreToolUse hook that matches REPL can inspect tool_input.code before it runs. Use it to log every script, or to block patterns you never want, such as references to credential directories. A text match is easy to get around, so treat it as a tripwire and a log, not as a security boundary.

If your hook is a safety control, decide what happens when it breaks. The docs say a hook that times out leaves the call refused by default, and command and HTTP hooks accept "onFailure": "block" or "continue". Set the value deliberately. We walked through testing this in Your Claude Code Guard Hook Fails Open. If a script's output can contain secrets, a PostToolUse hook on REPL can redact the response using updatedToolOutput.

If you use subagents that restrict their tools and you want them to use Code Mode, add REPL to their tools list, for example tools: Read, Bash, REPL.

Step 6: Know where the sandbox stops

The docs are direct here: the sandbox applies to shell commands only. The JavaScript itself, and its file, web and MCP tool calls, run outside the sandbox. If your safety plan was "the sandbox will contain it", that plan does not cover those calls. A strict sandbox mode is documented; read what it covers before relying on it. For unfamiliar or untrusted code, run the whole session in a container or a disposable virtual machine with no real credentials.

Step 7: Turn it off

Setting the variable to 0 in any settings file overrides a 1. Administrators can disable it for the organization by adding it to the env block of managed settings:

{
  "env": {
    "CLAUDE_CODE_EXPERIMENTAL_CODE_MODE": "0"
  }
}

According to the docs, people already in a session keep Code Mode until they exit.

Common Challenges

  • Missing features. Remote Control, Claude in Chrome and computer use are unavailable in Code Mode, and sessions cannot move to the Desktop app or become background sessions.
  • Long scripts. The docs say long-running code stops after ten minutes unless it is backgrounded, which can extend to two hours in auto or bypassPermissions mode.
  • Review habits. If you are used to approving one command at a time, a whole script is a different thing to read. Slow down on loops, deletes, network calls and anything that writes outside the files you named.
  • Judging the benefit. We have not measured speed or token use for this feature, so we make no claim about either. Run the same small task with and without it and compare the results yourself.

Conclusion

Code Mode moves the unit of review from a single tool call to a short program. That can be a good trade for repetitive search-and-edit work, as long as your controls move with it: an ask rule on REPL while you learn, rules on the dangerous tools, a hook whose failure behavior you chose, and isolation for code you do not trust.

For the wider habit of reviewing what an agent does before you ship it, see the Vibe Coding Ebook. To follow changes to tools like this one, join the Vibe Coding Academy newsletter.

Source: Claude Code documentation: Code Mode

SECOND OPINION · BY VIBE CODING ACADEMY

Your agent says it’s done. What needs checking?

Paste your coding-agent conversation for supported claims, visible problems, and useful next steps. Reviews only the material you provide; no account needed to start.

Review a session