← Table of Contents · Page 248 of 868
LIFECYCLE HOOKS
Part 2 of 11
WHAT IS A HOOK
A hook is code that runs automatically at a defined stage of the agent's lifecycle.
The word comes from the idea of hanging something on a hook.
The harness provides hooks at specific points, and you hang your own code on them.
Think about a doorbell with a camera.
Every time someone presses the button, a series of things happen automatically: the bell rings, the camera records, and your phone gets a notification.
You did not have to remember to do any of this each time.
The system did it because it was attached to the event of the button press.
Hooks work the same way.
Rather than asking the model to remember to log every tool call, you attach a logging hook that does it every time, without exception.
Rather than asking the model to remember not to delete important files, you attach a hook that blocks such actions regardless of what the model wants.
This is a crucial principle of harness engineering.
Instructions to the model are requests.
Hooks are enforcement.
A model may forget or misunderstand a request.
A hook written in ordinary code does exactly what it was told every time.
Common points where hooks attach include the start of a run, before a model call, after a model call, before a tool call, after a tool call, when an error occurs, when the agent tries to finish, and at the end of a run.
Different frameworks use different names for these points, so when you learn a new system, look for its list of hook points.
Advertisement
Read Aloud:
0:00 / 0:00