AI Briefing
KO

Enforcing Style in Claude Code with Hooks

·2026.08.30 00:49

Key point

A technique using Stop Hooks to immediately fix lint errors in Claude Code, instead of relying on CLAUDE.md instructions, has been introduced.

Details

In Claude Code, the method of placing instructions in CLAUDE.md to enforce code style has limitations, as it only applies when the model remembers them and consumes context costs every session. Consequently, an alternative approach using Stop Hooks, which execute commands whenever the model finishes a task, has been proposed.

How Stop Hooks Work

Stop Hooks execute a specified command after Claude writes code. If that command exits with exit status 2, the stderr content is passed to the model. This allows Claude to read its own lint errors and fix them autonomously before the user sees the diff. When using a single formatter, the setup is completed with one line: prettier --write . || exit 2.

File Routing with stagelint

When different tools (eslint, tsc, ruff, etc.) need to be applied based on various file types, you can utilize stagelint, which maps file globs to commands. Since stagelint shares the same configuration file (.stagelint.yml) as git pre-commit execution tools, both commit hooks and Claude Hooks can be managed with a single configuration.

Configuration Example and Precautions

Register stagelint --unstaged --quiet || exit 2 in the Stop Hook of .claude/settings.json to configure Claude to check only the unstaged changes it just modified. The --quiet flag minimizes CLI output to save tokens. Additionally, providing immediate feedback upon each file write via the PostToolUse Hook is possible, but for full-project checks (such as tsc), the Stop Hook may be more suitable.

This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.

Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.