You send a long task to Claude Code, switch windows, and forget about it. Minutes later, you discover it was stuck waiting for permission. Another common scenario: the agent edits ten files and each one comes out with a different formatting style. Both problems have the same solution: hooks, shell commands that Claude Code runs at fixed points in its lifecycle.

Claude Code: 3 hooks for desktop alerts, auto-formatting with Prettier, and context retention
Configure three hooks in Claude Code: desktop alerts when the agent waits for you, automatic Prettier after every edit, and context re-injection after compaction.
Get AI articles in your inbox
Technical curation on models, agents and coding tools. No spam, unsubscribe anytime.
In this tutorial you configure three practical hooks: an alert when the agent needs you, automatic formatting with Prettier, and context reinjection after compaction. Everything follows the official Claude Code hooks guide.
What a hook is and why use it
Hooks are shell commands you define. The difference from asking something in the prompt is that the hook is deterministic: the action always happens, without depending on the model deciding to do it. You register hooks in a configuration file, inside a hooks key, and each event (such as Notification, PostToolUse, or SessionStart) is a key inside that object.
There are two common places for the file:
~/.claude/settings.json: applies to all your projects..claude/settings.jsonat the project root: applies only to that repository and can be versioned with the team.
If the file already has a hooks key, add the new event as a sibling of the existing ones. Do not replace the entire object, or you will erase what was already there.
Hook 1: alert when Claude waits for you
The Notification event fires when Claude is waiting for a response or permission. Open ~/.claude/settings.json (create it if it does not exist) and add, on macOS:
{
"hooks": {
"Notification": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Claude Code needs your attention\" with title \"Claude Code\"'"
}
]
}
]
}
}Linux
Replace the command with notify-send:
"command": "notify-send 'Claude Code' 'Claude Code needs your attention'"Test first in the terminal with notify-send 'Claude Code' 'test'. If the command does not exist, install the libnotify-bin package on Debian and Ubuntu. Headless servers, SSH sessions, and most containers do not have a notification daemon, so the alert will not appear there.
Windows (PowerShell)
"command": "powershell.exe -Command \"[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Claude Code needs your attention', 'Claude Code')\""This command opens a dialog box, not a corner notification, and it may open behind the terminal. In WSL, powershell.exe must be in the PATH for interoperability with Windows.
macOS: if nothing appears
The osascript sends the notification through the Script Editor app. If it lacks permission, the command fails silently and macOS does not ask anything. Run osascript -e 'display notification "test"' once, open System Settings > Notifications, enable Script Editor, and run again.
Confirm and test
At the Claude Code prompt, type /hooks. The new hook should appear under Notification. To test, press Esc, use Shift+Tab until the status bar shows manual mode on, ask for something that requires permission, and switch windows. The notification should arrive.
Filter by alert type
An empty matcher fires on all notification types. To restrict it, use a value such as:
permission_prompt: Claude needs you to approve a tool, and the prompt has been waiting for about six seconds.idle_prompt: Claude finished responding about 60 seconds ago and you have not typed since then.auth_success: authentication completed.elicitation_dialog: an MCP server opened a form and you have not typed for about six seconds.
The official guide warns that the permission_prompt timing differs between the terminal and hosts like the desktop app and the VS Code extension. See the Notification event reference for both timings.
Hook 2: Automatic Prettier after every edit
Now for the formatting problem. The PostToolUse event runs after a tool finishes successfully. With the matcher Edit|Write, the hook only fires after tools that edit files. Add to .claude/settings.json at the project root:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "f=$(jq -r '.tool_input.file_path') && npx prettier --write --ignore-unknown \"$f\""
}
]
}
]
}
}The command reads the JSON that Claude Code sends via standard input, extracts the edited file path with jq, and passes it to Prettier. Install jq with brew install jq on macOS or apt-get install jq on Debian and Ubuntu.
To test, ask Claude to add a line with single-quoted strings to a JavaScript file and open the file. With Prettier's default settings, the hook rewrites them to double quotes. When the hook works, Claude Code shows nothing in the conversation; the proof is the reformatted file.
If you want to reformat a file even when a Bash command changes it, the documentation suggests using the FileChanged event.
Hook 3: Re-inject context after compaction
When the context window fills up, compaction summarizes the conversation to free space, and important details can be lost. A SessionStart hook with the matcher compact runs after each compaction. The text the command writes to standard output enters Claude's context:
{
"hooks": {
"SessionStart": [
{
"matcher": "compact",
"hooks": [
{
"type": "command",
"command": "echo 'Reminder: use Bun, not npm. Run bun test before committing. Current sprint: auth refactor.'"
}
]
}
]
}
}Replace echo with any command that generates dynamic output, such as git log --oneline -5 to show recent commits. To inject context in every session, the documentation recommends considering CLAUDE.md. The guide on AGENTS.md helps decide what goes in an instruction file and what goes in a hook.
When this actually helps
Hooks pay off most when you let the agent work while you look at something else. If you already run two isolated sessions, as in the tutorial on Claude Code and Codex together in the same repository, knowing which one stopped makes a difference. And to control what the agent can do without asking, see sandbox and approvals in Codex CLI. To prevent edits to sensitive files, also see how to block edits to .env with a PreToolUse hook.
Frequently asked questions about Claude Code hooks
Where do I put Claude Code hooks?
In ~/.claude/settings.json to apply globally, or in .claude/settings.json at the project root. The event goes inside the hooks object, as a sibling of the other events.
How do I confirm the hook was registered?
Type /hooks at the Claude Code prompt. The new hook appears in the list under the event name.
Can I get alerts only when it asks for permission?
Yes. Replace the empty matcher with permission_prompt. To be notified when it finishes and you haven't typed for about 60 seconds, use idle_prompt.
Does notify-send work on a server or in a container?
Generally no. It requires a desktop notification daemon, which headless servers, SSH sessions, and most containers lack.
Conclusion
Three JSON blocks in settings.json solve three annoying problems: the agent stuck without you knowing, badly formatted code, and context lost during compaction. Register each hook, verify with /hooks, and test with a simple request before trusting it.
Related articles

Claude Code: how to block edits to .env and protected files with a hook
Use a PreToolUse hook with exit 2 to prevent Claude Code from editing .env, package-lock.json, and .git, and to block dangerous Bash commands. Includes the limits of this protection.

How to use Claude Code and Codex together in the same repository without conflicts
Step-by-step guide to running Claude Code and Codex CLI in the same project: one implements in an isolated worktree while the other reviews in read-only mode, without stepping on each other's files.

How to test Gemini 4 Hydrogen today: access, pricing, and tasks
Access Gemini 4 Hydrogen today, understand the pricing, and see the tasks to test.
