skip to content

In a Claude Agent SDK service, why does the canUseTool callback never see some tool calls, and where must a check that sees every call live?

level: seniorimportance: must knowfreq 10%

answer

  1. the callback is the last step
  2. hooks, deny, ask, mode, allow
  3. a bare allow entry approves first
  4. allowedTools is not a whitelist
  5. PreToolUse runs before everything

basics

~20 s

canUseTool is the last step of the SDK's permission flow, so calls already approved by a hook, a mode such as acceptEdits or bypassPermissions, or an allow rule never reach it. A check for every call belongs in a PreToolUse hook callback.

solid answer

~40 s

The SDK resolves each tool request in a fixed order: **hooks**, then **deny rules**, **ask rules**, the **permission mode**, **allow rules**, and only then the `canUseTool` callback. Anything resolved earlier never reaches it: a bare `allowedTools` entry such as `"Read"` or `"Bash"`, `acceptEdits` for file edits, `bypassPermissions` for almost everything, allow rules from `.claude/settings.json`, and calls that need no approval at all, such as file reads inside the working directory. `allowedTools` pre-approves; it does not restrict, so unlisted tools stay available. In `dontAsk` mode the callback is never called and prompts become denials. A check that must see every call goes in a `PreToolUse` hook, which runs first and whose deny holds even in `bypassPermissions`; a hook's allow, however, does not skip deny or ask rules.

code

typescript · 14 lines
typescript
const options = {
  permissionMode: "default" as const,
  allowedTools: ["Read", "Grep", "Glob", "mcp__prs__get_pr_diff"],
  disallowedTools: ["Bash(git push *)"],
  hooks: {
    PreToolUse: [
      { hooks: [async (input) => { console.log("tool call", JSON.stringify(input)); return {}; }] },
    ],
  },
  canUseTool: async (toolName: string, input: Record<string, unknown>) =>
    toolName === "Bash" && String(input.command).startsWith("gh pr view")
      ? { behavior: "allow" as const, updatedInput: input }
      : { behavior: "deny" as const, message: `${toolName} is outside the PR-summary brief` },
};

go deeper

for a junior

Recall that canUseTool is called only when nothing earlier approved or denied the call, and that allowedTools pre-approves rather than restricts.

for a middle

Walk the order hooks, deny rules, ask rules, permission mode, allow rules, canUseTool, and say which modes and allow entries keep calls away from the callback.

for a senior

Diagnose a callback that never fires, including allow rules arriving from a committed settings file, and move every-call policy into a PreToolUse hook that holds even in bypassPermissions.

for a principal

Split responsibilities across deny rules, hooks, modes and the callback so that a configuration change in one layer cannot silently remove the guarantee another layer was meant to provide.

