Claude Code, Codex and Cursor hooks: prompt and stop events
Claude Code, Codex and Cursor can all run a command of yours when you send a prompt and when the agent stops. Claude Code and Codex call these events UserPromptSubmit and Stop, in ~/.claude/settings.json and ~/.codex/hooks.json. Cursor calls them beforeSubmitPrompt and stop, in ~/.cursor/hooks.json. Your script gets JSON on stdin, should print nothing the agent will read, and should always exit 0.
The official docs for each agent explain its own hooks well. What they don’t show is how they line up, and what goes wrong when one script has to work in all of them. We learned that by shipping a Mac app that listens to these events in Claude Code, Codex, Cursor and GitHub Copilot. This post puts the events side by side, gives you one small script that works in every agent, and covers the problems you are most likely to hit.
Every event name, path and field below was checked against the official docs on 29 October 2026: Claude Code, Codex, Cursor and GitHub Copilot. Hooks change often, so check them again before you rely on a detail.
What a hook is, and why prompt and stop matter
A hook is a command the agent runs at a fixed point in its work. The agent starts your command, writes a JSON object about the event to its stdin, and reads its exit code and stdout. You don’t change the agent. You only tell it what to run and when.
Two of those points are useful on almost any desk: the moment you send a prompt, and the moment the agent stops. Between them, the agent is working and you are waiting. With those two events you can:
- play a sound or show a macOS notification when a long task finishes, so you can look away;
- measure how long each turn takes, per project or per day;
- start a timer for a break when the agent starts, and end it when the agent needs you.
A third kind of event, “the agent needs you”, matters as much. A turn can pause on a permission prompt long before it stops, and a notification that waits for the stop event will come too late. What to do with the wait itself is a different question, covered in what to do while Claude Code is working.
The events side by side
All four agents read a user-level file in your home folder and run hooks from it in every project. The table shows the events that matter for a turn.
| Agent | User config | Prompt sent | Agent stopped | Agent needs you | Notes |
|---|---|---|---|---|---|
| Claude Code | ~/.claude/settings.json, shared with all your settings |
UserPromptSubmit |
Stop, StopFailure (API error), SessionEnd |
PermissionRequest, Elicitation, Notification with notification_type such as permission_prompt |
async: true runs a hook in the background. Stop doesn’t fire when you interrupt. |
| Codex | ~/.codex/hooks.json, or [hooks] in ~/.codex/config.toml |
UserPromptSubmit |
Stop, Interrupt, SessionEnd |
PermissionRequest |
Each hook must be trusted in /hooks before it runs. async: true is supported. |
| Cursor | ~/.cursor/hooks.json, with "version": 1 |
beforeSubmitPrompt |
stop (with status: completed, aborted or error), sessionEnd |
None documented | Runs your Claude Code hooks too, by default. |
| GitHub Copilot CLI | Any *.json in ~/.copilot/hooks/ |
userPromptSubmitted |
agentStop, sessionEnd |
permissionRequest, notification |
Uses bash and timeoutSec fields. camelCase payloads carry no event name. |
The payloads differ in small ways. Claude Code and Codex send session_id, cwd, hook_event_name and, on a prompt, prompt. Cursor sends conversation_id, cursor_version, workspace_roots and user_email with every event, and prompt on beforeSubmitPrompt. Copilot CLI sends sessionId, timestamp, cwd and prompt when the event is named in camelCase, and snake_case fields when you name it in PascalCase, as VS Code does.
One script for every agent
This script times each turn and tells you when one that took 30 seconds or more ends. It reads the session ID from stdin, so parallel sessions don’t mix. It uses jq to read the JSON. Run which jq to check you have it; if not, install it with Homebrew (brew install jq).
Save it as ~/bin/agent-turn and run chmod +x ~/bin/agent-turn.
#!/bin/sh
# agent-turn AGENT start|stop
# Times one agent turn and tells you when a long one ends.
# Reads the hook's JSON on stdin and always exits 0.
agent=$1
mode=$2
input=$(cat)
# Cursor also runs your Claude Code hooks (Third-Party Imports).
# Cursor has its own entry below, so skip the borrowed copy.
if [ "$agent" = claude ] && [ -n "$CURSOR_VERSION" ]; then
exit 0
fi
# Claude Code and Codex send session_id, Cursor conversation_id,
# Copilot CLI sessionId.
id=$(printf '%s' "$input" |
jq -r '.session_id // .conversation_id // .sessionId // "default"' 2>/dev/null |
tr -cd 'A-Za-z0-9_-')
[ -n "$id" ] || id=default
dir="${TMPDIR:-/tmp}/agent-turns"
mkdir -p "$dir"
case $mode in
start)
date +%s > "$dir/$id"
# Cursor documents a JSON answer for beforeSubmitPrompt.
[ "$agent" = cursor ] && printf '{"continue":true}\n'
;;
stop)
started=$(cat "$dir/$id" 2>/dev/null)
rm -f "$dir/$id"
case $started in ''|*[!0-9]*) exit 0 ;; esac
took=$(( $(date +%s) - started ))
if [ "$took" -ge 30 ]; then
afplay /System/Library/Sounds/Glass.aiff >/dev/null 2>&1 &
osascript -e "display notification \"Done after $took seconds\" with title \"Your agent finished\"" >/dev/null 2>&1
fi
;;
esac
exit 0
Two details matter here. The sound and the notification send their own output to /dev/null, so nothing reaches the agent. And the last line is exit 0 whatever happened, so the hook can’t block a prompt by mistake.
If no notification appears, the Claude Code guide explains why: osascript notifications come from Script Editor, and they fail silently until you allow Script Editor in System Settings › Notifications. In the configs below, replace /Users/you with your home folder.
Claude Code
Add the two events to the hooks object in ~/.claude/settings.json. Keep everything else in the file as it is.
{
"hooks": {
"UserPromptSubmit": [
{ "hooks": [{ "type": "command", "command": "/Users/you/bin/agent-turn claude start", "async": true }] }
],
"Stop": [
{ "hooks": [{ "type": "command", "command": "/Users/you/bin/agent-turn claude stop", "async": true }] }
]
}
}
UserPromptSubmit and Stop don’t use matchers, so the group needs none. Claude Code’s file watcher picks up the change, and /hooks lists what it loaded. If you also want to hear about permission prompts, add a Notification group with "matcher": "permission_prompt".
Codex
Codex uses the same three-level shape in ~/.codex/hooks.json. Hooks are on by default.
{
"hooks": {
"UserPromptSubmit": [
{ "hooks": [{ "type": "command", "command": "/Users/you/bin/agent-turn codex start", "async": true, "timeout": 5 }] }
],
"Stop": [
{ "hooks": [{ "type": "command", "command": "/Users/you/bin/agent-turn codex stop", "async": true, "timeout": 5 }] }
]
}
}
Then run codex in a terminal, type /hooks, and trust both hooks. Until you do, Codex skips them. Note that Stop in Codex expects JSON on stdout or nothing at all: plain text is invalid for that event.
Cursor
Cursor lists handlers directly under each event, with no matcher group.
{
"version": 1,
"hooks": {
"beforeSubmitPrompt": [
{ "command": "/Users/you/bin/agent-turn cursor start", "timeout": 5 }
],
"stop": [
{ "command": "/Users/you/bin/agent-turn cursor stop", "timeout": 5 }
]
}
}
Cursor reloads hooks.json when you save it. The Hooks output channel shows each run and its errors.
GitHub Copilot CLI
Copilot reads every JSON file in ~/.copilot/hooks/, so give it a file of its own, such as ~/.copilot/hooks/agent-turn.json.
{
"version": 1,
"hooks": {
"userPromptSubmitted": [
{ "type": "command", "bash": "/Users/you/bin/agent-turn copilot start", "timeoutSec": 5 }
],
"agentStop": [
{ "type": "command", "bash": "/Users/you/bin/agent-turn copilot stop", "timeoutSec": 5 }
]
}
}
Codex hooks compared with Claude Code’s
Codex has hooks, and they look very similar. Codex reads ~/.codex/hooks.json, uses the same event names for the main events (SessionStart, UserPromptSubmit, PreToolUse, PermissionRequest, PostToolUse, Stop, SessionEnd) and the same matcher groups. You can often copy a Claude Code entry across and change only the command.
Three differences matter. Codex adds an Interrupt event for a turn you stop by hand, where Claude Code’s Stop simply doesn’t fire. Codex has no Notification event, so PermissionRequest is your only “needs you” signal. And Codex asks you to review and trust every hook that isn’t managed by an administrator. The docs say trust is recorded against the hook’s hash, so a changed hook needs your OK again.
From our own installs we would add two things. Trust can only be granted in the Codex terminal interface: the Codex desktop app lists an untrusted hook but skips it without saying so (openai/codex#47283). And the trust record also depends on where the hook sits in the file, so inserting a new hook above existing ones can cancel their trust (openai/codex#49399). Add new hooks at the end, and edit existing ones in place.
Common problems, and how to avoid them
Most broken hooks fail for one of a few reasons. These are the ones that cost us the most time.
Stdout goes to the model
On UserPromptSubmit, Claude Code adds plain text from stdout to Claude’s context, and Codex adds it as developer context. A debug echo in your hook becomes part of every prompt. A line your shell profile prints at startup can do the same, or break the JSON you meant to return. Write logs to a file or to stderr, and print nothing unless the agent documents an answer. The one we print is the continue: true object that Cursor documents for beforeSubmitPrompt.
Always exit 0, and stay fast
Exit code 2 blocks a prompt in Claude Code, Codex and Cursor. A script that fails with exit 2 by accident stops your own prompt. End every path with exit 0, and avoid set -e.
A normal hook also makes the agent wait. Claude Code and Codex wait for a command hook to finish unless you set async: true, and Claude Code’s default timeout on UserPromptSubmit is 30 seconds. Cursor’s beforeSubmitPrompt runs before the request leaves. Do the quick part in the hook and start anything slow as a separate process.
Cursor runs your Claude Code hooks too
Cursor reads ~/.claude/settings.json and runs the hooks in it, mapping UserPromptSubmit to beforeSubmitPrompt and Stop to stop. The setting is under Cursor Settings › Agents › Third-Party Imports, and it is on by default. If you have hooks in both files, every Cursor turn reports twice, once under Claude Code’s name. Copilot CLI borrows in the same way: it also reads a repository’s .claude/settings.json.
You can tell the real caller apart. Cursor sets CURSOR_VERSION in the environment of every hook and puts cursor_version in the payload. The script above uses that to skip the borrowed copy.
Edit shared files with care
~/.claude/settings.json also holds your permissions, model and other settings, and the Codex and Cursor files may hold other tools’ hooks. Add your entries and leave the rest as it is, and check that the file is still valid JSON afterwards: one stray comma and the agent ignores every hook in it. Copilot is easier, because it reads a folder, so your hook gets a file of its own. In Codex, add new hooks at the end of the file, for the trust reason above, and restart a running session after editing hooks.json (openai/codex#21160).
Hooks run where the agent runs
A hook in your home folder runs only for agents running on your Mac: the CLIs, the editor extensions, and the coding agents inside the desktop apps. Cloud sessions run their hooks on a remote machine that doesn’t have your home folder. Cursor’s docs say its cloud agents never load ~/.cursor/hooks.json, and Copilot’s cloud agent reads only .github/hooks/*.json from the repository. Ordinary chats in a browser have no hooks at all.
Your hook reads your prompts
The prompt-submitted hook receives the full text of your prompt on stdin, and Cursor adds your email address to every payload. Treat that input as private: don’t write it to a log, and don’t send it anywhere you would not paste the prompt yourself.
Spare Minute’s hook uses the prompt only to tell a quick question from a long task. It does that on your computer, keeps no text and never sends your prompts anywhere. Our privacy page has the details.
Questions
Does Codex have hooks like Claude Code?
Yes. Codex reads hooks from ~/.codex/hooks.json or from [hooks] tables in ~/.codex/config.toml, with the same event names and matcher groups as Claude Code for the main events. The big difference is trust: you must review and trust each hook in the Codex terminal with /hooks before it runs.
Why is my Claude Code hook not working?
Run /hooks to see whether Claude Code loaded it. The usual causes are invalid JSON in the settings file (trailing commas and comments aren’t allowed), a script that isn’t executable or a relative path (use chmod +x and an absolute path), and a matcher that doesn’t match: tool matchers are case-sensitive, and Stop and UserPromptSubmit ignore matchers. Text on stdout can break JSON output, a slow hook can hit its timeout, and disableAllHooks or a managed setting can turn hooks off. Start Claude Code with claude --debug to see what each hook did.
Can a hook slow down my agent?
Yes, a synchronous one can. Claude Code and Codex wait for a command hook to finish before they continue, unless it has async: true, and Cursor runs beforeSubmitPrompt before your prompt is sent. Keep the hook short, start slow work in the background, and give it a small timeout.