If you let Claude Code work on its own, set up two hooks in the project’s .claude/settings.json: a PostToolUse hook that formats every file the agent edits, and a Stop hook that runs the tests and won’t let it wrap up while they fail. It’s about 30 lines, and it lives in the repo.
The problem: “done” doesn’t mean it works
When you run two or three agents at once, you can’t check every “I’m done” as it happens. The agent usually remembers to run the tests, but usually isn’t always. When it forgets, you find out while reviewing the pull request, long after you’ve moved on to something else.
You can ask for it in CLAUDE.md, but that’s an instruction the model may or may not follow. What you want is a rule that holds every time, whatever the model thinks.
What hooks are
The official guide describes them as shell commands that Claude Code runs “at specific points in its lifecycle”. The important word is deterministic: the hook always runs, not when the model decides it should.
There are more than twenty events. Two are enough here:
PostToolUsefires after a tool call succeeds. With theEdit|Writematcher, only after files are edited or created.Stopfires when Claude finishes responding. If the hook exits with code 2, the reference says Claude is prevented from stopping and keeps working, using whatever the hook wrote to stderr as its next instruction.
Where you put them decides who they affect. ~/.claude/settings.json applies to all your projects, on your machine only. .claude/settings.json applies to one project and gets committed, so anyone who clones the repo inherits it, even if the team is you and your agents. .claude/settings.local.json is per project but not shared.
How to set it up
1. Format every edited file. This is the Prettier example from the docs. The hook receives JSON on standard input, pulls out the path with jq and hands it to the formatter:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}
]
}
]
}
}
You need jq installed. If you use another formatter, or a linter with auto-fix, change only the end of the command.
2. Write the script that guards the finish line. Save it as .claude/hooks/tests-before-stop.sh and make it executable with chmod +x:
#!/bin/bash
INPUT=$(cat)
# If we already forced one extra pass, let it stop to avoid a loop
if [ "$(echo "$INPUT" | jq -r '.stop_hook_active')" = "true" ]; then
exit 0
fi
if ! npm test > /tmp/claude-tests.log 2>&1; then
echo "Tests are failing. Fix them before you call this task done:" >&2
tail -n 40 /tmp/claude-tests.log >&2
exit 2
fi
exit 0
Exit code 2 is what blocks. Whatever you write to stderr reaches Claude as the reason, so send it the last lines of the failure, not just “something broke”.
3. Register it in the same .claude/settings.json, next to the first one:
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/tests-before-stop.sh"
}
]
}
]
}
}
4. Check that it’s live. Type /hooks inside Claude Code to see your hooks grouped by event.
Three details worth knowing
The loop has a brake. Claude Code overrides a Stop hook that blocks eight times in a row with no tool call from the agent in between. The guide also recommends reading the stop_hook_active field and exiting when it’s true, as the script does. With that check, the agent gets one extra pass to fix the tests; drop it and the hook keeps insisting until they pass or the cap of eight kicks in.
Formatting doesn’t see changes made from the shell. The Edit|Write matcher doesn’t fire when the agent changes a file through a Bash command, and the guide says so. The Stop hook still covers it, because it runs the whole suite against whatever is on disk.
Mind the clock. A command hook times out after 600 seconds by default. If your suite takes longer, raise the timeout field, or run only the fast tests in the hook and leave the rest to CI.
There’s a script-free alternative: an agent hook, which spawns a subagent that runs the tests and decides. The docs mark it as experimental and recommend command hooks for production workflows. I’d stick with the script.
My take
I’d add this to any repo where an agent works without me watching every step. The agent decides how to solve the task; the hook decides when it’s finished. That split is what lets me keep several sessions open and only review pull requests that are already green.
I like how little it takes: one JSON file, a ten-line script and nothing to install beyond jq. And I like that it lives in the repo, because the rule travels with the code instead of depending on each session reading CLAUDE.md carefully.
The limit is clear: the hook guarantees the tests pass, not that the tests are any good. If the agent deletes an inconvenient test or writes one that checks nothing, the hook lets it through. Reviewing the pull request is still your job, but you start it with one less question.
Sources
If you want to see how I work with AI agents without a team, follow me on LinkedIn.
