Skip to content
docs

Hooks

A hook is a command that runs on a specific agent event. You declare hooks in settings.json under the hooks key.

A rule in WAIBEE.md is a request: the agent usually follows it but can miss it, especially when the context is full. A hook is run by the application when its event fires, regardless of what the model decides. That makes hooks a good fit for automatic checks, but they are not guaranteed to run if the process crashes. For example, a forced exit with a second Ctrl+C can skip SessionEnd.

EventWhen it fires
SessionStartBefore the first request of a session, while its history is empty. Does not fire when you resume a session that has history
SessionEndExiting the terminal, closing each of its tabs, the end of waibee run, closing or deleting a session in an IDE, logging out, and disconnecting the editor. /clear and closing a session in the browser do not trigger it
UserPromptSubmitYou sent a prompt, before the model is called. Also fires on the task a subagent receives
PreToolUseBefore a tool call. The only place where a call can be denied or rewritten
PostToolUseAfter a tool call: successful, failed, or denied
PreCompact, PostCompactBefore context compaction and after the summary is ready
StopThe main agent finishes its turn; a blocking hook can ask it to continue
SubagentStart, SubagentStopA subagent’s first request and the end of its turn
NotificationThe agent is waiting for your approval of a tool call. Does not fire in headless mode, where such a call is denied right away

An unknown event name is silently ignored, so the only sign of a typo in the key is that the hook never fires.

{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "command": "./scripts/audit.sh" }
]
}
]
}
}
  • matcher (optional): a tool name or several names separated by | (Write|Edit); these match only the whole name. Any other matcher is a regular expression searched for in the name: all tools of an MCP server are mcp__<server>__.*. "*" or an empty string means “any”. An invalid expression matches nothing. matcher applies only to events with a tool (PreToolUse, PostToolUse, Notification); on all other events the hook always fires.
  • command: a shell command. An entry without one is skipped.
  • async (default false): true makes the hook fire-and-forget. By default a hook is blocking: the agent waits for it and reads the result.
  • enabled (default true): false turns the hook off without removing the entry.

You can leave out the nested hooks list: the entry { "matcher": "Bash", "command": "./scripts/audit.sh" } means the same as the example above.

Earlier tool names in matcher and the blocking field from early versions are also understood; see Names from earlier versions.

Hooks are read from the hooks key in all of these places at once:

  1. ~/.claude/settings.json;
  2. ~/.waibee/settings.json (on Windows, %APPDATA%\waibee\settings.json);
  3. the project’s .claude/settings.json and .waibee/settings.json, only after the folder is trusted; see Folder trust;
  4. enabled plugins; see Plugins;
  5. the hooks field in a subagent’s definition, only while that subagent runs; see Subagents.

settings.local.json files and the organization’s managed file do not define hooks. The same hook (same command, matcher, and mode) declared in several settings files runs once.

The command runs through bash -c in the session folder. On Windows it runs in Git Bash or the bash on PATH; without them, in the built-in shell of Waibee Code, and as a last resort in cmd /C. The hook gets the event JSON on stdin (see Hook input) and these environment variables:

VariableValue
HOOK_EVENTthe event name
TOOL_NAMEthe tool name; empty on events without a tool
TOOL_INPUTthe whole event JSON, as on stdin, not only the call’s arguments. A value that is too long is truncated; the full JSON is always on stdin
SESSION_IDthe session ID
CWD, PROJECT_DIR, CLAUDE_PROJECT_DIRthe session folder
TRANSCRIPT_PATHthe conversation file; empty if the session is not written to disk
TIMESTAMPthe start time in UTC, in RFC 3339 format
CLAUDE_PLUGIN_ROOTthe plugin directory, for plugin hooks only

Environment variables whose names contain KEY, TOKEN, SECRET, PASSWORD, CREDENTIAL, AUTH, SESSION, COOKIE, or PRIVATE do not reach the hook, except service variables such as SSH_AUTH_SOCK and GIT_AUTHOR_EMAIL. If a hook needs such a variable, declare it in the env key of the settings; see Settings reference.

The agent waits 60 seconds for a blocking hook; the timeout field is not read. An async hook has no time limit. Blocking hooks of one event run one after another, in the order they are declared.

The /hooks command shows the configured hooks and adds a new one through a form: the event, the tools, the command, whether to wait for the hook, and where to save it (“this project” writes to .waibee/settings.json, “every project” to ~/.waibee/settings.json). The form offers PreToolUse, PostToolUse, UserPromptSubmit, Stop, and SubagentStop; hooks on other events go into the file by hand. Keys: n adds a hook, Enter goes a level deeper, Space turns a hook off without removing the entry, d removes the entry, and Esc or Backspace goes a level back. In the form, Tab moves between fields, Enter saves, and Esc cancels. Hooks from .claude/ and from plugins are shown here but cannot be changed. The same is available on the extensions page in the browser.

A blocking hook answers with its exit code:

