G-18Hooks & Lifecycle Events

/coding-agents/hooks

Scripts the harness runs at fixed moments in a session — session start, before a tool call, after it, at the stop — regardless of what the model decides.

Step-by-step walkthrough7 steps
  1. A session has fixed moments: it starts, it is about to call a tool, the call returns, it declares itself done.
  2. Instructions can ask for the same things — but the model weighs them against everything else, and forgets.
  3. A hook is a script the harness runs at the moment itself. At session start: put the branch and open task in context.
  4. Before a tool call, a guard sees the exact command — and can refuse it, returning the reason as context.
  5. After every edit, the formatter and linter run. Not when the model remembers — every time.
  6. When the agent says it is done, the tests run first. Red means the turn cannot end.
  7. Instructions shape what the model tries to do; hooks decide what happens regardless.
Key insight

An instruction is a request the model weighs against everything else in its window; a hook is code that runs at the moment itself. Anything that must happen every time, such as formatting, a blocked path or tests before “done”, belongs in a hook.

Failure mode

Slow or noisy hooks. A thirty-second check after every edit, or a hook that pours a thousand lines of lint back into context, taxes every step of every session.

Further reading

Elsewhere in the atlas