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.
Events
Section titled “Events”| Event | When it fires |
|---|---|
SessionStart | Before the first request of a session, while its history is empty. Does not fire when you resume a session that has history |
SessionEnd | Exiting 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 |
UserPromptSubmit | You sent a prompt, before the model is called. Also fires on the task a subagent receives |
PreToolUse | Before a tool call. The only place where a call can be denied or rewritten |
PostToolUse | After a tool call: successful, failed, or denied |
PreCompact, PostCompact | Before context compaction and after the summary is ready |
Stop | The main agent finishes its turn; a blocking hook can ask it to continue |
SubagentStart, SubagentStop | A subagent’s first request and the end of its turn |
Notification | The 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.
Configuration
Section titled “Configuration”{ "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 othermatcheris a regular expression searched for in the name: all tools of an MCP server aremcp__<server>__.*."*"or an empty string means “any”. An invalid expression matches nothing.matcherapplies 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(defaultfalse):truemakes the hook fire-and-forget. By default a hook is blocking: the agent waits for it and reads the result.enabled(defaulttrue):falseturns 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.
Where hooks are declared
Section titled “Where hooks are declared”Hooks are read from the hooks key in all of these places at once:
~/.claude/settings.json;~/.waibee/settings.json(on Windows,%APPDATA%\waibee\settings.json);- the project’s
.claude/settings.jsonand.waibee/settings.json, only after the folder is trusted; see Folder trust; - enabled plugins; see Plugins;
- the
hooksfield 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.
How hooks run
Section titled “How hooks run”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:
| Variable | Value |
|---|---|
HOOK_EVENT | the event name |
TOOL_NAME | the tool name; empty on events without a tool |
TOOL_INPUT | the 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_ID | the session ID |
CWD, PROJECT_DIR, CLAUDE_PROJECT_DIR | the session folder |
TRANSCRIPT_PATH | the conversation file; empty if the session is not written to disk |
TIMESTAMP | the start time in UTC, in RFC 3339 format |
CLAUDE_PLUGIN_ROOT | the 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:
| Code | Meaning |
|---|---|
0 | Continue 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 |
2 | A blocking decision; the effect depends on the event. The reason is taken from stderr |
126, 127 | The hook was not found or is not executable: this is a broken configuration, not a decision |
| anything else | Continue 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 broken hook
Section titled “A broken hook”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
UserPromptSubmithook will not block all your prompts.
Several calls in one response
Section titled “Several calls in one response”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.
Hook input
Section titled “Hook input”stdin receives JSON on a single line. Fields common to all events:
| Field | Value |
|---|---|
hook_event_name | the event name, for example PreToolUse |
session_id | the session ID |
cwd | the session folder |
transcript_path | the conversation file, if the session is written to disk |
tool_name | the tool name, on PreToolUse, PostToolUse, and Notification |
Fields of individual events:
| Event | Fields |
|---|---|
SessionStart, SubagentStart | model: the model of the session or subagent |
UserPromptSubmit | prompt: the prompt text |
PreToolUse | tool_input: the call’s arguments |
PostToolUse | tool_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, SubagentStop | agent_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 |
PreCompact | agent_id; trigger: manual for /compact, auto for automatic compaction; custom_instructions: the text after /compact; input_tokens, context_limit, history_len |
PostCompact | agent_id, trigger, history_len |
Notification | kind: permission_required; command: the command or null; reason: why approval is needed |
SessionEnd | reason: 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'.
What a hook can block or change
Section titled “What a hook can block or change”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:
| Field | Event | Effect |
|---|---|---|
hookSpecificOutput.permissionDecision | PreToolUse | allow 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.permissionDecisionReason | PreToolUse | The reason for the decision, shown to the model |
hookSpecificOutput.updatedInput | PreToolUse | Replaces the call’s arguments entirely, also without permissionDecision. Every hook gets the original arguments; the last updatedInput wins |
hookSpecificOutput.updatedToolOutput | PostToolUse | Replaces the tool result before the model and the interface see it |
hookSpecificOutput.additionalContext | SessionStart, SubagentStart, UserPromptSubmit, PostToolUse, Stop, SubagentStop | Adds 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 stopReason | UserPromptSubmit, PreToolUse, PostToolUse | Rejects 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.
Examples
Section titled “Examples”Formatting after every edit
Section titled “Formatting after every edit”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.
Protecting paths from edits
Section titled “Protecting paths from edits”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" }] } ] }}#!/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 ;;esacThe 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.
Adding context to every prompt
Section titled “Adding context to every prompt”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" }] } ] }}#!/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}}'A notification when the agent is done
Section titled “A notification when the agent is done”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.
A command log
Section titled “A command log”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 }] } ] }}Checking for secrets before a write
Section titled “Checking for secrets before a write”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" }] } ] }}Claude Code compatibility
Section titled “Claude Code compatibility”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
stdoutdoes not become context. In Claude Code, aUserPromptSubmitorSessionStarthook can print text; here it needs JSON withadditionalContext. - Only hooks with
commandwork. Thetypefield is not read: any entry withcommandruns, and Claude Code hooks of other types (http,prompt,agent,mcp_tool) withoutcommandare skipped. - The timeout is always 60 seconds. The
timeoutfield is not read. - A broken
PreToolUsehook denies the call instead of letting it through with a warning. - Hooks from
settings.local.jsonare not read; see Where hooks are declared. - The input JSON has only the fields listed in Hook input. For example, it has no
permission_modeortool_use_id,SessionStarthas nosource, andtool_responseinPostToolUseis text.
Claude Code has more events than this table. A hook on any other event, such as PermissionRequest or PostToolUseFailure, does not fire here.