# Claude Code hooks in practice: how hivemux reads session state

A real hooks setup: which Claude Code hook events hivemux registers, what each payload carries, how they become running, waiting and done, and the traps we hit.

Source: https://hivemux.dev/guides/claude-code-hooks-example/

> Claude Code hooks are shell commands in `~/.claude/settings.json` that run at events like SessionStart, PreToolUse, PermissionRequest, Notification, Stop and SessionEnd, with JSON on stdin carrying `session_id` and `cwd`. hivemux registers one script for eleven events, maps them to four states (running, waiting, done, idle) and writes the state onto its tmux pane; the sidebar only reads tmux.

Claude Code's hooks are documented, but most examples stop at "play a sound on Stop". This guide shows a hooks setup that has to be right all day: the one hivemux uses to know, for every Claude Code session on the Mac, whether it is running, waiting for you, or done. It is a single shell script registered for eleven events.

## How a hook is registered

Hooks live in `~/.claude/settings.json` under `hooks`, one list per event. `hivemux setup` adds an entry like this for each event it needs:

```json
{
  "hooks": {
    "Stop": [
      { "hooks": [ { "type": "command", "command": "/path/to/hivemux-hook Stop", "timeout": 5 } ] }
    ]
  }
}
```

Claude runs the command with the event's JSON payload on stdin. Every payload has `session_id`, `cwd` and `hook_event_name`; tool events add `tool_name` and `tool_input`; Notification adds `notification_type` and `message`. The script reads what it needs with `jq`:

```sh
event="$1"
input="$(cat)"
field() { printf '%s' "$input" | jq -r --arg k "$1" '.[$k] // empty'; }
sid="$(field session_id)"; cwd="$(field cwd)"
```

Setup passes the event name as the argument, so one script handles all eleven: `SessionStart`, `UserPromptSubmit`, `PreToolUse`, `PermissionRequest`, `PostToolUse`, `PostToolUseFailure`, `PermissionDenied`, `Notification`, `Stop`, `StopFailure`, `SessionEnd`.

## From events to four states

The sidebar needs only four states per session. The hook maps events to them:

| Event | What it means | State written |
|---|---|---|
| `SessionStart` | a session started or resumed; payload has the session ID and folder | `idle`, plus `@claude_session` and `@claude_cwd` |
| `UserPromptSubmit` | you sent a prompt | `run` |
| `PreToolUse` / `PostToolUse` | Claude is using a tool | `run` (and counts subagents, notes plan mode) |
| `PermissionRequest` | the approval dialog opened | `wait`, with the command's description or the file |
| `Notification` (`permission_prompt`, `elicitation_dialog`, `agent_needs_input`) | Claude needs you | `wait`, with the message |
| `PermissionDenied` | you chose "No, and tell Claude…" | `run` (the turn continues) |
| `Stop` | the turn ended | `done`, unless background work is still running |
| `StopFailure` | the turn ended with an error | `wait`, kind `error` |
| `SessionEnd` | `/exit`, Ctrl+D or the process ended | state cleared |

The state goes onto the tmux pane the session runs in, with a timestamp:

```sh
tmux set -p -t "$TMUX_PANE" @claude_state wait
tmux set -p -t "$TMUX_PANE" @claude_state_at "$(date +%s)"
tmux set -p -t "$TMUX_PANE" @claude_wait "$(printf 'approve\tBash\t%s' "$description")"
```

`$TMUX_PANE` is set in the environment of every process inside a tmux pane, including Claude and the hooks it spawns, so the hook does not have to work out which pane it is in. Anything that can run `tmux show -pv @claude_state` can now read the state: the sidebar does it once a second, the bee office server does it for its page, and `hivemux-panes` reads `@claude_session` to resume each pane after a reboot.

## What we got wrong first

Things the documentation does not spell out, found by watching real sessions:

- **Notification is late.** The approval dialog opens with `PermissionRequest`; the `permission_prompt` Notification arrives a few seconds later. Early hivemux used Notification only and the card turned amber after the screen did. Listen to `PermissionRequest` for the state and keep Notification for the message text.
- **Stop is not done.** A turn can end while subagents Claude started in the background are still working, or while a shell command it ran in the background (a build, a test run) is still going. The hook checks for both and keeps the session running, with a count: "2 agents · 1 shell running". A dev server (`npm run dev`, `vite`, `tail -f`) is the exception: that is not work the turn waits for.
- **Subagent events are not the main session's.** A subagent's tool calls fire the same hooks with the same `session_id`. While the main Claude waits for your answer, its subagents' PreToolUse must not flip the state back to running; the hook remembers who opened the wait and ignores the others.
- **A deliberate exit must be remembered.** `SessionEnd` after `/exit` clears the session ID from the pane, so a reboot does not resume a conversation you ended on purpose.
- **Exit 0, print nothing.** A hook that prints to stdout can feed text into the conversation, and a non-zero exit can block a tool call. The hook ends with `exit 0` on every path and sends its own errors nowhere.

## Reading the state yourself

With hivemux installed, the state is on the panes:

```sh
tmux list-panes -a -F '#{pane_id} #{@claude_state} #{@claude_wait}'
tmux show -pv -t %18 @claude_session
```

That is also how you would build your own view on top of it, or a script that waits until every session is done.

hivemux is a tmux sidebar that shows every Claude Code session — running, waiting for you, or done — and lets you jump to the one that needs you.

```sh
curl -fsSL https://hivemux.dev/install.sh | bash
```

How the rest of hivemux fits together is in [/docs/how-it-works/](/docs/how-it-works/); the session ID the hook records is explained in [How to find a Claude Code session ID](/guides/claude-code-session-id/).

## FAQ

### Can a hook slow Claude Code down or block it?

A command hook runs synchronously, so a slow hook delays Claude. hivemux's hook does one or two tmux calls and exits; it is registered with a 5 second timeout, always exits 0 and never prints to stdout, so it can neither block a tool call nor inject text into the conversation.

### Which hook fires when Claude asks for permission?

PermissionRequest fires the moment the approval dialog opens. A Notification event with `notification_type` of `permission_prompt` follows a few seconds later. If you only listen to Notification, your "waiting" state lags behind the screen.

### Does Stop mean the session is done?

Not always. Stop fires when Claude's turn ends, but background subagents or a shell command Claude said it is waiting for may still be running. hivemux keeps the session in the running state in that case and shows how many agents or shells are still going.

### Where do the hooks write their state?

Onto the tmux pane, as user options such as `@claude_state`, `@claude_session` and `@claude_wait`, read with `tmux show -pv`. No database and no server; the pane is the state store, and anything that can run tmux can read it.
