# Agent Hooks

> The Agent Hooks are user-defined commands that run automatically at specific points of a coding agent's work.

## What are hooks in AI coding agents?

The scripts that a harness runs on events, such as before a tool call, after a file edit or at the end of a task. Used to run formatters and linters, block dangerous commands or send notifications, deterministically and without relying on the model.

- Topic: Agent Customization
- Type: concept

## Example

Wrong:

```json
{
  "hooks": {
    "PostToolUse": [
      { "command": "npm run lint && npm test" }
    ]
  }
}
```

Right:

```json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "command": "./.agent/hooks/format.sh"
      }
    ]
  }
}
```

The matcher runs the hook only after file edits, and a fast format script replaces a slow full test run on every step.

## Common mistakes

- Relying on instructions in a prompt for a rule that must always run, instead of a hook.

## Key points

- Each hook is attached to a specific event and filtered to the tools it needs.
- Hook scripts are fast and handle errors, so a failing hook does not break every step.
- Blocking hooks use the exit code or output format the agent expects, with a clear reason.

## Interview questions

### What is the difference between hooks and instructions?

Instructions are suggestions the model may skip. Hooks are scripts the harness always runs on an event, so they suit rules that must never be skipped.

### Which events can agent hooks run on?

Common events are session start, prompt submit, before a tool call, after a tool call and when the agent stops. Names differ between tools.

### Are hooks a security risk?

Yes, they run shell commands with your permissions. Review hooks in shared settings and plugins like any other code.

## Related cards

- [Pre-Tool Hook](https://wonderium.dev/ai/pre-tool-hook.md)
- [Post-Tool Hook](https://wonderium.dev/ai/post-tool-hook.md)
- [Stop Hook](https://wonderium.dev/ai/stop-hook.md)
- [Session Start Hook](https://wonderium.dev/ai/session-start-hook.md)
- [Prompt Submit Hook](https://wonderium.dev/ai/prompt-submit-hook.md)

---

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