Hooks and Plugins
On this page8 items
In 10 seconds
A hook is a shell command that always runs, regardless of Claude’s judgment.
A skill is “instructions for Claude to read,” whereas a hook is guaranteed to run. That’s the difference.
Back to the point made in No.38: CLAUDE.md is context, not enforcement.
If there’s something you need done “every single time,” don’t write it down and ask nicely. Make it run as part of the system. That’s what hooks are.
When to use a hook#
There’s just one dividing line.
| When it takes effect | |
|---|---|
CLAUDE.md | Claude reads it and probably follows it |
| Skills | Read when they’re invoked |
| Hooks | Always run at a defined moment |
Make hooks out of things where “probably follows” isn’t good enough. Formatting after edits, linting before commits, blocking writes to sensitive files.
The official documentation says it plainly: hooks run whether or not Claude decides to, so if you need certainty, use a hook.
When can they run?#
These are the officially listed events.
| Event | When |
|---|---|
SessionStart | When a session starts or resumes |
UserPromptSubmit | Right after you send a prompt, before it’s processed |
PreToolUse | Before a tool runs. Can block it |
PostToolUse | After a tool runs successfully |
PostToolUseFailure | After a tool run fails |
Stop | When Claude finishes responding |
SubagentStop | When a subagent finishes |
PreCompact | Before context compaction |
SessionEnd | When a session ends |
The fact that PreToolUse can block is what matters. You can reject dangerous operations before they run. The “make it impossible in the first place” idea from No.21 becomes real here.
Example: trimming output#
The example given in the official docs is easy to follow, so here it is.
A hook that trims test output down to just the failures. Instead of Claude reading 10,000 lines of logs, the hook returns only the ERROR lines. The context shrinks from tens of thousands of tokens to a few hundred.
This is also an implementation of “don’t overstuff” from No.24, and a cost saving from No.44. It’s worth keeping in mind that hooks are a cost tool, too.
Plugins#
A plugin is a way to bundle and distribute skills, agents, hooks and MCP servers together.
Standard placement (.claude/) | Plugin | |
|---|---|---|
| How you invoke it | /hello | /plugin-name:hello |
| Best for | Personal work, quick experiments | Sharing with a team, distribution, version management |
The official recommendation is practical: start by building and using it in .claude/, and convert it to a plugin once you want to share it. There’s no need to make a plugin up front.
The contents are determined by directory layout: skills in skills/, agent definitions in agents/, hooks in hooks/hooks.json, MCP servers in .mcp.json. The manifest goes in .claude-plugin/plugin.json.
The docs also call out a common mistake: putting skills/ or hooks/ inside .claude-plugin/. Only plugin.json goes in .claude-plugin/.
Distribution happens through marketplaces. Anthropic runs an official one and a community one, and for internal use you can set up a marketplace in a private repository.
About safety#
Hooks run shell commands. Naturally, that’s also where the danger lies.
- Don’t enable hooks written by someone else without reading them
- Verify where a plugin comes from (same as in No.32)
- Hooks in a settings file run as soon as you trust that folder
The point from Chapter 3, No.21 applies here too: every connection you add is one more entry point for things coming from outside.
Try it: Find the tasks that must always run (3 minutes)
Prompt
“`
In my project, list the tasks that should always run without being left
to Claude’s judgment. Limit it to the kinds of things that might not be
followed even if written in CLAUDE.md.For each one, also tell me which hook event it should be implemented with.
Don’t implement anything yet.
“`Expected result: Candidates and event names. Formatting after edits maps to
PostToolUse, and blocking dangerous operations maps toPreToolUse.If it doesn’t work: If no candidates come up, you don’t need hooks yet. Come back the moment you find yourself saying the same thing twice.
Common pitfalls#
Trying to build hooks from the start. The order is CLAUDE.md → skills → hooks. Build one after you have a track record of it being ignored.
Adding too many hooks. They run every time, so things slow down. Only what you truly need.
Using someone else’s hooks without reading them. Shell commands will run. Read what’s inside.
Summary#
- Hooks always run, regardless of judgment. If you need certainty, this is the place
PreToolUsecan block operations, making “impossible in the first place” a reality- Hooks that trim output reduce both context and cost
- Plugins are a way to bundle and distribute. Build in
.claude/first, then convert