# Pre-Tool Hook

> The Pre-Tool Hook is an agent hook that runs before a tool call and can block or modify it.

## How do I block dangerous commands in an AI coding agent?

The hook that runs before the agent uses a tool, such as a shell command or file edit, and receives its details. It can allow, block or change the call, for example to forbid deleting files or running commands outside the project.

- Topic: Agent Customization
- Type: concept

## Example

Wrong:

```bash
#!/bin/bash
tool=$(jq -r '.tool_name')
[ "$tool" = "Bash" ] && exit 2
exit 0
```

Right:

```bash
#!/bin/bash
cmd=$(jq -r '.tool_input.command // empty')
if echo "$cmd" | grep -qE 'rm -rf|git push --force'; then
  echo "Blocked: destructive command. Ask the user first." >&2
  exit 2
fi
exit 0
```

Checking the actual command blocks only dangerous calls and returns a reason, instead of blocking every shell command silently.

## Key points

- The hook checks the actual command or file path, not just the tool name.
- A blocked call returns a clear reason so the agent can choose another way.

## Interview questions

### Can a pre-tool hook change a tool call?

In many tools, yes: the hook can return modified input, such as a safer command or a corrected path, as well as allow or block the call.

### Is a pre-tool hook enough to block all dangerous commands?

No. Pattern matching can be bypassed with other spellings, so combine hooks with permission rules and a sandbox.

### What does a pre-tool hook receive?

Usually JSON on standard input with the tool name and its input, such as the shell command or the file path to edit.

## Related cards

- [Agent Hooks](https://wonderium.dev/ai/agent-hooks.md)
- [Post-Tool Hook](https://wonderium.dev/ai/post-tool-hook.md)
- [Agent Permissions](https://wonderium.dev/ai/agent-permissions.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/pre-tool-hook
- Last updated: 2026-11-25
