Automating agent events with hooks
For engineers
Running format, test, context and block steps automatically when a coding agent starts, calls a tool or stops
110 questions across four stages: Understand, Apply, Solve and Judge.
Try 5 questions
Practice items, not counted towards your mastery. No account needed.
Check yourself · 1 of 5
Which event can stop a tool call before it runs?
Check yourself · 2 of 5
Which input should an after-edit formatting hook use to find the file?
Check yourself · 3 of 5
Why is a pre-tool hook stronger than a line in AGENTS.md saying "never edit .env"?
Check yourself · 4 of 5
A session-start hook is best used for which kind of fact?
Check yourself · 5 of 5
A hook meant to block an edit exits with code 1. What happens in Claude Code?
Topics
- The agent lifecycle events hooks attach to5 min read · Session start, prompt submit, before and after tool use, stop and subagent stop, with matchers and the JSON a hook receives
- Running formatters and tests after agent edits5 min read · After-edit hooks that format files, run fast checks and feed failures back to the agent without slowing every step
- Blocking risky tool calls with pre-tool hooks6 min read · Deterministic deny decisions for protected files and dangerous commands, and why a hook is stronger than an instruction
- Injecting context at session start and prompt submit5 min read · Adding branch, ticket or environment facts automatically, and keeping injected text short
- Debugging hooks that fail, stall or loop6 min read · Debug logs, exit codes, timeouts, slow hooks and stop hooks that keep the agent running forever
- Reviewing hooks as code that runs with your permissions6 min read · Hooks run shell commands as you, so hooks from a cloned repo or plugin need the same review as any script
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.
Helps to know first
110 questions · last verified 2026-12