Sub topic 3
Where Hooks Run in the Agent Lifecycle
This chapter assumes you can read JSON, write a short Python script, and use a shell. You should also know that an AI coding agent works by calling tools, such as running shell commands or editing files.
The loop that hooks attach to
An agent works in a loop. It reads the conversation, decides on a next step, and asks to call a tool. The agent program (the harness) runs the tool and feeds the result back to the model. The loop repeats until the task is done.
A hook is a script that the harness runs at a fixed point in this loop. The model does not run hooks, and it cannot skip them. The harness runs them as ordinary programs, outside the model's control.
The main lifecycle events
Most hook systems offer a few events. Names differ between agents, so check your agent's documentation. The common ones are:
| Event | When it runs | What it can do |
|---|---|---|
| Before tool use (pre-action) | After the model proposes a call, before the tool runs | Allow or block the call |
| After tool use (post-action) | After the tool has finished | Log, format, or report. It cannot undo what already happened |
| Session or prompt events | When a session starts or the user submits a prompt | Add context or record activity |
This tutorial focuses on the pre-action event. It is the only common point where a script can stop an action before any effect occurs.
What a pre-action hook can and cannot control
A pre-action hook sees one proposed tool call. It can:
- Read the tool name and its input, such as the shell command text.
- Allow the call by exiting with code 0.
- Block the call by exiting with code 2 and printing a reason to stderr.
It cannot:
- Change what the model decides to ask for. It only answers yes or no to each request.
- See the full effect of a command. It sees the text of the command, not what the command will do once it runs.
- Stop actions that do not pass through the harness, such as something a script spawned earlier or a human typing in another terminal.
The last two points matter later, when we discuss the limits of hooks.
A first look at the hook script
The harness starts your script, sends the event as JSON on standard input, and reads the exit code. Here is a hook that blocks a risky command:
#!/usr/bin/env python3
import json
import re
import sys
BLOCKED_PATTERNS = [
r"\bgit\s+push\b.*--force",
]
event = json.load(sys.stdin)
command = event.get("tool_input", {}).get("command", "")
for pattern in BLOCKED_PATTERNS:
if re.search(pattern, command, re.IGNORECASE):
print(f"Blocked: command matches risky pattern {pattern!r}.", file=sys.stderr)
sys.exit(2)
sys.exit(0)Field names such as tool_input follow a common convention but vary between agents. Confirm them in your agent's documentation before relying on them.
Common mistakes
- Using a post-action event for protection. The action has already happened.
- Assuming exit code 1 blocks. In the common convention, only code 2 blocks the call. Other nonzero codes are often treated as a hook error, and the action may proceed. Chapter 10 covers this.
- Forgetting that the hook sees text only. A command can be written in many ways, so a hook is a check, not proof of safety.
Exercise
A teammate wants to stop the agent from deleting a build directory. They propose a post-action hook that checks whether the directory still exists and reports an error if it does not. Why does this fail to protect the directory, and which event should they use?
Show answer
A post-action hook runs after the tool has finished, so the directory is already deleted when the check runs. It can only report the damage. They should use a pre-action hook, which runs before the tool executes and can block the call by exiting with code 2.