Lesson 14 / 25

What Hooks Are

Explain how hooks differ from prompt instructions and where they are configured.

Instructions vs enforcement

A line in a prompt like "always run the formatter" is a request, and the model may forget it. A hook is a shell command that the harness itself runs on events such as before or after a tool call. In Claude Code hooks are configured in settings.json, and the command receives event details as JSON on standard input.

Rules the model cannot forget

A hook is code that runs at a fixed moment in the agent's lifecycle, so the rule is enforced rather than requested.

Three moments: before a tool, after a tool, at stop.
Figure 5.1 — Before, after and at stop.

Speed bump vs sign

A "slow down" sign asks drivers to be careful. A speed bump makes them. Hooks are the speed bump.

Hooks run with your permissions

A hook is arbitrary code on your machine. Read any hook config you copy from someone else, and keep hook scripts in version control so changes are reviewed.

Quick check: How is a hook different from a prompt instruction?

  • It is only a suggestion
  • It runs automatically as code at an event
  • It needs the model to remember it
  • It cannot run shell commands
Answer

It runs automatically as code at an event — The harness executes hooks itself, so they work even if the model forgets.