Hooks for coding agents: run your own checks around agent actions
Configure hooks, the commands a coding agent runs at defined points in its work. Attach formatters, linters and loggers to events that fire after an edit or tool call, and put blocking checks on the event before a tool runs, where a hook can deny the call. Event names, file locations and failure behavior differ between Claude Code, Cursor, Codex and Copilot, so set up and test each tool separately.
Reviewed November 2026
What can a hook do that an instruction cannot?
An instruction asks the model to behave. A hook is a program the agent's runtime starts. GitHub's docs say hooks run code rather than asking the model to follow a rule. Claude Code's docs draw the same line: permissions and hooks enforce limits regardless of what Claude decides, while CLAUDE.md tells Claude how the project works.
The interface is JSON over standard streams. Cursor describes hooks as spawned processes that communicate over stdio using JSON in both directions. In Claude Code, command hooks receive JSON data via stdin and report results through exit codes, stdout and stderr.
Which events does each tool offer?
Each tool names its events differently. Check a script written for one tool against the next tool's event list before you reuse it.
- Claude Code's PreToolUse hook returns its decision inside a hookSpecificOutput object, with four outcomes (allow, deny, ask or defer) and the ability to modify tool input before execution.
- PostToolUse and Stop events in Claude Code use top-level decision and reason as their current format.
- Cursor's agent hooks include beforeShellExecution and afterShellExecution for shell commands, beforeMCPExecution and afterMCPExecution for MCP tools, and beforeReadFile and afterFileEdit for file access and edits.
- Codex runs PreToolUse, PermissionRequest, PostToolUse, PreCompact, PostCompact, UserPromptSubmit, SubagentStop and Stop during a turn.
- GitHub Copilot fires sessionStart when a session begins or resumes and agentStop when the main agent has finished responding, and Copilot cloud agent supports a subset of the CLI events.
How do I block writes to a protected path?
Put the check on the event that fires before a tool runs and have it deny the call. In Claude Code, a UserPromptSubmit hook that blocks by exiting 2 has its stderr text treated the same way as the block reason, which is shown to the user. Scope the hook with a matcher. Claude Code's docs say to use a single string with | to match multiple tools, for example "Edit|Write", and matching is case-sensitive, so tool names take capitals: Bash, Edit, Write.
In Cursor, preToolUse fires for all tools, and exit code 2 from command hooks blocks the action, which Cursor says matches Claude Code behavior. GitHub Copilot's preToolUse runs before any tool, such as bash, edit or view, and can approve or deny the call. Under cloud agent, an ask decision is treated as deny because no user is available to answer. The Codex matcher field is a regex string that filters when hooks fire.
Test what your guard does when it crashes or stalls. VS Code's Copilot docs say GitHub documents command preToolUse errors as denying the tool call, but timeouts as falling through to normal permissions. For Codex hooks under cloud orchestration, a PreToolUse error, timeout, or malformed response can let the tool run.
A hook also covers gaps in permission rules. Claude Code notes that a Bash(rm *) deny rule doesn't block /bin/rm or find -delete, and points to a PreToolUse hook or the sandbox for a hard guarantee.
How do I run a formatter or linter after edits?
Attach the command to an event that fires after the edit. Cursor lists running formatters after edits as a hook use, and its quickstart registers ./hooks/format.sh under afterFileEdit. Cursor says your hook runs after every file edit and that it reloads hooks config files automatically.
Claude Code's plugin docs show a formatting script on PostToolUse with the matcher "Write|Edit". GitHub's how-to describes hooks that run linting, formatting, or security scans at key points during agent execution.
Codex's hooks page gives a custom validation check when a chat turn stops as one use, for enforcing standards. Pair a lint hook with a review loop so failures reach a person or another agent.
How do I log every command the agent runs?
Pick the shell or tool event and append one line per call. In Copilot, postToolUse runs after a tool completes, whether it succeeded or failed, and can log execution results. Cursor's afterShellExecution covers shell commands. Codex lists the option to send the chat to a custom logging/analytics engine.
Keep the logger fast and quiet. GitHub says hooks run synchronously and block agent execution and suggests keeping execution under 5 seconds when possible. It also says to never log sensitive data such as tokens or passwords. VS Code's Copilot docs say a command hook can return one final JSON output object on stdout, with diagnostic text on stderr.
For Copilot cloud agent, ship logs out of the run. GitHub says those hooks run in an ephemeral Linux sandbox that is destroyed when the job ends.
Where do hook files live in each tool?
- In Claude Code, hooks go under the "hooks" key in a settings file, and there is no standalone hooks file for project or user config. Edits are normally picked up automatically by the file watcher.
- Cursor reads hooks.json files at the project or user level, or hooks from plugins, and cloud agents pick up .cursor/hooks.json at the root of your project.
- Codex skips project-scoped .codex/ layers, hooks included, when you mark a project untrusted. For plugins, it looks for hooks/hooks.json inside the plugin root by default.
- GitHub Copilot CLI loads repository-level hook files from .github/hooks/*.json in the repository root. For Copilot cloud agent, the hooks file goes in the .github/hooks/ folder and must be on the default branch of the repository.
Hooks from several sources stack. In Codex, matching hooks from multiple files all run, and matching command hooks for the same event launch concurrently. Copilot CLI says that when an event appears in several sources, all hook entries from all sources are run. When several agents share one repo, check for duplicate formatters, and see running Claude Code and Codex together.
Why is my hook not firing, and how do I switch hooks off?
Start with the listing. In Claude Code, type /hooks to open a read-only browser of configured hooks. If the hook appears but doesn't fire, the docs name the matcher as the usual cause, and they say to start a session with claude --debug and trigger the tool call to watch hook evaluation live.
For Copilot, GitHub's troubleshooting table says to verify the JSON file is in .github/hooks/ and that version 1 is set. Its default timeout is 30 seconds, raised with timeoutSec. Codex says non-managed hooks must be reviewed and trusted before they run, and a plugin update that changes a bundled script requires a new review. Cursor cloud agents can start read-only, and hooks do not run during those turns.
To pause hooks in Claude Code, set disableAllHooks to true in a settings file. The docs say there is no way to disable an individual hook while keeping it in the configuration. In Codex, hooks are on by default, and you turn them off with hooks = false under [features] in config.toml. An administrator can lock Claude Code hooks to plugins and managed settings with strictPluginOnlyCustomization, and then hooks in your own settings files don't run.
What else do readers ask?
Can one hook script serve several agents?
Partly. Cursor supports loading hooks from third-party tools like Claude Code, and Copilot CLI reads cross-tool .claude/settings.json files in the repository. VS Code's Copilot docs warn that sharing a hook file location does not establish runtime compatibility, so test the script in each tool.
Do hooks run when the agent runs in the cloud?
Yes, with limits. Cursor says cloud agents run command-based hooks from your repository. In Copilot cloud agent, a subset of events fires and only bash or command entries are honored. For unattended runs, see headless coding agents.
Can an administrator enforce hooks?
In Codex, setting allow_managed_hooks_only to true skips user, project, session and plugin hooks while still loading managed ones. In Claude Code, disableAllHooks set in user, project, or local settings can't disable hooks an administrator configured through managed policy settings.
Are Copilot hooks in VS Code stable?
The VS Code docs say agent hooks is in preview and might change before it becomes generally available.