The Codex Stop hook runs a command every time a turn ends and Codex hands control back to you, which makes it the right place for a "done" notification, a log entry or a final check. It receives Codex's last message in last_assistant_message. It must print either nothing or a JSON object, because Codex treats plain text from a Stop hook as invalid. To make Codex carry on instead of stopping, the hook returns {"decision": "block", "reason": "..."}, and Codex uses the reason as its next prompt.
01A minimal Stop hook
Add this to ~/.codex/hooks.json (macOS shown; swap the command for notify-send on Linux or PowerShell on Windows):
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Codex has finished\" with title \"Codex\"' >/dev/null",
"timeout": 30
}
]
}
]
}
}
Stop ignores matcher, so there is nothing to filter on. Start codex, open /hooks and trust the new hook, or Codex will skip it. Ready-made commands for every system are in how to get a notification when Codex finishes.
02What the hook receives
Codex sends one JSON object on stdin:
{
"session_id": "…",
"turn_id": "…",
"cwd": "/Users/you/projects/billing-svc",
"hook_event_name": "Stop",
"model": "…",
"transcript_path": "…",
"permission_mode": "default",
"stop_hook_active": false,
"last_assistant_message": "Moved invoice fetching to paged queries. Tests pass."
}
last_assistant_messageis Codex's final text for the turn, handy for the body of a notification. It can benull.cwdtells you which project finished, which matters when several sessions run at once.stop_hook_activeistruewhen this turn was itself started by aStophook continuing Codex. Check it to avoid loops.transcript_pathpoints to the session transcript, but OpenAI says its format is not a stable interface, so don't build on it.
03A script with the project name and message
A small script makes better notifications than a one-line command. This one reads stdin, picks out the folder name and the start of the message, and prints nothing:
#!/bin/sh
# ~/.codex/hooks/stop-notify.sh
input=$(cat)
project=$(printf '%s' "$input" | python3 -c 'import json,os,sys; print(os.path.basename(json.load(sys.stdin).get("cwd","")))')
message=$(printf '%s' "$input" | python3 -c 'import json,sys; m=json.load(sys.stdin).get("last_assistant_message") or "Finished"; print(m.replace("\n"," ")[:150])')
osascript -e "display notification \"$message\" with title \"Codex: $project\"" >/dev/null 2>&1
exit 0
Make it executable with chmod +x ~/.codex/hooks/stop-notify.sh and set "command": "~/.codex/hooks/stop-notify.sh". On Linux, replace the osascript line with notify-send "Codex: $project" "$message". If Codex's message contains quotes, the osascript line can fail quietly; a Python version of the whole script avoids that.
04Making Codex keep going
A Stop hook can refuse to let the turn end. Return this, with exit code 0:
{ "decision": "block", "reason": "Run the test suite once more and fix anything that fails." }
Codex does not treat this as an error. It starts a continuation with your reason as if you had typed it as a new prompt. Exit code 2 with the reason on stderr does the same.
Use it with care:
- Check
stop_hook_active. If it istrue, Codex is already running because of aStophook. A hook that always blocks can keep Codex busy indefinitely. - Make the condition concrete. "Tests are failing" (checked by running them) is a good reason to continue. "Keep improving" is not.
continue: falsewins. If any matchingStophook returns{"continue": false}, that takes precedence over continuation decisions from the others.
This is also how replies from a phone work. Notifier for Codex's Stop hook waits briefly after a turn ends (two minutes by default, set with stop_hold_seconds) and, if you type a follow-up in the iPhone or Android app, returns it as the reason, so Codex picks it up as your next instruction. If you send nothing, the hook returns nothing and Codex simply waits for you in the terminal. It only waits when you are away from the keyboard.
05Common mistakes
- Printing text.
echo "done"in aStophook is invalid output. Redirect everything to/dev/null, or print{}. - Forgetting trust. Every edit to the hook needs a new trust in
/hooks. - Slow commands. Codex waits for a synchronous hook. A
curlto a slow server delays the end of the turn. Set a shorttimeout, or add"async": trueso the hook runs in the background (a background hook can't block or continue Codex). - Assuming one alert per task.
Stopfires once per turn, not once per job. In a long conversation you get one per reply.
06Stop or notify?
Codex's notify setting also fires when a turn completes and is simpler: no trust step and no output rules. It cannot continue the turn, though. The notify guide compares the two. For "needs approval", neither is enough; use a PermissionRequest hook, covered in the PermissionRequest guide.
07FAQ
Does the Stop hook fire when I interrupt Codex?
Interrupting has its own event, Interrupt, which runs when you interrupt an active turn. If you want to know about interruptions, hook that event; it has a 1-second default timeout and a 3-second maximum.
Why does Codex report my Stop hook output as invalid?
Your command printed plain text. Stop accepts only JSON (or nothing) on stdout when it exits 0. Send other output to /dev/null or stderr.
Does a Stop hook fire for subagents?
Subagents have their own event, SubagentStop. Stop is for the main turn.
Can the Stop hook see what Codex changed?
Not directly. It gets the last message and the working folder. Your script can run git diff --stat in cwd to see what changed.
Does it run in codex exec?
Yes. Hooks run for non-interactive codex exec runs too, so a Stop hook is a simple way to be told a scripted run has finished.