CodeMeaning
0Continue as usual if there is no decision. If stdout holds JSON, it is parsed (see below); a deny decision in it works like code 2
2A blocking decision; the effect depends on the event. The reason is taken from stderr
126, 127The hook was not found or is not executable: this is a broken configuration, not a decision
anything elseContinue without a decision from the hook; a warning goes to the log

An async hook ("async": true) is started and forgotten: neither its exit code nor its output is read. That is enough for notifications and logging, and it delays nothing.

A hook that has a typo in its path, is not executable, fails to start, or hangs until the timeout does not count as a decision:

  • on PreToolUse, it counts as a denial. This is a security boundary, and a broken check must not quietly let calls through;
  • on every other event, the agent shows a warning and keeps working. A wrong path in a UserPromptSubmit hook will not block all your prompts.

The model can call several tools in one response. Then the PreToolUse hooks run one after another for all the calls, and only after that do the approved tools run. Calls that only read run at the same time; calls that change something (an edit, a write, a command that does more than read, an MCP tool above read_only) run one at a time, in the order the model wrote them.

So a hook can deny a call but cannot hold it until a neighboring call finishes. At that point the neighboring call has not started yet: a hook that waits for it only waits out the timeout, and the call is denied. If the read_only tools of one MCP server must not run at the same time, the queue belongs in the server itself. The other option: the hook denies the second call with a reason such as “call these one at a time”, and the model repeats the call on its next turn.

stdin receives JSON on a single line. Fields common to all events:

FieldValue
hook_event_namethe event name, for example PreToolUse
session_idthe session ID
cwdthe session folder
transcript_paththe conversation file, if the session is written to disk
tool_namethe tool name, on PreToolUse, PostToolUse, and Notification

Fields of individual events:

EventFields
SessionStart, SubagentStartmodel: the model of the session or subagent
UserPromptSubmitprompt: the prompt text
PreToolUsetool_input: the call’s arguments
PostToolUsetool_input; tool_response: the tool result as text; is_error; elapsed_ms: how long the call took. A denied call has denied: true instead of elapsed_ms, and tool_response holds the denial text
Stop, SubagentStopagent_id; stop_hook_active: a hook has already asked to continue in this turn; aborted. With aborted: false, also last_assistant_message, the model’s last text. With aborted: true, the turn was cut short by an error or a limit, and reason names the cause
PreCompactagent_id; trigger: manual for /compact, auto for automatic compaction; custom_instructions: the text after /compact; input_tokens, context_limit, history_len
PostCompactagent_id, trigger, history_len
Notificationkind: permission_required; command: the command or null; reason: why approval is needed
SessionEndreason: prompt_input_exit (exiting the terminal, the end of waibee run), close, delete, logout, disconnect (IDE)

Paths in tool_input are absolute, even if the model wrote a path relative to the project folder. This applies to file_path in Read, Write, and Edit, notebook_path in NotebookEdit, and path in Glob and Grep. A Bash command reaches the hook as is. In PostToolUse, tool_input already includes the updatedInput changes from PreToolUse hooks.

A hook whose matcher matched only an earlier tool name gets the earlier name and the earlier parameter names; see Names from earlier versions.

An example for PreToolUse:

{
"tool_input": {
"file_path": "/home/me/proj/src/main.rs",
"old_string": "foo",
"new_string": "bar"
},
"hook_event_name": "PreToolUse",
"session_id": "0b6f3c1e-8a55-4c1e-9f7e-2d2f1c9a7b10",
"cwd": "/home/me/proj",
"transcript_path": "/home/me/.waibee/sessions/0b6f3c1e-8a55-4c1e-9f7e-2d2f1c9a7b10.jsonl",
"tool_name": "Edit"
}

A script reads the field it needs with jq: jq -r '.tool_input.file_path'.

Code 2 or a deny decision blocks sending the prompt on UserPromptSubmit and the tool call on PreToolUse. On PostToolUse, it appends the reason to the result and ends the turn after the current calls, but it does not undo a tool that already ran. On PreCompact, code 2 or the JSON {"decision":"block","reason":"reason"} leaves the history uncompacted and shows the reason. On Stop and SubagentStop, it asks the agent to continue. On SessionStart, SubagentStart, PostCompact, Notification, and SessionEnd, blocking changes nothing, and for a denied call the PostToolUse hook’s answer is ignored.

Other decisions are set by JSON on the stdout of a blocking hook:

FieldEventEffect
hookSpecificOutput.permissionDecisionPreToolUseallow removes the question the mode would have asked, but does not approve a command the check cannot parse; ask asks you (in headless mode, and for a subagent that is not allowed to ask, such a call is denied); deny forbids the call. defer and no decision leave the usual permission check. deny rules, protected paths, and a subagent’s plan mode apply whatever the hook answers. If there are several hooks, the strictest decision wins
reason or hookSpecificOutput.permissionDecisionReasonPreToolUseThe reason for the decision, shown to the model
hookSpecificOutput.updatedInputPreToolUseReplaces the call’s arguments entirely, also without permissionDecision. Every hook gets the original arguments; the last updatedInput wins
hookSpecificOutput.updatedToolOutputPostToolUseReplaces the tool result before the model and the interface see it
hookSpecificOutput.additionalContextSessionStart, SubagentStart, UserPromptSubmit, PostToolUse, Stop, SubagentStopAdds context for the model: before the request, to the tool result, or to the next request. On its own, it does not make the agent continue
continue: false plus stopReasonUserPromptSubmit, PreToolUse, PostToolUseRejects the prompt, denies the call, or ends the turn after the current calls

