Blog · Guide · 16 November 2026

Codex Hooks: A Practical Guide to the Lifecycle

Codex hooks let you run your own scripts at fixed points while OpenAI's Codex works: when a session starts, before and after a tool call, when it asks for permission, when a turn ends. They are the way to add logging, checks and outside approval to Codex without changing Codex. This guide covers the events, where hooks are configured, the trust step that catches people out, and how one integration (Forkit's) uses them. Facts about Codex come from OpenAI's hooks documentation.

What Codex hooks are for

OpenAI's docs describe hooks as an extensibility framework that runs scripts or MCP tools during the agentic loop. The examples they give:

  • Send the chat to a custom logging or analytics engine.
  • Scan prompts to block accidentally pasted API keys.
  • Summarize chats to create memories automatically.
  • Run a validation check when a turn stops, to enforce standards.
  • Customize prompting in a certain directory.

In short: anything that must happen every time, regardless of what the model decides.

The lifecycle events

The docs group the events by when they run.

When Events
During a turn PreToolUse, PermissionRequest, PostToolUse, PreCompact, PostCompact, UserPromptSubmit, SubagentStop, Stop
For each model request attempt PreModelRequest, PostModelRequest
When you interrupt an active turn Interrupt (not for subagents)
When a session or subagent starts SessionStart, SubagentStart
When the main thread ends SessionEnd (not for subagents)

If you know Claude Code's hooks, most of these names are familiar. The model is the same: the harness calls your script with a description of the event, and your script may influence what happens next.

Where hooks.json lives

Codex looks for hooks next to its configuration layers, in either of two forms: a hooks.json file, or inline [hooks] tables inside config.toml. The docs name the four most useful locations:

  • ~/.codex/hooks.json
  • ~/.codex/config.toml
  • <repo>/.codex/hooks.json
  • <repo>/.codex/config.toml

Three rules from the docs about how they combine:

  1. Every matching hook runs. If more than one source defines hooks, Codex loads all of them. Higher-precedence layers do not replace lower ones.
  2. Matching command hooks start concurrently. One hook cannot prevent another matching hook from starting.
  3. Prefer one form per layer. If a layer has both hooks.json and inline [hooks], Codex merges them and warns at startup.

Plugins can bundle hooks too, through their manifest or a default hooks/hooks.json.

For the exact schema of a hook entry, use OpenAI's documentation; it is the authority and it changes.

Trusting hooks

This is the step that surprises people: a hook you just configured does not run.

Per the docs, non-managed hooks must be reviewed and trusted before they run. Codex records trust against the hook's current hash, so a new or changed hook is marked for review and skipped until you trust it. Use /hooks in the Codex CLI to inspect hook sources, review new or changed hooks, trust them or disable individual ones.

Project-local hooks have one more condition. They load only when the project's .codex/ layer is trusted. In an untrusted project, Codex still loads user and system hooks from their own layers.

The design is sound. A hook is arbitrary code that runs on your machine with your permissions. A repository you cloned should not be able to run code just because it contains a hooks.json.

The practical consequence: after installing or updating any hook, open Codex and run /hooks once. And in an environment with no interactive terminal, such as a CI job or sandbox, plan for how trust will be established; it cannot be clicked through.

PermissionRequest in Codex

Codex combines a sandbox with an approval policy. OpenAI's security docs say that locally Codex uses an OS-enforced sandbox that typically limits it to the current workspace, with network access off by default, plus an approval policy that controls when it must stop and ask. In the Auto preset, Codex reads, edits and runs commands inside the working directory, and asks before editing outside it or running commands that need network access.

The PermissionRequest hook runs at the point where Codex would ask. That makes it the place to send the question elsewhere: to a policy script, a teammate or a queue.

PreToolUse is the event for rules that should apply whether or not Codex would have asked.

Edits arrive as apply_patch

One difference from Claude Code matters if you write hooks that care about files. Codex makes edits with a tool called apply_patch, whose input is a patch that can touch several files. A hook that wants to know which files changed has to read the paths out of the patch. In Claude Code, by contrast, an edit tool call names one file.

If you are porting a Claude Code hook that checks file paths, this is the part to rewrite.

The older route: notify

Codex's configuration also has a notify setting, a command that Forkit's integration notes use to have each turn's reply reported. It is simpler than hooks and one-directional. It can tell another program what happened. It cannot hold a command or feed anything back.

How Forkit uses Codex hooks

Forkit connects Codex in two ways, and its documentation is careful about the difference.

Through hooks. For Codex 0.133 or later, forkit init writes ~/.codex/hooks.json with entries for SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, PermissionRequest, Stop and SessionEnd. All of them run the same hook script Forkit uses for Claude Code, with a flag saying the source is Codex. It removes Forkit's own notify line if one was there, and tells you to run /hooks in Codex once to trust the hooks.

With hooks in place, a Codex session behaves like a Claude Code session in Forkit: one thread per session, commands and changed files recorded (file paths are read out of each apply_patch), permission requests held for rules or a person, and messages from people or other agents delivered into the session.

Through notify. Without hooks, Forkit can be set as Codex's notify command:

notify = ["forkit", "notify"]

This reports each turn's reply. A session connected only this way cannot receive messages, and Forkit's thread page says so under the reply box. forkit init leaves an existing notify command of yours alone.

An environment variable, FORKIT_CODEX=hooks|notify, overrides the version check.

Two caveats, stated in Forkit's own documentation. The hooks path has not yet been run against a real Codex session. And Codex in a sandbox is not covered, because trusting hooks needs /hooks in a terminal. If you depend on Codex, test before relying on either.

Writing your own hook: advice

  • Exit quickly. Hooks run inside the agent's loop. A slow hook is a slow agent.
  • Fail open, deliberately. Decide what happens if your hook crashes or times out. Forkit's capture hooks give up after a few seconds and always exit 0, so a network problem never blocks the agent.
  • Read identity from your own credentials, not from the event payload.
  • Do not log secrets. Prompts and commands can contain them.
  • Expect concurrency. Other matching hooks start at the same time as yours.
  • Re-trust after every change. The hash changes when the hook does.

Codex hooks or MCP

Hooks and MCP tools do different jobs. A hook always runs; a tool runs when the model chooses. Use hooks for capture and enforcement, and MCP for things the agent decides to do, such as asking a question. Claude Code hooks vs MCP covers the reasoning, which applies to Codex as well. For the Claude Code version of the permission hook, see what the PermissionRequest hook does in Claude Code, and for approvals more broadly, ways to approve agent commands compared.

Where Forkit fits

Forkit uses Codex's hooks to give Codex sessions the same inbox, rules and threads as Claude Code sessions, with the caveats above. npx forkit-cli init writes the hook file. It is free to try. More at forkit-mcp.com.