Hooks let Codex run reviewed commands at lifecycle points such as session start, prompt submission, tool use, and turn stop. Use them for deterministic guardrails, logging, context injection, and lightweight checks.
| Back to Codex CLI Operator Handbook |
Hooks are an active Codex feature. Keep the exact event list and output schema refreshed from official docs before release.
Use the current feature key:
[features] hooks = true
| Location | Scope |
|---|---|
.codex/hooks.json |
Project-shared hooks |
~/.codex/hooks.json |
Personal hooks |
Project hooks and user hooks can both run. Keep shared hooks small, reviewed, and deterministic.
| Event | Use |
|---|---|
SessionStart |
Add startup context or run a light initialization check |
UserPromptSubmit |
Validate or enrich a submitted prompt |
PreToolUse |
Inspect a pending tool call before execution |
PostToolUse |
Inspect a completed tool call result |
Stop |
Add final checks or request continuation |
Use commandWindows when the POSIX command is not portable:
{
"type": "command",
"command": "python3 .codex/hooks/scripts/hooks.py --hook SessionStart",
"commandWindows": "python .codex/hooks/scripts/hooks.py --hook SessionStart",
"timeout": 10
}- Treat hooks as guardrails, not a complete security boundary.
- Keep hook timeouts short.
- Parse JSON from stdin defensively.
- Keep local overrides in
.codex/hooks/config/*.local.json. - Ignore generated hook logs.
- Do not put tokens or auth material in hook config.
| Anti-pattern | Fix |
|---|---|
| Relying on hooks as the only protection | Combine sandbox, approvals, review, and secret scanning |
| Committing local hook overrides | Use ignored *.local.json files |
| Writing long-running hook commands | Keep hooks fast and bounded |
| Logging full sensitive payloads | Redact or disable logging |
| Using only POSIX commands | Add commandWindows when sharing with Windows users |