> ## Documentation Index
> Fetch the complete documentation index at: https://kiro.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Hooks

> Automate reactions to chat events. Run scripts, inject context, or gate tool calls when sessions start, prompts arrive, tools run, or turns end.

Hooks let you automate supported events in Crew. A hook can run a script, inject context, or gate a tool call when a Gateway chat event fires. You can also store Kiro Agent task, file, and manual event names for on-demand testing.

## Managing hooks

### From the dashboard

Open **Agent Capabilities → Hooks**. You can view, create, edit, and delete hooks from the browser. Each hook shows its trigger, action, and optional matcher pattern.

### From the filesystem

Hooks are stored in `~/.kiro/crew/hooks.json`. Provider-owned hooks appear read-only in the dashboard and remain in their provider configuration.

## Hook structure

A hook has three parts:

1. **Trigger** — when should Crew run it?
2. **Matcher** (optional) — filters which events run the hook. Tool events match the tool name, and message events match the message text. See [Match automatic events](#match-automatic-events).
3. **Action** — run one shell command, or load selected Skills into context.

## Triggers

### Events that fire automatically

| Trigger | Fires when |
|---|---|
| `AgentSpawn` | A new agent session begins |
| `UserPromptSubmit` | A message is sent to the agent |
| `PreToolUse` | Before a tool starts |
| `PostToolUse` | After a tool finishes |
| `Stop` | An agent turn ends |

### Match automatic events

| Event type | Match subject | Modes |
|---|---|---|
| `PreToolUse`, `PostToolUse` | Tool name | Glob: exact name, `prefix*`, `*suffix`, `*text*`, or `*` |
| `AgentSpawn`, `UserPromptSubmit`, `Stop` | Message or final reply text | Glob, case-insensitive regex, or pipe-delimited contains terms |

The six KAS events below take no matcher.

### Kiro Agent events stored for testing

The Hooks editor can store six more event names from the [KAS backend](https://kiro.dev/docs/crew/features/agent-backends.md). None fires automatically in Crew 0.8. **Test** is the only way to run these hooks, and each one is saved disabled.

| Trigger | Intended moment | Row status |
|---|---|---|
| `PreTaskExecution` | Before a spec task starts | `not fired yet` |
| `PostTaskExecution` | After a spec task completes | `not fired yet` |
| `FileCreated` | After Kiro Agent creates a file | `never fires` |
| `FileEdited` | After Kiro Agent saves a file | `never fires` |
| `FileDeleted` | After Kiro Agent deletes a file | `never fires` |
| `UserTriggered` | When you run the hook by hand | `never fires` |

## Actions

### Command action

A command action runs one shell command. It receives `KIROCREW_HOOK_EVENT` and `KIROCREW_HOOK_CONTEXT` in its environment, plus the event JSON on stdin. Hooks use `/bin/sh` on POSIX and `%ComSpec%` on Windows, so write platform-specific commands when needed.

A hook does not inherit the Gateway's full environment. It receives `PATH`, home and temp directories, locale, TLS trust, and `KIROCREW_HOME`. Set values such as `AWS_PROFILE` or `VIRTUAL_ENV` inside the command, or use absolute paths.

Exit codes matter:

- `0` allows `PreToolUse`. For `AgentSpawn` and `UserPromptSubmit`, stdout enters the model context.
- `2` denies a `PreToolUse` call and sends stderr to the model.
- Any other exit, timeout, or crash also blocks `PreToolUse`. The gate fails closed.
- On every other event, a non-zero exit records a warning and the turn continues.

A `Stop` hook can request another turn. Exit `0` with stdout `` to inject the reason and continue the agent. Any other stdout is ignored.

A `PreToolUse` hook can block only when the Gateway controls approval. If Kiro CLI already approved the call, the hook reports the event but cannot stop it.

### Skills action

Leave the command empty and select Skills to load them into context without starting a subprocess. This works only with `AgentSpawn` or `UserPromptSubmit`. Other trigger and action combinations are refused rather than saved without effect.

## Common patterns

### Gate tool calls with a command

Use `PreToolUse` with matcher `*` to send every tool call the Gateway approves through one policy script. Exit `0` to allow the call or `2` with a useful stderr message to deny it.

### Load Skills for matching prompts

Use `UserPromptSubmit`, choose **contains**, and set a matcher such as `deploy|release`. Select the Skills to load and leave the command empty.

## Creating a hook from the dashboard

1. Open **Agent Capabilities → Hooks**.
2. Select **New Hook**, name it, and choose an event.
3. For a tool event, add a glob matcher. For a message event, choose glob, regex, or contains.
4. Add one command, or leave it empty and select Skills for `AgentSpawn` or `UserPromptSubmit`.
5. Save the hook. Automatic-event hooks are enabled when saved; the six KAS event hooks are saved disabled.
6. Use **Test** to run the stored hook and inspect its exit code, stdout, and stderr.

A new automatic hook can fire before you test it. Disable a `PreToolUse` hook first when its command is not ready to gate live calls. No Gateway restart is needed.
