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

02Step-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"
    }
  ]
}

Start shipping
better products.

Join 100,000+ product managers who use ChatPRD to write better docs, align teams faster, and build products users love.

Free to start
No credit card
SOC 2 certified
Enterprise ready