What are hook matchers in Claude Code and Codex?
By Flavio
A hook matcher decides which occurrences of an agent event run a script. See how Claude Code, Codex, and Grok Build match tool names and other fields.
A hook matcher is a filter that decides which occurrences of a coding agent event run a hook. For example, it can run a script after an editing tool finishes and skip calls to tools that only read files.
How I use the same Git work log with Codex and ChatGPT shows the whole setup, including the script that decides which shell commands it cares about.
How hook matchers work
The event tells the agent when to look for hooks. The matcher tests a value from that event, such as a tool name or the reason a session started.
In coding tools
In Claude Code, Edit|Write matches either tool name exactly. A pattern like Edit.* uses regular-expression matching and also matches NotebookEdit. The event determines the value being tested.
In Codex, PreToolUse matches tool names and aliases. SessionStart matches the start source, such as resume, while Stop ignores the matcher.
The Cursor release notes name beforeShellExecution. Cursor’s hook reference now points to Grok Build’s Agents Window docs, so check the matcher syntax for the app you’re running.
In Grok Build Terminal UI, the tool matcher is a regular expression. Grok maps Claude tool names such as Bash to its own names. In the Agents Window, beforeShellExecution can match the full shell command text.
Details checked against vendor documentation in December 2026.
A real example
The Codex work log uses this field in a PreToolUse group:
{
"matcher": "^Bash$"
}
^ and $ anchor the pattern at the start and end, so the matcher selects the tool named Bash. The hook script then checks the command for git commit before it installs the Git work log hook.
When to use a matcher
Use a matcher when your script only needs one kind of event. A formatter can run on edits without starting up for every file the agent reads.
Pitfalls
Check what the event passes to the matcher. A tool name and a shell command are different strings. For Codex’s PreToolUse, put checks for text like git commit in the script that receives the tool input.
A formatter attached to Claude Code’s Edit|Write tools will miss edits made through Bash. The Claude Code hook reference documents FileChanged for watching selected files.
Related on flaviocopes.com
- The complete guide to Claude Code shows how hooks receive tool calls and handle the result.
- Git hooks for coding agents covers hooks attached to Git operations.
- Work Tracebook gives you a place to review what agents did across projects.
Related glossary entries
- Hooks: Hooks run your own scripts when a coding agent calls a tool or stops. How hooks work in Claude Code, Codex, Cursor and Grok Build, with examples.
- Compaction hooks: Compaction hooks run around a coding agent's conversation summary. See how to add instructions before compaction or log the event after it finishes.
- Agent work log: An agent work log records completed changes across coding sessions. See how it differs from chat history, what to record, and how Git can supply the entries.
All AI glossary entries → · AI for coding hub →
Want me to talk about your product? You can sponsor this site.