Topic 1 of 6 · 5 min read
The agent lifecycle events hooks attach to
Goal: Choose the right event and matcher for a given automation and read the input a hook receives for that event.
A hook is a command your harness runs for you at a fixed point in the agent's lifecycle, so the step happens every time and does not depend on the model remembering it.
Check yourself · 1 of 3
Which event can stop a tool call before it runs?
Why it matters
An instruction in a memory file such as AGENTS.md or CLAUDE.md is a request. The model usually follows it, but it can forget it or skip it. A hook is code. If you want the formatter to run after every edit, or .env to never be touched, you attach a script to an event and the harness runs it. Claude Code, Gemini CLI and GitHub Copilot's agent all offer this idea, with slightly different names.
The events that matter most
In Claude Code the main events are:
SessionStart: a session begins or resumes. Good for adding facts.UserPromptSubmit: you press enter, before the model sees the prompt.PreToolUse: the model asked to call a tool, and nothing has run yet. The only event that can stop the call.PostToolUse: the tool finished. Good for formatting and checks.Stop: the agent thinks it is done. A hook can send it back to work.SubagentStop: the same, for a subagent.
Matchers and input
For tool events you add a matcher: a pattern on the tool name. Edit|Write fires for file edits only. Bash fires for shell commands. Without a matcher the hook fires for every tool call, which is rarely what you want.
The harness passes the hook a JSON object on standard input. Common fields are session_id, cwd, hook_event_name, and for tool events tool_name and tool_input. For an edit, tool_input.file_path is the file. Your script reads the JSON, decides, and exits. Exit code 0 means carry on. Exit code 2 means block, and what you print to standard error goes back to the model.
Before and after
Weak: "Always run prettier after editing files" written in CLAUDE.md. Sometimes it happens, sometimes it does not.
Better: a PostToolUse hook with matcher Edit|Write that runs prettier on tool_input.file_path. It runs every time, and the model never has to think about it.
What goes wrong
- Choosing
Stopfor something that should happen per edit, so the work arrives far too late. - Leaving out the matcher, so a slow check runs after every
ReadandGrep. - Guessing the input fields instead of printing the real JSON once and reading it.
- Using
PostToolUseto stop something. The tool has already run by then.
Examples
Print the input once
A first hook that only records what it receives, so you can see the real fields before writing logic.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "cat >> /tmp/hook-input.log" }
]
}
]
}
}
Edit a file, then read the log. You will see tool_name, tool_input.file_path and the session fields.
Pick the event
Goal: never let the agent edit package-lock.json. Event: PreToolUse, matcher Edit|Write, because it must happen before the edit. Goal: format after each edit. Event: PostToolUse. Goal: add the current branch name to every session. Event: SessionStart.
Key points
- Choose the event by when the step must happen: before the action to stop it, after it to react, at the end to check.
- A matcher narrows a tool-event hook to the tools it cares about, such as
Edit|WriteorBash. - The hook receives a JSON object on standard input with the event name and, for tools, the tool name and its input.
- Only a pre-tool hook can prevent a tool call; a post-tool hook can only react.
- A hook is deterministic code, while a line in a memory file is a request the model may skip.
Common mistakes
- Using a memory-file instruction where a hook is needed, then being surprised when it is skipped.
- Registering a hook with no matcher so it runs on every tool call.
- Writing parsing logic before looking at a real sample of the input.
- Putting a blocking rule in a post-tool hook, where it is too late.
Check yourself · 2 of 3
What does a matcher such as Edit|Write do?
Check yourself · 3 of 3
You want a formatter to run after every file edit. Which setup fits best?
Practice items, not counted towards your mastery.
Start here
- Hooks reference (Claude Code docs) (opens in a new tab)DocsFree
Every event, matcher, input field and exit-code rule, straight from the vendor.
- Automate workflows with hooks (Claude Code docs) (opens in a new tab)DocsFree
Worked examples: formatting after edits, blocking commands, desktop notifications.
- Hooks (Gemini CLI docs) (opens in a new tab)DocsFree
Shows the same idea in another agent, so you can compare event names and decision formats.