Sub topic 4
The hook contract: event in, exit code and message out
A hook is a small program that the agent runs at a fixed point in its loop. This chapter covers the contract between the agent and the hook. It assumes you can read JSON, write a short Python script, and use a shell. It also assumes you know that an agent calls tools such as shell commands and file edits.
The three parts of the contract
Hook systems such as Claude Code and Kiro CLI follow the same basic convention:
- Event in. The agent writes a JSON description of the proposed tool call to the hook's standard input (stdin).
- Exit code out. The hook's exit code tells the agent what to do.
- Message out. Text the hook writes to standard error (stderr) goes back to the agent as the reason.
The exact field names differ between products, so check your agent's documentation. The examples here use tool_name and tool_input.
The event
For a shell command, the event looks like this:
{
"tool_name": "shell",
"tool_input": {
"command": "rm -rf /"
}
}tool_name says which tool the agent wants to call. tool_input holds that tool's arguments. A file-write tool would carry a path and content instead of a command.
The exit code
| Exit code | Meaning |
|---|---|
| 0 | Allow. The tool call proceeds. |
| 2 | Block. The tool call does not run, and stderr is shown to the agent. |
| Other non-zero | Hook error. Behavior depends on the agent. Many agents let the call proceed. |
The last row matters for safety. If your script crashes with an uncaught exception, Python exits with code 1. An agent may treat that as a warning and run the tool anyway. A later chapter on failing closed shows how to avoid this.
A minimal blocking hook
This hook reads the event, checks the command against deny patterns, and exits with code 2 if one matches.
#!/usr/bin/env python3
import json
import re
import sys
BLOCKED_PATTERNS = [
r"\brm\s+-[a-zA-Z]*r[a-zA-Z]*f[a-zA-Z]*\s+/(\s|$)",
r"\bgit\s+push\b.*--force",
r"\bdrop\s+table\b",
]
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}. Use a safer alternative.", file=sys.stderr)
sys.exit(2)
sys.exit(0)Three details are worth noticing:
- The hook reads stdin once with
json.load. The event is the only input it gets. - The message goes to stderr, not stdout. Many agents ignore stdout for blocking decisions or use it differently.
- The message names the problem and suggests a next step. The agent reads it and can choose another approach.
Try it without an agent
You can test the contract by piping an event into the script and reading the exit code. Save the script as guard.py.
echo '{"tool_name":"shell","tool_input":{"command":"rm -rf /"}}' | python3 guard.py
echo "exit code: $?"
echo '{"tool_name":"shell","tool_input":{"command":"ls -la"}}' | python3 guard.py
echo "exit code: $?"The first run prints the "Blocked" message and exit code: 2. The second prints only exit code: 0.
Common mistakes
- Printing the reason to stdout instead of stderr, so the agent never sees it.
- Exiting with code 1 to mean "block". Use 2.
- Assuming
tool_inputalways has acommandkey. File tools do not. - Writing extra output to stdout that the agent may try to parse.
Exercise
A hook prints "Blocked: do not delete the repo" to stdout and then calls sys.exit(1). The agent runs the command anyway. Give two reasons this can happen and how to fix them.
Show answer
Exit code 1 is not the blocking code in this convention, so the agent treats it as a hook error and may continue. Also, the message went to stdout, but the agent reads the reason from stderr. Fix both by printing with file=sys.stderr and exiting with sys.exit(2).