Using Pre-Commit Hooks to Stop AI Coding Agent Drift
Geoffrey Huntley explains how to use Git pre-commit hooks to automatically catch AI coding agent drift and inject corrective prompts. When an agent bypasses preferred libraries, the hook fails the commit and feeds a corrective instruction back to the agent. This technique provides a practical, automated way to steer AI coding agents toward preferred architectural patterns and prevent them from dropping constraints over long tasks. It helps developers maintain strict code standards without manually policing every line generated by generative AI. The approach inspects the Git diff during a pre-commit check to spot direct calls to forbidden modules, such as bypassing a custom diagnostics library. If a violation is found, the commit is blocked and a prompt is dynamically injected to guide the agent toward correct usage.
## BACKGROUND
Git pre-commit hooks are scripts executed by Git before a commit is finalized, commonly used for linting, formatting, or running tests. AI coding agent drift occurs when large language models gradually lose track of strict project constraints or preferred libraries during extended coding tasks.