Told Claude Code to lint after every edit and it forgot? Use a hook
Hooks in Claude Code are commands that run on their own at set points, such as after every file edit or before any tool. Put the rule in a hook instead of asking for it in words, and use exit 2 to block the tool or send the error back to Claude.

Claude Code writes a whole file in seconds, and it can forget to run the linter after it. The reason is not a weak model: you set the rule as a request in words, and a request gets done or it does not. This article shows how to turn the rule into a command that runs on its own every time, without anyone having to remember.
What are hooks in Claude Code?
Hooks in Claude Code are commands you write that run on their own at set points in the tool's work, such as before a tool is used or after a file is edited. Nobody asks for them, and Claude does not decide whether to run them.
The hooks page in the Claude Code docs defines them like this: "Hooks are user-defined shell commands, HTTP endpoints, MCP tool calls, LLM prompts, or subagents that execute automatically at specific points in Claude Code's lifecycle".
For a business owner: it is the difference between telling an employee "close the door when you leave" and fitting the door with an automatic closer.
Why isn't a request in words enough?
A request in words is not enough because it goes through the model's judgment, and a rule that must happen every time should not be a judgment call. When you write "run npm run lint after every edit" in the instructions, you are betting that the request will be remembered on every edit, even late in a long session.
A hook flips this: the command is tied to the event itself. Every time an edit happens, it runs.
Which event do you use: PreToolUse or PostToolUse?
Use PreToolUse when you want to stop something before it happens, and PostToolUse when you want to check something after it happened. The docs list many events, but these two are the ones you need most in daily work.
Event | When it runs | Can it block the tool? |
|---|---|---|
PreToolUse | Before the tool call runs | Yes |
PostToolUse | After the tool call succeeds | No, the tool already ran |
There are others, such as SessionStart when a session begins, and Stop when Claude finishes responding.
How do you write a hook that runs lint after every edit?
You put it in a settings file under hooks, with a matcher that sets which tools trigger it. This example is from the docs themselves, and it runs a lint script only when Claude writes or edits a file:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "/path/to/lint-check.sh"
}
]
}
]
}
}Where you put it depends on what you want it to cover:
~/.claude/settings.json: all your projects..claude/settings.json: a single project..claude/settings.local.json: a single project, and it is added to.gitignorewhen Claude Code saves a setting to it.
Why is exit code 2 the one that matters?
Exit code 2 is the code that makes a hook count: in PreToolUse it blocks the tool, and in PostToolUse it shows the error to Claude so it can fix it. Exit 0 means success, and the work goes on.
The trap is that 1 is the usual failure code in any script, but the docs warn that exit 1 without valid JSON output is a non-blocking error: "If your hook is meant to enforce a policy, use exit 2." So the lint-check.sh script has to exit with 2 when the check fails, and write the errors to stderr:
#!/bin/sh
npm run lint --silent 1>&2 || exit 2This way every time Claude edits a file and a lint error shows up, the error goes back to it on its own, and it fixes it in the same turn.
Where do you start now?
The lesson for you as a developer working with an agent: a rule that must happen every time goes in a hook, and requests in words are for things that need judgment.
Write a list of what you ask Claude Code for every time: lint, formatting, a quick test.
For each one, decide: before the tool (PreToolUse) or after it (PostToolUse).
Write the script, and make it exit with 2 when you want Claude to stop or fix.
Put it in
.claude/settings.jsonin the project, and try a small edit.
If a secret from your .env files ever reaches a place it should not, we wrote about rotating secrets after a leak. On instructions that can reach your agent from pages and files, read prompt injection in agent tools. And all our skills for coding agents are in the AI skills section.
What is the first rule you will turn into a hook?
FAQ
What is the difference between exit 1 and exit 2 in a hook?
The docs say exit 1 without valid JSON output is a non-blocking error, even though 1 is the usual failure code. If your hook is meant to enforce a policy, use exit 2.
Can I stop a file edit after it happened with PostToolUse?
No. In PostToolUse the tool already ran, and exit 2 only shows the error to Claude. To stop a tool before it runs, use PreToolUse.
Where do I put a hook so it runs in all my projects?
In your user settings file, ~/.claude/settings.json. If you want it for one project only, put it in .claude/settings.json inside the project.