## The permission flow, in order When Claude asks to run a tool, the Agent SDK walks a fixed sequence and stops at the first step that resolves the call: 1. **Hooks.** `PreToolUse` callbacks (and hooks loaded from settings files) run first. A hook can deny outright or pass the call on; a hook that returns allow does **not** skip the deny and ask rules below. 2. **Deny rules** from `disallowedTools` and settings. A match blocks the call in every mode, including `bypassPermissions`. A bare name such as `Bash` removes the tool from Claude's context before this step even runs. 3. **Ask rules** from settings. A match sends the call to `canUseTool`, even in `bypassPermissions`. 4. **Permission mode.** `bypassPermissions` approves what reaches it (apart from protected actions such as removing a critical path), `acceptEdits` approves file edits and simple filesystem commands in the working directories, `plan` sends edits and file-modifying shell commands to the callback, and other modes fall through. 5. **Allow rules** from `allowedTools` and settings. A match approves the call. Calls a tool approves on its own, such as a file read inside the working directory or a read-only shell command, resolve here with no rule. 6. **`canUseTool`.** Only what is still unresolved reaches your callback. In `dontAsk` mode this step is skipped and the call is denied. ## Why the callback goes quiet The callback is a **fallback for prompts**, not an interceptor. Common configurations that starve it: | Configuration | What reaches `canUseTool` | |---|---| | `allowedTools: ["Bash"]` (bare name) | No ordinary `Bash` call; all are pre-approved | | `allowedTools: ["Bash(npm test *)"]` | Only `Bash` calls that do not match and need approval | | `permissionMode: "acceptEdits"` | No file edits inside the working directories | | `permissionMode: "bypassPermissions"` | Only ask-rule matches, interactive tools such as `AskUserQuestion`, and protected actions | | `permissionMode: "dontAsk"` | Nothing; prompts become denials | | An allow rule in `.claude/settings.json` | Nothing for matching calls, and the rule is invisible in your code | The TypeScript SDK warns once with the process warning code `CLAUDE_SDK_CAN_USE_TOOL_SHADOWED` when a callback is combined with `bypassPermissions` or with a bare `allowedTools` entry. It cannot see allow rules coming from settings files, which load by default through the project setting source. ## allowedTools is not a whitelist `allowedTools` adds **allow rules**. Tools you do not list still exist, and their calls fall through to the mode and the callback. With `bypassPermissions`, an `allowedTools: ["Read"]` list still lets `Bash`, `Write` and `Edit` run. To put a tool out of reach, list its bare name in `disallowedTools` or leave it out of the `tools` option. For a fixed, locked-down surface, the docs pair `allowedTools` with `permissionMode: "dontAsk"`: listed tools run, calls that need no approval run, everything that would prompt is denied. Pass `permissionMode` explicitly in a service. When it is omitted, Claude Code picks a starting mode from settings and its built-in default, and a service should not depend on which one that turns out to be. ## What the callback returns - `{ behavior: "allow", updatedInput }` runs the tool, optionally with rewritten input (Python `PermissionResultAllow(updated_input=...)`). - `{ behavior: "deny", message }` blocks it; Claude reads the message and may change approach (Python `PermissionResultDeny(message=...)`). - Echoing one of the `suggestions` back in `updatedPermissions` remembers the decision; a `localSettings` suggestion writes the rule to `.claude/settings.local.json`. The callback may stay pending indefinitely, and the run waits. For a human approval that could take hours, a `PreToolUse` hook returning `defer` lets the process exit and resume the session later. ## Debugging a callback that never fires - Add a matcher-less `PreToolUse` hook that logs every call, then compare that log with what the callback saw. - Look for bare names in `allowedTools`; a scoped rule such as `Bash(npm test *)` approves far less. - Read the settings files the session loads: user, project and local allow rules count, and your code never mentions them. - Confirm the `permissionMode` actually passed, including any later `setPermissionMode()` call. - In TypeScript, listen with `process.on('warning', ...)` for `CLAUDE_SDK_CAN_USE_TOOL_SHADOWED`. ## Where an every-call check goes Put audit logging, tenant policy or argument validation that must see **every** call in a `PreToolUse` hook callback. It runs before all other steps, it fires inside subagents too, and its deny applies even in `bypassPermissions`. Keep `canUseTool` for what it is good at: asking a person, or your application's policy, about the calls that genuinely need a decision.

  • A teammate sets permissionMode to bypassPermissions plus a canUseTool that blocks writes. What happens?
    Writes run. `bypassPermissions` approves calls at the mode step, before the callback, so the callback only sees ask-rule matches, interactive tools such as `AskUserQuestion` and protected actions. The TypeScript SDK emits `CLAUDE_SDK_CAN_USE_TOOL_SHADOWED`. To block writes, use a deny rule in `disallowedTools` or a `PreToolUse` hook, both of which hold in that mode.
  • Why might a canUseTool check stop firing after someone commits a .claude/settings.json?
    Default `query()` options load the project setting source, so allow rules in `.claude/settings.json` join the allow step and pre-approve matching calls before the callback. The TypeScript shadowing warning cannot see settings-file rules. Pin `settingSources` to what you intend, or enforce the check in a `PreToolUse` hook.
  • How does dontAsk differ from simply not passing canUseTool?
    `dontAsk` makes the outcome explicit: anything that would prompt is denied and the callback is never called, while pre-approved calls and calls needing no approval still run. The docs recommend it for a fixed headless tool surface, preferring a hard deny over relying on the callback being absent.

A bank's manual-review desk only sees the transactions no automatic rule already approved or rejected; if you want every transaction screened, the screen has to sit at the front door, not at the review desk.

  • allowedTools restricts the agent to only the listed tools.
  • canUseTool runs for every tool call, so it is a complete audit point.
  • A PreToolUse hook that returns allow skips deny and ask rules.
  • In bypassPermissions mode, tools missing from allowedTools still go to canUseTool.
  • dontAsk asks canUseTool first and denies only if the callback refuses.