Back/Engineering/Claude Code
AdvancedEngineeringClaude Code

Automate Code Quality and Fixes with AI Stop Hooks

Implement an automated 'fix and commit' loop by configuring an AI stop hook. This hook runs quality checks on AI-generated code and automatically prompts the AI to fix any detected errors.

From How I AI

Beyond Vibe Coding: Advanced AI Engineering with John Lindquist

with Claire Vo

Automate Code Quality and Fixes with AI Stop Hooks

Tools Used

Claude Code

Claude for coding tasks

Step-by-Step Guide

1

Create a Stop Hook Script

Using the Claude Agent SDK, write a script (e.g., in TypeScript) that will run automatically every time the AI agent finishes a task.

2

Implement Quality Checks in the Script

Add logic to the script to check for file changes. If files were modified by the AI, run a quality gate command like a linter or type checker (e.g., bun typecheck).

3

Handle Check Failures Automatically

If the quality check fails, program the script to capture the error report and output a new JSON prompt instructing the AI to fix the specific errors. The script's console.log output is sent back to Claude.

Prompt:
{"prompt": "Please fix the TypeScript errors. Here is the report: ..."}
4

Handle Check Successes

If all checks pass, the script can trigger a subsequent action, such as instructing a background agent to generate a git commit message and commit the successful changes.

5

Configure the Hook in Settings

Link your script to the AI tool by adding a configuration to its settings file (e.g., settings.local.json for Claude Code), specifying the command to run the hook.

Prompt:
{
  "claude.hooks.stop": [
    {
      "command": "bun run claude-hooks/index.ts"
    }
  ]
}

Become a 10x PM.
For just $15 / month.

We've made ChatPRD affordable so everyone from engineers to founders to Chief Product Officers can benefit from an AI PM.