Built-in Tools, Permission Modes and Rules
Claude Code, not the model, enforces what an agent may do. Every tool call passes through a fixed chain (hooks, deny rules, ask rules, permission mode, allow rules, then your canUseTool callback), and a call approved at any step never reaches the later ones. Know the six modes, the rule syntax for Bash, Read/Edit, WebFetch and MCP, what no mode will auto-approve, and where the OS sandbox takes over from text-matching rules.
Key points
- 1
Evaluation order in the SDK:
PreToolUsehooks, deny rules, ask rules, permission mode, allow rules,canUseTool. A hook can deny outright, but itsallowdoes not skip deny or ask rules. Deny rules block even inbypassPermissions; ask rules send the call to the callback even inbypassPermissions. - 2
Modes (
PermissionMode):default(unresolved calls go tocanUseTool),acceptEdits(file edits plusmkdir/touch/rm/rmdir/mv/cp/sedinside the working directories),plan(edits never auto-approved, even with an allow rule or hook allow),dontAsk(anything that would prompt is denied, callback skipped),bypassPermissions(approves everything that reaches the mode step),auto(a classifier reviews shell commands, network requests and similar actions). - 3
Set the mode with
permissionMode/permission_mode, change it mid-session withsetPermissionMode()/set_permission_mode(). TypeScript requiresallowDangerouslySkipPermissions: truefor bypass. In auto mode, switching to an unsupported model drops the session todefault(asystemstatus message), and switching back needs an explicitsetPermissionMode('auto'). - 4
allowedTools/allowed_toolsadds allow rules; it is not a whitelist. Unlisted tools still exist and fall through to the mode, soallowed_tools=['Read']plusbypassPermissionsstill runs Bash. Restrict withdisallowedToolsor thetoolsoption. The locked-down pattern isallowedTools+permissionMode: 'dontAsk'. - 5
Deny rules: a bare name such as
Bash(or a glob like*ormcp__*) removes the tool from the request; a scoped rule such asBash(rm *)keeps the tool and blocks matching calls in every mode. Rules are evaluated deny, then ask, then allow; specificity never lets an allow carve an exception out of a deny. - 6
Bash rule syntax:
*matches any text including spaces; a trailing*that is the only wildcard also matches the bare command (Bash(ls *)matcheslsbut notlsof;Bash(ls*)matcheslsof);:*at the end equals*. Allow rules must match every subcommand of&&,||,;,|chains, while deny and ask rules apply when any subcommand matches. Wrappers liketimeout,nice,nohupare stripped before matching. - 7
Read/Edit rules use gitignore patterns with four anchors:
//pathabsolute,~/pathhome,/pathrelative to the settings source (project root for project settings,~/.claudefor user settings),pathrelative to the current directory.Edit(path)governs Write and NotebookEdit too;Write(path)andGlob(path)rules never match. A Read deny also blocks Edit/Write on that path. - 8
WebFetch rules use
domain:(WebFetch(domain:*.example.com)covers subdomains but not the apex). MCP:mcp__server__tool, ormcp__server__*; allow globs need a literalmcp__<server>__prefix, somcp__*or*in an allow list is ignored.Agent(Explore)style rules control subagents. - 9
canUseTool(toolName, input, options)returns{ behavior: 'allow', updatedInput?, updatedPermissions? }or{ behavior: 'deny', message, interrupt? }(PythonPermissionResultAllow/PermissionResultDeny). It fires only for calls nothing earlier resolved, can stay pending indefinitely, andsuggestionscan be echoed back asupdatedPermissions(alocalSettingsdestination persists to.claude/settings.local.json). Python rejectscan_use_tooltogether withpermission_prompt_tool_name. - 10
No mode auto-approves: explicit ask-rule matches,
AskUserQuestionand MCP tools markedrequiresUserInteraction, organisationaskconnector tools, and recognisedrm/rmdirof critical paths (root, top-level dirs, home, the working directory and its parents). In SDK sessions inautomode those removals are denied without calling the callback. Protected paths (.git,.husky,.vscode, most of.claude,.bashrc,.npmrc,.mcp.json...) are prompted in default/acceptEdits, denied in dontAsk, and allowed only in bypass. - 11
The
sandboxoption (SandboxSettings:enabled,autoAllowBashIfSandboxed,excludedCommands,allowUnsandboxedCommands,network,filesystem) confines Bash, PowerShell and Monitor commands and their children at the OS level. Read/Edit/WebFetch, hooks and local MCP servers run outside it. In TypeScript the option merges into an inlinesettings.sandbox: scalars are replaced, restriction lists such asdenyReadare combined. - 12
additionalDirectories/add_dirsare passed as--add-dir: the directory becomes readable and editable under the same rules, and its skills, commands and subagents load through theprojectsetting source; its CLAUDE.md loads only withCLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD=1, and its hooks and permission rules do not load.
Common traps
Putting audit or policy checks in
canUseTool: anything approved by an allow rule,acceptEditsorbypassPermissionsnever reaches it. Use aPreToolUsehook for checks that must see every call; the SDK warns withCLAUDE_SDK_CAN_USE_TOOL_SHADOWED(PythonCanUseToolShadowedWarning) for bareallowedToolsentries and bypass mode.Treating
Bash(curl *)orRead(./.env)deny rules as a security boundary:/usr/bin/curl,sh -c '...'and a Python script that opens.envall slip past. Enforce with the sandbox (allowedDomains,filesystem.denyRead) or a container.Writing
Read(/secrets/**)in~/.claude/settings.jsonand expecting it to protect every project: in user settings/anchors at~/.claude.Assuming a more specific allow rule beats a broader deny, or that a hook returning
allowskips an ask rule. Neither is true.Expecting an AgentDefinition's
permissionMode: 'plan'to restrain a subagent when the parent runs inbypassPermissions: the subagent inherits bypass. A subagent's own mode applies only when the parent is indefault,dontAskorplan.Relying on CLAUDE.md or
.claudeignoreto keep secrets away: CLAUDE.md is guidance only and.claudeignorehas no effect; use Read deny rules.On Windows with Git Bash, any Bash deny rule silently turns off the PowerShell tool unless
CLAUDE_CODE_USE_POWERSHELL_TOOL=1or a scopedPowerShell(...)rule is set.
Read the source
Further reading
Suggest a resourceNo extra reading for Built-in Tools, Permission Modes and Rules yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on Built-in Tools, Permission Modes and Rules
Ten questions, with the answer and explanation after each one.