Learning resourcesReviewed · OpenAI

Codex Hooks documentation

The Codex Hooks guide teaches developers and administrators to automate checks around an AI coding assistant, but some hook errors allow actions to proceed. It explains how to configure command scripts and connected MCP tools, choose lifecycle events, review hook trust and apply managed settings.

Researched 14 Nov 2026

In short.

The Codex Hooks guide shows developers and administrators how to run their own scripts or connected MCP tools at set points while Codex, an AI coding agent, works, but hooks cannot guarantee that every unwanted action is blocked. Its detailed reference covers configuration, matching, trust, event inputs and outputs, background execution and managed environments. Timing matters: PreToolUse can deny or rewrite supported calls, while PostToolUse runs after an action and background hooks cannot control it. No hooks were executed; practical reliability, performance and security outcomes remain unassessed.

Researched and written by an AI model and checked by a second AI model. How it works

Coverage is limited to the complete documentation page at [https://learn.chatgpt.com/docs/hooks.md], including its configuration examples, lifecycle contracts, trust controls, managed and cloud behavior, limitations and schema-version caveat. Linked documentation, the documentation index and GitHub schemas are excluded. Scripts, MCP services, installed-release compatibility, runtime performance and enforcement outcomes were not executed or assessed.

The overview

Identity

This is the Hooks documentation for Codex, an AI coding agent, at [https://learn.chatgpt.com/docs/hooks.md]. It describes an extensibility framework that runs command scripts or tools from connected MCP servers at particular points in an agent conversation.

The resource is a configuration and behavior reference. It includes examples and event contracts, but does not provide a complete logging service, secret scanner or validation system.

Assessment

The guide is useful for implementing custom automation because it explains both configuration and consequences: which events honor matchers, how trust is recorded, what outputs are supported and when failures permit an operation to continue.

Its main practical limitation is enforcement coverage. Hosted tools do not use the documented local tool-hook path, specialized paths can opt out, and several error conditions allow work to proceed. A configured hook therefore does not establish comprehensive protection.

Users

The guide suits developers maintaining repository checks, teams connecting existing scanners or logging services, and administrators managing hooks across Codex environments. Beginners can use it to understand event timing, but implementing custom behavior requires scripting or knowledge of the connected tool's interface.

Administrators also get guidance on managed hooks and cloud orchestration. Those environments have different supported handlers and sources, so a local configuration cannot be assumed to work unchanged in a cloud-managed workspace.

Capabilities

Documented uses include sending conversations to analytics, checking prompts for secrets, adding session context, preserving memories and validating work when a turn stops. These are integration possibilities; their quality depends on the scripts or services supplied.

PreToolUse can deny or rewrite supported tool calls. PermissionRequest can decide approval requests, with a denial taking precedence when multiple hooks decide. UserPromptSubmit can reject a prompt, and Stop or SubagentStop can request continued work.

PreModelRequest and PostModelRequest provide diagnostics for individual model-request attempts, including retries. Their output cannot change model context or block a request.

Using it

Requirements

Command hooks need the referenced scripts and their runtimes to be available. Commands run from the session's working directory. Python and Git appear in examples, but the page does not establish them as requirements for every hook.

MCP, or Model Context Protocol, hooks require an already-connected server exposing the named tool. Hooks do not start or reconnect servers. Non-managed hooks normally require review and trust, and repository hooks also require a trusted project configuration layer. For one-off automation that already vets hook sources outside Codex, --dangerously-bypass-hook-trust runs enabled hooks without persisted hook trust for a single invocation.

No minimum Codex version, complete operating-system compatibility matrix or account entitlement is established. Managed script deployment is the administrator's responsibility; Codex does not distribute scripts from the managed directory.

Mechanism

Configuration has three levels: an event, a matcher group and one or more handlers. Matchers use regular expressions, but some events ignore them. Matching hooks from multiple sources accumulate rather than replacing lower-precedence hooks, and matching command hooks for the same event start concurrently. Setting allow_managed_hooks_only = true in requirements.toml skips user, project, session and plugin hooks while retaining managed hooks. Hooks are enabled by default; setting hooks = false under [features] in config.toml turns them off. Administrators can force hooks off with the same setting in requirements.toml.

A command handler receives event JSON on standard input and returns event-specific feedback through output or exit status. Supported fields differ by event; using an unsupported control field can cause failure and allow the underlying call to continue.

MCP handlers send structured arguments directly to an existing tool. Templates such as ${tool_input.file_path} extract event fields; a placeholder occupying an entire value preserves its JSON type. MCP hooks run synchronously and do not trigger other hooks or request tool approval.

Setup

Configure hooks in hooks.json or inline hooks tables in config.toml. The page identifies ~/.codex/hooks.json, ~/.codex/config.toml, <repo>/.codex/hooks.json and <repo>/.codex/config.toml as the four most useful locations. Codex also discovers hooks beside other active config layers and can load hooks from enabled plugins and managed sources. Prefer one representation per layer: using both merges their contents and produces a startup warning.

Choose the event and matcher, supply the command or MCP tool arguments, and set an appropriate timeout. For repository scripts, the guide recommends resolving paths from the Git root because Codex may start in a subdirectory.

Use /hooks in the CLI to inspect and trust non-managed definitions. Trust follows the current definition hash, so changes normally require review. Plugin script updates can also trigger renewed review. For one-off automation that already vets hook sources outside Codex, --dangerously-bypass-hook-trust runs enabled hooks without persisted hook trust for a single invocation. Managed hooks are trusted by policy and cannot be disabled through the user hook browser.

Example

The page's patch-scanning example attaches an MCP handler to PostToolUse, matches Write|Edit, and calls scan_patch on an already-connected scanner server. It passes the event's tool_input.command field as the patch argument and sets a 30-second timeout.

This illustrates passing tool-event data to an existing service. It does not supply the scanner implementation or establish detection accuracy. Because PostToolUse runs after editing, the scan cannot prevent the original file change. The example was not executed.

Assessment & evidence

Limitations

MCP hook errors, missing servers and unavailable tools do not block the operation. The cloud section also warns that PreToolUse errors, timeouts or malformed responses can let a tool run. PostToolUse cannot undo completed side effects.

Background command hooks cannot block, approve or rewrite actions. They are limited to eight concurrent executions per session; excess invocations wait. Unfinished background hooks are cancelled when the session ends, and undelivered output is discarded.

Command and MCP handlers are supported, while prompt and agent handlers are parsed but skipped. SessionEnd does not support MCP handlers. Cloud orchestration supports only Global MCP hooks and excludes local configuration, plugin hooks and command handlers.

By default, model-visible hook-output messages are limited to roughly 2,500 tokens. Oversized output may be saved under <temp_dir>/hook_outputs/<session_id>/<uuid>.txt, with a shortened preview sent to the model. For command handlers, additionalContextLimit changes the threshold for additionalContext; 0 passes the complete additional context directly to the model. Multiple hooks can add enough context to degrade model performance, but the performance drop is unquantified. Sensitive data can end up on disk when oversized output is saved, so the page advises excluding secrets and other sensitive data from hook output.

Reasoning

Event timing should determine the design. A synchronous PreToolUse hook can act before a supported operation; PostToolUse can inspect the result after its effects; an asynchronous command hook suits informational work that need not control the action.

The documented concurrency and failure behavior mean a single denying hook cannot be assumed to prevent other matching handlers from starting. Keep logging, validation and action control distinct, and assess each handler's actual event contract.

Alternatives

Within the documented framework, command handlers provide local script execution, while MCP handlers reuse connected tool services. Background command execution is another option when delayed informational output is acceptable. hooks.json and inline TOML offer equivalent configuration representations.

The page does not compare external automation frameworks or competing coding assistants. No evidence supports a recommendation about their relative capability, cost or reliability.

Evidence

The source is the complete captured Hooks page at [https://learn.chatgpt.com/docs/hooks.md]. It contains configuration examples, event tables, input and output definitions, trust behavior and explicit limitations.

The captured source revision is sha256:a50c60b697d8df62e2f907adbb18b2f5a1a25cd3360e7b843cde25f18ab6d782. The documentation supports descriptions of intended behavior, but supplies no measured enforcement results, scanner accuracy or execution benchmarks. Nothing was executed.

Access & freshness

Costs

The page gives no prices, plan requirements, usage allowances or per-hook charges. Costs for Codex access, connected services and script infrastructure are unknown from this evidence.

Synchronous hooks make Codex wait for completion, so they can add delay. Most hooks default to a 600-second timeout, but that is a timeout setting rather than a measured runtime. No latency or resource-use benchmarks are provided.

Rights

The supplied page does not state a licence or redistribution terms for its text and examples. Permission to redistribute those materials is therefore unknown from this evidence.

The page also does not establish rights for any external scripts, plugins or MCP services that a reader might connect.

Freshness

The page was captured on 14 November 2026 at 19:39:30.892 UTC. Its publication date, last-update date and corresponding Codex release number are not established.

The page identifies itself as the release-behavior reference and warns that linked main-branch schemas may contain fields absent from the current release. Compatibility with a particular installed version remains unassessed.

Actions

Start with one bounded use case and choose an event that occurs at the right time. Inspect all active hook sources, confirm the script or connected tool exists, and review the exact non-managed definition before trusting it.

Before relying on a blocking check, verify its behavior with allowed input, explicit denial, malformed output, timeout and service failure in the intended environment. Confirm coverage of the actual tool path rather than assuming all actions pass through it.

Keep returned context concise and exclude secrets from hook output. Confirm installed-release compatibility, account access requirements and any service costs before adopting a wider configuration.

Evidence for this report

selected-page.txtselected · checked 2026-11-14