#### Features

# Terminal UI hooks

This page covers the Terminal UI. For the Agents Window, see [Hooks in the Agents Window](/build/agents-window/hooks).

A hook is a shell command or HTTP endpoint that Grok calls when a lifecycle event occurs: block a dangerous command before it runs, log tool use, run a formatter after edits, or send a notification when a turn ends.

## Configuration

Hooks are JSON files. Personal hooks live in `~/.grok/hooks/*.json`; project hooks live in `<project>/.grok/hooks/*.json`. Claude Code (`.claude/settings.json`) and Cursor (`.cursor/hooks.json`) hook files are read as well, including Cursor's camelCase event names.

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [{ "type": "command", "command": "bin/safety-check.sh", "timeout": 10 }]
      }
    ]
  }
}
```

`matcher` is a regular expression tested against the tool name (Claude tool names such as `Bash`, `Read`, and `Edit` are mapped to Grok's automatically); omit it to match everything. `type` is `"command"` or `"http"` (with a `url` to POST the event to). Manage and inspect loaded hooks in the `/hooks` tab of the extensions modal.

`timeout` is in seconds, and its default depends on the event, because the work these hooks do differs by an order of magnitude: a hook on `PostToolUse` or `Stop` is often a build, a test run, or a linter, while a hook on `UserPromptSubmit` runs before every prompt you send and a slow one stalls the session.

| Event | Default timeout |
| ----- | --------------- |
| `PostToolUse`, `Stop`, `SubagentStop` | 600 seconds |
| `UserPromptSubmit` | 30 seconds |
| `SessionEnd` | 1.5 seconds |
| Everything else, `PreToolUse` included | 5 seconds |

Set `timeout` yourself whenever the default does not fit your hook; the value you give is used as-is. `SessionEnd` is the exception, because the session is already closing: anything above 60 seconds is clipped to 60.

Project hooks require trust before they run: the first time you open a repo with hooks, grant it with `/hooks-trust` or by launching with `--trust`. The decision is stored in `~/.grok/trusted_folders.toml` and covers project MCP and LSP servers too.

## Events

| Event | Fires when |
| ----- | ---------- |
| `SessionStart`, `SessionEnd` | A session starts or ends |
| `UserPromptSubmit` | You submit a prompt |
| `PreToolUse` | A tool is about to run |
| `PostToolUse`, `PostToolUseFailure` | A tool completes or fails |
| `PermissionDenied` | The permission system denies a tool call |
| `Stop`, `StopFailure` | A turn ends, or ends with an API error |
| `Notification` | The agent sends a notification |
| `SubagentStart`, `SubagentStop` | A subagent starts or finishes |
| `PreCompact`, `PostCompact` | Conversation compaction runs |

## The script contract

The event arrives as JSON on stdin, including `hookEventName`, `sessionId`, `cwd`, `workspaceRoot`, and for tool events `toolName` and `toolInput`. Every hook process also receives `GROK_HOOK_EVENT`, `GROK_HOOK_NAME`, `GROK_SESSION_ID`, and `GROK_WORKSPACE_ROOT` in its environment.

A `PreToolUse` hook decides by writing JSON to stdout:

```json
{ "decision": "deny", "reason": "Unsafe command detected" }
```

Exit code 0 allows, exit code 2 denies. Everything else—timeouts, crashes, malformed output—is fail-open: the failure is recorded in the session but the tool call proceeds. Only an explicit `deny` blocks.

To block on failure instead, set `"failClosed": true` on the hook, and a run that times out, crashes, or returns output Grok cannot read denies the action. It applies to the two events that gate what happens next, `PreToolUse` and `UserPromptSubmit`; on any other event it is ignored and logged. A Claude settings file spells the same thing as `"onFailure": "block"`. Hooks read from a Cursor `.cursor/hooks.json` file follow these rules too, so set `failClosed` there as well if you want a malformed response to deny.

A `UserPromptSubmit` hook gates the prompt rather than a tool call, and stops it before the model sees it. Exit code 2 blocks, as does `{ "decision": "block", "reason": "..." }` on stdout; the reason is shown to you and is not added to the model's context. For passive events, stdout is ignored; exit 0 on success.
