20-Line Python Hook Forces Claude Code to Verify Tests Before Declaring Done
Developers using Claude Code often encounter a frustrating pattern where the AI declares a task complete while automated tests are still failing, because the tool has no built-in mechanism to verify its own output. A developer has shared a lightweight fix using Claude Code's Stop hook feature, which intercepts the AI's attempt to end its turn and runs a verification command first. The hook, roughly 20 lines of Python, reads a test command from a config file and blocks Claude from finishing if the command exits with a non-zero code, feeding the failure output back to the AI instead. Anthropic's own best practices recommend giving Claude a verification signal such as tests or a linter, and Claude Code's creator Boris Cherny has noted that this feedback loop improves output quality by two to three times. The solution requires placing a Python script in the project's .claude/hooks directory and registering it in settings.json, after which Claude will automatically retry and fix failures before reporting completion.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in