Code Mode changes how Claude uses its tools. Without Code Mode, Claude asks for tool calls such as Bash, Read, or Edit directly and reads each result before it decides what to do next. With Code Mode, Claude writes a piece of JavaScript that makes the tool calls, and Claude Code runs that code in your session.
How Code Mode works
In a Code Mode session, you still describe your task in a prompt, and Claude writes JavaScript to carry it out. 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. For example, if you ask Claude to raise a retry limit, Claude might write this code to find the file, edit it, and run the tests. The last line returns the file’s path and the test output to Claude:
In the conversation, you see the tool calls, such as the edit’s diff, and not the code. To read the code, press Ctrl+O to open the transcript viewer. Each piece of code appears there as a collapsed Code row, with a hint for how to expand it.
When to use Code Mode
Try Code Mode on tasks that take many related tool calls, such as:
- A change that repeats across many files
- A search whose results need filtering before Claude reads them
- Several independent commands that can run at the same time
Code Mode limitations
Before you turn on Code Mode, check that you can accept these limits:
- Resuming: you can resume a Code Mode session only in the terminal, with Code Mode on
- Auto mode: auto mode’s classifier reviews the code, not each tool call
- Long-running code: unless code can run in the background, you wait for it to finish, and it stops after ten minutes
- Hooks: you can see tool calls refused when a
PreToolUsehook doesn’t finish, for example after a timeout - Unavailable features: you can’t use Remote Control, Claude in Chrome, or computer use
- Moving the session: you can’t move the session to the Desktop app or turn it into a background session, and
/forkcan’t start a background copy
Requirements
- Version: Claude Code v2.1.323 or later, on either release channel. An older version starts without Code Mode, with or without the reason
it isn't open to your account yet - Account: a Claude subscription or Console account. You can’t use Code Mode through a gateway, Amazon Bedrock, Claude Platform on AWS, Google Cloud’s Agent Platform, Microsoft Foundry, or Claude Models on Google Cloud (Anthropic managed)
- Organization: your organization doesn’t use the HIPAA configuration or zero data retention
- Model: Claude Opus 5.5, Sonnet 5.5, Haiku 5.5, or Fable 5.2
- Surface: an interactive
claudesession in a terminal. Code Mode doesn’t work with the VS Code panel, the Desktop app, or cloud sessions - Agent teams: to use Code Mode with agent teams on, you need v2.1.326 or later. Teammates run in Code Mode in the default in-process display mode, and start without it in split panes
- Telemetry settings: to use Code Mode with feature-flag fetching off or OpenTelemetry events and metrics on, you need v2.1.329 or later. Keep tracing off on any version
- Installation: the native build of Claude Code
- Windows: Git for Windows is installed
You also can’t start Code Mode with some settings on, such as an API key helper. For the fix for each, see Code Mode doesn’t start.
Try Code Mode
Turn on Code Mode, give Claude a task, and look at the code that Claude wrote.
Turn off Code Mode
Remove CLAUDE_CODE_EXPERIMENTAL_CODE_MODE from your shell and from ~/.claude/settings.json, then start a new session. You can also keep the variable and change its value to 0 in either place. With a 0 in one place, Code Mode stays off even if you set 1 in the other.
Block Code Mode for your organization
Add CLAUDE_CODE_EXPERIMENTAL_CODE_MODE with the value 0 to your managed settings so that nobody in your organization can use Code Mode:
Users who are in a Code Mode session stay in it until they exit or restart Claude Code. After that, your users start without Code Mode and can’t resume their Code Mode sessions while the 0 is in place. You can’t turn on Code Mode for your users from managed settings.
Monitor Code Mode with OpenTelemetry
If you collect OpenTelemetry events, your backend receives tool_decision and tool_result events for the tool calls in the code that Claude writes, under each tool’s usual name, such as Bash. Update your queries and settings for these calls:
- Find calls from code: filter on
tool_use_idvalues that start withrepl_ortoolu_cc_. TheREPLtool, which runs the code, has events of its own without either prefix - Read classifier verdicts: in auto mode, a call that the classifier’s review of the code covers has no
classifier_verdict. Look for the verdict on theREPLtool’stool_decisionevents that have that call’srequest_id. If you find more than one, the events don’t show which piece of code made the call - Keep secrets out of your backend: code can read a value, such as a token in a file, and pass it to another call. Leave
OTEL_LOG_TOOL_DETAILSunset if your backend shouldn’t receive those values
Permissions and long-running code
Your permission mode decides how the code that Claude writes is checked and whether you can talk to Claude while the code runs.
Permission checks
In auto and bypassPermissions modes, code can run from start to finish without a permission prompt. Some calls still prompt you, such as the actions no mode auto-approves.
Each tool call in the code goes through your deny and ask permission rules and PreToolUse hooks under the tool’s usual name, such as Bash or an MCP tool’s name. Beyond rules and hooks, a classifier reviews the code in auto mode, and you answer prompts in the modes that ask:
- Auto mode: the classifier reviews each piece of code before it runs, not each tool call. It doesn’t see values computed at run time, such as a path taken from search output, so keep rules or hooks for calls you must control. You can still be prompted for some calls, such as calls in code that you approved yourself after repeated classifier blocks
- Other permission modes: you’re prompted for each tool call that would prompt you in that mode
If you have a PreToolUse hook that allows or blocks shell commands, make it check each part of a command, as Bash rules do. In Code Mode, a git push in a folder named app can reach your hook as cd 'app' && git push.
To check the code itself, write a PreToolUse hook that matches REPL, the tool that runs the code, and read the JavaScript from tool_input.code. To check what the code returns, see Check code output with a hook.
Require approval for a command
To see a prompt for a specific command in auto or bypassPermissions mode, add an ask rule for it. You’re prompted for the forms of the command that the rule matches. To be prompted for commands that start with git push, save this rule in ~/.claude/settings.json:
To test the rule, start a new session in auto mode in a repository with no remote, and ask Claude to run git push origin main. You see a prompt for that command.
Approve each piece of code first
To read and approve code before it starts, add an ask rule for REPL, the tool that runs the code. Save this rule in ~/.claude/settings.json:
To test the rule, start a new session and give Claude a task. You see a prompt that doesn’t include the code. To read the code before you answer, press Ctrl+O, expand the Code row, and press Ctrl+O again to return. From the main conversation, you don’t see a subagent’s code in that view.
If you approve in auto mode, the code starts even when the classifier would have blocked it, and its project edits and read-only commands run without a prompt. If Claude reports that the code can’t start a subagent, remove the rule.
Answer a permission prompt while code runs
To stop the code at a prompt for one of its tool calls, answer No without a comment. These results apply to prompts that aren’t labeled as coming from a subagent:
- No, or
Esc: the code stops, and changes from calls that already finished stay in place - No, with a comment: the call is refused, and the code can continue with its other calls
Limit shell commands with the sandbox
Your sandbox settings apply to the shell commands that code runs. The JavaScript itself and its file, web, and MCP tool calls run outside the sandbox. To isolate the JavaScript and those calls too, run Claude Code in a container or virtual machine. To keep commands in the sandbox when code requests to run them unsandboxed, turn on strict sandbox mode. In auto mode, the classifier reviews the code that makes the request, and the command has no separate review.
Long-running code
While code runs a long build or test, Claude either waits for it or lets it continue in the background:
- Waiting: Claude answers after the code finishes, and the code stops after ten minutes. In Manual and
acceptEditsmodes, time you spend at a tool’s permission prompt doesn’t count toward the ten minutes. Before v2.1.328, that time counted. - Background: you can keep talking to Claude, and the code can run for up to two hours
For background code, start your session in auto or bypassPermissions mode. If Claude still waits, check your current mode and how you started the session:
- You switched to another mode: press
Shift+Tabuntil you’re back in the mode you started in - You started in another mode: if auto mode is available to you, exit, then run
claude --continue --permission-mode autowith Code Mode on - You’re on a version before v2.1.328 and picked the session from the
claude --resumelist: exit, update Claude Code, then run the sameclaude --resumecommand with Code Mode on. To stay on your version, replace--resumewith--continue - You passed tool names to
--tools: start again in auto mode without that flag
Stop code in progress
To stop code while Claude is responding or waiting for the code, press Esc. To stop background code after Claude finishes responding, run /tasks in the session, select the running task, and press x.
After you stop the code, check /tasks for subagents, shell commands, and MCP calls that the code started and that are still running. To stop one, select it and press x.
Check code output with a hook
To check or redact what the code returns, write a PostToolUse hook that matches REPL, the tool that runs the code. The same hook runs when background code finishes and when code throws an error.
Read code output in the hook
In your PostToolUse hook, read the output from tool_response, which has fields such as these:
Redact code output
To hide part of the output, return a changed copy of tool_response as updatedToolOutput, and build the copy this way:
- Change the text: redact
result,stdout,stderr, anderror. Handle aresultthat’s missing ornull, and expecterroronly when the code failed or was stopped - Keep the shape: return
code,stdout, andstderras strings, and returnerroras a string or leave it out. If you break this shape, Claude gets the original output - Remove images and PDFs: if the copy has
imagesordocumentsto hide, set that field to[]
To hide the deploy token in the notes.txt result, print this JSON:
To test the hook, create a file that contains a value your hook redacts. Then start a Code Mode session, and ask Claude to read and quote the file. Type the path without @, and check that Claude’s reply shows your replacement text.
Redact when your hook blocks or hits an error
Return a changed copy of tool_response as updatedToolOutput even when your hook blocks or hits an error:
- Your hook blocks: print the redacted copy in the same JSON as
"decision": "block". Without the copy, Claude still reads the original output - Your hook hits an error: catch the error, and print a copy with placeholder text in the fields that you redact. If the hook crashes, times out, or prints invalid JSON, Claude gets the original output
Move a check from a UserPromptSubmit hook
If you check code output in a UserPromptSubmit hook, move that check to your PostToolUse hook. On v2.1.328 or later, UserPromptSubmit hooks don’t receive code output, even when an administrator sets them in managed settings.
Troubleshooting
Code Mode doesn’t start
Code Mode (experimental) isn't available near the top of the session means that Claude Code started without Code Mode. The reason follows on the same line, and /status shows it too. Find the reason in this table, make the change, and start a new session. For a reason that isn’t listed, check the requirements.
If you see no line about Code Mode, run claude --version. If your version is older than v2.1.323, update. Then check that CLAUDE_CODE_EXPERIMENTAL_CODE_MODE is 1 in your shell or user settings, and remove any 0 from those and from any --settings file.
Permission prompt with a countdown
In auto mode, if you approve code yourself after repeated classifier blocks, you can be asked about tool calls in that code and in earlier code that’s still running. With no IDE connected, the prompt can show a countdown:
Answer before the countdown ends. Otherwise that call is refused, and later calls that would show a countdown are refused without a prompt. To see the prompts again, send Claude a message. Before v2.1.327, you could also be asked about the remaining calls in running code after Claude started new code, even if the classifier allowed the new code. To see fewer of these prompts, update Claude Code to v2.1.327 or later.
Tool call refused by a hook
If Claude reports a refused tool call, ask it for the error text. These errors mean that your PreToolUse hook didn’t give a decision:
the hook did not finish: your hook timed out, or your agent hook ended without a decision. After a timeout, raise the hook’stimeout. For an agent hook that gives no decision, move the check to acommandhook. You can see this error on calls from code, and on other tool calls after Claude runs code in the sessionthe hook failed: on a call from code, your hook couldn’t start, was killed, exited with 126, 127, or a code above 128, or returned an HTTP error or invalid output. Fix the hook. You can see this error before v2.1.329. On later versions it’s rare: one cause is feature-flag fetching turning off during a session, after Claude has run code
On v2.1.329 or later, expect a call from code to run by default after a hook failure other than the hook did not finish. To choose whether the call runs, use a command or http hook and set onFailure on it:
- To stop the call after a timeout or failure: set
"onFailure": "block". Do this for a hook that guards calls - To let the call run after a timeout or failure: set
"onFailure": "continue"
Code output withheld by a hook
If you expected output from code and see UserPromptSubmit operation blocked by hook in Claude’s reply, your UserPromptSubmit hook received the output as a prompt and blocked it. If Claude reports only the hook’s reason, ask it to quote the message it received.
On v2.1.328 or later, UserPromptSubmit hooks don’t receive code output. To let the output through on an earlier version, update Claude Code or change the hook:
- Update: if you rely on the hook to check code output, first move the check to a
PostToolUsehook. Then update Claude Code to v2.1.328 or later - Stay on your version: change the hook so that it doesn’t block when
sourcein its input ispoll_event
- Permission modes: compare auto mode with the modes that prompt you
- Hooks reference: write
PreToolUseandPostToolUsehooks - Managed settings: apply one configuration across an organization