permissionDecision is also read at the top level of the JSON. The decision field is understood the same way as permissionDecision, and decision: "block" means deny. reason inside hookSpecificOutput is not read: put the reason at the top level or in permissionDecisionReason.

No hook decision lets through a call that a deny rule or an absolute ban blocks: such a call is denied without a question, whatever the hook answers.

If one step of cleaning up arguments depends on the result of another, combine them in one hook: the next hook does not get the previous hook’s changes.

When a turn ends normally, Stop and SubagentStop hooks also accept {"decision":"block","reason":"what is left to do"}. The reason goes to the model for the next request. The number of such continuations in a row is limited; after the limit, the turn ends. After an error or a forced stop, a hook does not resume work.

The detail that matters most: plain output on stdout does not reach the model. A hook that prints text in the hope of adding to the context changes nothing: it needs JSON with additionalContext. Non-JSON on stdout sets no decision: on PreToolUse, the usual permission check stays.

The most common case. The agent does not spend a turn running the formatter, and the diff stays clean.

{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [{ "command": "make fmt", "async": true }]
}
]
}
}

async earns its place here: the agent does not need the formatter’s result, and without the flag every edit waits for make fmt to finish.

Migrations, lock files, generated code, another team’s module: things the agent should not touch without you. The deny list in permissions covers commands; this hook covers files.

{
"hooks": {
"PreToolUse": [
{
"matcher": "Write|Edit",
"hooks": [{ "command": "./scripts/guard-paths.sh" }]
}
]
}
}
scripts/guard-paths.sh
#!/usr/bin/env bash
# Exit 2 blocks the tool call; stderr becomes the reason shown to the agent.
path=$(jq -r '.tool_input.file_path // empty')
case "$path" in
*/migrations/*|*/package-lock.json)
echo "this path is edited by hand only" >&2
exit 2
;;
esac

The file must be executable (chmod +x) and sit where the config says: on PreToolUse, a hook that is not found counts as a denial, and every edit will be blocked.

A UserPromptSubmit hook can add to your input. It helps when you keep repeating the same things: the current branch, the task number, the state of the working copy. The context goes in the additionalContext field; the agent does not read the command’s plain output.

{
"hooks": {
"UserPromptSubmit": [
{ "hooks": [{ "command": "./scripts/git-context.sh" }] }
]
}
}
scripts/git-context.sh
#!/usr/bin/env bash
# Plain stdout is ignored; only additionalContext reaches the model.
jq -n --arg ctx "$(git status --short --branch)" \
'{hookSpecificOutput: {hookEventName: "UserPromptSubmit", additionalContext: $ctx}}'

You no longer have to watch the terminal during a long task.

{
"hooks": {
"Stop": [
{ "hooks": [{ "command": "osascript -e 'display notification \"Done\" with title \"Waibee Code\"'", "async": true }] }
]
}
}

On Linux, use notify-send instead. You can also skip the notification and send a message to a messenger with curl.

Every Bash run goes into one file. This helps when you work in trust mode or want to see what happened in a long session.

{
"hooks": {
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [{ "command": "jq -r '.tool_input.command' >> .waibee/commands.log", "async": true }]
}
]
}
}

A PreToolUse hook stops a write if the content looks like a key or a token. That is cheaper than scrubbing it from the git history.

{
"hooks": {
"PreToolUse": [
{
"matcher": "Write|Edit",
"hooks": [{ "command": "./scripts/no-secrets.sh" }]
}
]
}
}

A hook brought over from Claude Code for one of the events in the table works without changes, blocking included, except for the differences below. The event names, the configuration format, the exit codes, and the hookSpecificOutput fields are the same. ~/.claude/settings.json is read too, CLAUDE_PROJECT_DIR and CLAUDE_PLUGIN_ROOT are exported, and decision: "block" is understood.

Differences:

  • Plain output on stdout does not become context. In Claude Code, a UserPromptSubmit or SessionStart hook can print text; here it needs JSON with additionalContext.
  • Only hooks with command work. The type field is not read: any entry with command runs, and Claude Code hooks of other types (http, prompt, agent, mcp_tool) without command are skipped.
  • The timeout is always 60 seconds. The timeout field is not read.
  • A broken PreToolUse hook denies the call instead of letting it through with a warning.
  • Hooks from settings.local.json are not read; see Where hooks are declared.
  • The input JSON has only the fields listed in Hook input. For example, it has no permission_mode or tool_use_id, SessionStart has no source, and tool_response in PostToolUse is text.

Claude Code has more events than this table. A hook on any other event, such as PermissionRequest or PostToolUseFailure, does not fire here.