The Complete Claude Guide
Chapter 17 · CLI

Hooks: automation on the session lifecycle

Hooks are commands that run automatically on defined events in the session lifecycle: before a tool, after a tool, on messages and more. They enforce quality and policy without relying on memory.

Verified against source on 2026-09-30

Video

Steps

  1. Learn the event types you can catch: before and after tool runs, session start, messages and more, per the hooks reference.
  2. Write a simple quality hook: for example, run a linter after a file edit so code is never saved unchecked.
  3. Hooks can block an action or add a message; use this to enforce policy such as forbidding deletion of certain files.
  4. Test a hook in a trial session before enabling it on real work, because a wrong hook blocks legitimate work.

Mechanism animation (illustration)

Common pitfall: A hook firing on every event without filtering bloats and slows; catch only the events and files that truly matter.
Hands-on exercise: Write a hook that runs a light check (echo or a linter, for instance) after an edit, and verify it fires at the right place and time.
Filmed demo · Video is added in the media phase

Recap

Comprehension check

What are hooks for?

Recap

Official sources

← Previous Back to contents Next →