# Stop Hook

> The Stop Hook is an agent hook that runs when the agent finishes its response.

## How do I make an AI agent run tests before it finishes?

The hook that runs when the agent is about to finish. It can run tests and make the agent continue if they fail, or send a notification that the task is done.

- Topic: Agent Customization
- Type: concept

## Example

Wrong:

```bash
#!/bin/bash
npm test || exit 2
```

Right:

```bash
#!/bin/bash
input=$(cat)
if [ "$(echo "$input" | jq -r '.stop_hook_active')" = "true" ]; then exit 0; fi
if ! npm test --silent; then
  echo "Tests fail. Fix them before finishing." >&2
  exit 2
fi
```

Skipping the check when the hook already forced a retry prevents an endless loop while still making the agent fix failing tests.

## Key points

- The hook has a limit, so a failing check cannot keep the agent running forever.
- The hook runs the same test and lint commands that CI runs.

## Interview questions

### Can a stop hook make the agent keep working?

Yes. If the hook blocks the stop and returns a reason, the agent continues and tries to fix the problem.

### How do I avoid an infinite loop in a stop hook?

Check the flag the tool passes when the hook already triggered a retry, or count attempts, and let the agent stop after a limit.

### Can a stop hook send a notification?

Yes. A common use is a desktop or chat notification that the task is done, so you do not have to watch the session.

## Related cards

- [Agent Hooks](https://wonderium.dev/ai/agent-hooks.md)
- [Post-Tool Hook](https://wonderium.dev/ai/post-tool-hook.md)
- [Unit Test](https://wonderium.dev/engineering/unit-test.md)

---

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