The Codex PermissionRequest hook runs at the moment Codex is about to ask you for approval, for example to run a command outside the sandbox, use the network or apply a patch, and it can answer for you. To approve, the hook exits 0 and prints {"hookSpecificOutput": {"hookEventName": "PermissionRequest", "decision": {"behavior": "allow"}}}; to refuse, it uses "behavior": "deny" with a message that Codex passes back to the model. If several hooks answer, any deny wins. If the hook prints no decision, the normal approval prompt appears.
01When it fires, and when it doesn't
PermissionRequest only runs when Codex would otherwise show you an approval prompt. In the default Auto preset, that means things like:
- a shell command that needs to leave the sandbox, or needs network access;
- a patch that edits files outside the workspace;
- an MCP or app tool call that requires approval.
Commands the sandbox already allows run without approval, so the hook never sees them. If you want to inspect every command, use PreToolUse instead. And if your approval policy is never, there are no prompts and this hook never fires. The approval modes guide explains which settings produce prompts.
02What the hook receives
{
"session_id": "…",
"turn_id": "…",
"cwd": "/Users/you/projects/billing-svc",
"hook_event_name": "PermissionRequest",
"model": "…",
"permission_mode": "default",
"tool_name": "Bash",
"tool_input": {
"command": "pnpm install",
"description": "Install dependencies (needs network access)"
}
}
tool_nameisBashfor shell commands,apply_patchfor file edits, or an MCP name such asmcp__github__create_issue.tool_input.commandholds the command or the patch text forBashandapply_patch. MCP tools send their arguments.tool_input.descriptionis a human-readable reason when Codex has one. Don't rely on it being there.
The matcher filters on the tool name. Bash, apply_patch (which Edit and Write also match) and MCP names like mcp__github__.* all work.
03Allowing and denying
Allow:
{
"hookSpecificOutput": {
"hookEventName": "PermissionRequest",
"decision": { "behavior": "allow" }
}
}
Deny, with a message for Codex:
{
"hookSpecificOutput": {
"hookEventName": "PermissionRequest",
"decision": { "behavior": "deny", "message": "Not on the production database. Use staging." }
}
}
The rules:
- Any deny wins. If one matching hook denies and another allows, the request is denied.
- An allow skips the prompt. You won't see it in the terminal.
- No decision means no change. Exit
0with no output and Codex shows the normal prompt. Plain text on stdout is ignored. - Reserved fields fail closed. Don't return
updatedInput,updatedPermissionsorinterrupthere; Codex reserves them for later.
04A small policy script
This Python hook approves git status and git diff, denies anything that mentions prod, and leaves everything else to you:
#!/usr/bin/env python3
import json, sys
event = json.load(sys.stdin)
command = (event.get("tool_input") or {}).get("command") or ""
def decide(behavior, message=None):
decision = {"behavior": behavior}
if message:
decision["message"] = message
print(json.dumps({"hookSpecificOutput": {
"hookEventName": "PermissionRequest", "decision": decision}}))
if command.startswith(("git status", "git diff")):
decide("allow")
elif "prod" in command:
decide("deny", "Production is off limits from Codex. Ask me first.")
# otherwise print nothing: the normal prompt appears
Register it with "matcher": "Bash" in ~/.codex/hooks.json, then trust it in /hooks.
05Often the better tool: sandbox and rules
Before you write a hook that approves things, check whether a setting does it more safely:
- If Codex keeps asking for network access,
[sandbox_workspace_write] network_access = trueinconfig.tomlallows it inside the sandbox. - If you want fewer prompts in general, the approval policy and sandbox mode decide that. See the approval modes guide.
- Codex's own rules control which commands may run outside the sandbox, and
approvals_reviewer = "auto_review"hands eligible approvals to a reviewer agent.
A hook is best when the decision needs something only you know, such as a project-specific policy, or a person who isn't at the keyboard.
06Approving from somewhere else
Because the hook can wait before answering, it can send the request somewhere and return whatever comes back. That is how remote approval works: the hook posts the command to your phone, waits for your tap and prints allow or deny. The default hook timeout is 600 seconds, and you can raise it with timeout.
Notifier for Codex does this with its free codex-notifier tool on Mac, Windows and Linux and an iPhone or Android app. It only holds the request when you are away from the keyboard; if you typed in the last 30 seconds, the terminal prompt appears at once. If no answer arrives within the limit you choose (10 minutes by default), the hook returns nothing and Codex shows its normal prompt, so a timeout never approves anything. Rejecting with a note sends the note to Codex as the reason. See approving Codex commands from your phone.
Notifier for Codex is independent and not affiliated with OpenAI.
07FAQ
Can a PermissionRequest hook approve something that my settings forbid?
It answers the approval prompt, nothing more. It can't widen the sandbox for commands that never reach a prompt, and an administrator's managed policy still applies. If another hook denies, the deny wins.
Can I return "allow for this session"?
No. The hook's decision covers this one request. The next request runs the hook again. To stop being asked about a kind of command, change your sandbox, rules or approval policy instead.
Does exit code 2 deny the request?
Yes, if it writes a reason to stderr: Codex treats that as a deny with that reason. OpenAI's docs only describe the JSON deny decision for PermissionRequest, so that is the safer choice.
What happens if two hooks take a long time?
Matching hooks run at the same time and Codex waits for them. A hook that waits for a person can hold the request up to its timeout, so keep other PermissionRequest hooks fast.
Does it fire for file edits?
Yes, when a patch needs approval, with tool_name set to apply_patch. Edits inside the workspace in the default Auto preset don't need approval, so they don't fire it.