Codex Rules and Hooks |
\\n\\nThrough Rules and Hooks, you can customize Codexβs behavior to better fit your workflow. This section details both features.
\\n\\n\\n\\n
Rules
\\n\\nRules allow you to define custom behavioral guidelines for Codex, applied to all conversations.
\\n\\nRule File Locations
\\n\\nCodex loads rules from the following locations:
\\n\\n- \\n
~/.codex/rules/β User-level rules \\n {project}/.codex/rules/β Project-level rules \\n
Rule File Format
\\n\\n## Create rule file\\n\\n# My coding rules\\n\\n## Code style\\n\\n - Use 4-space indentation\\n\\n - Maximum 100 characters per line\\n\\n - Always use const/let, never use var\\n\\n## Commenting conventions\\n\\n - Public functions must include JSDoc comments\\n\\n - Add inline comments for complex logic\\n\\n - Remove all debug console statements.log\\n\\n## Test requirements\\n\\n - All new features must include test cases\\n\\n - Test file naming:*.test.js or *.spec.js\\n\\n## Git submit\\n\\n - submitCommit messages follow Conventional Commits format\\n\\n - submitRun lint and tests before commit\\n\\nEnabling Rules
\\n\\n## Enable rules in configuration\\n\\n\\n\\n codex_hooks = true\\n\\n\\n\\n\\nRules are injected into the system prompt of each conversation, ensuring Codex always adheres to your coding standards.
\\n
\\n\\n
Hooks
\\n\\nHooks allow you to execute custom actions when specific events occur.
\\n\\nHook File Location
\\n\\nHooks are configured in ~/.codex/hooks.json:
## Create hook configuration\\n\\n{\\n\\n "hooks": [\\n\\n {\\n\\n "event": "on_tool_call",\\n\\n "match": "shell",\\n\\n "action": "allow"\\n\\n },\\n\\n {\\n\\n "event": "on_task_complete",\\n\\n "action": "notify",\\n\\n "command": "echo 'Task completed'"\\n\\n }\\n\\n ]\\n\\n }\\n\\nAvailable Events
\\n\\n| Event | \\nDescription | \\n
|---|---|
on_tool_call | \\n Triggered before a tool call | \\n
on_task_start | \\n Triggered when a task starts | \\n
on_task_complete | \\n Triggered when a task completes | \\n
on_error | \\n Triggered when an error occurs | \\n
on_message | \\n Triggered when a message is received | \\n
Hook Actions
\\n\\n| Action | \\nDescription | \\n
|---|---|
allow | \\n Allow the operation to proceed | \\n
deny | \\n Block the operation | \\n
notify | \\n Send a notification | \\n
log | \\n Log the event | \\n
custom | \\n Execute a custom script | \\n
\\n\\n\\nEnabling hooks requires setting
\\n.codex_hooks = true
\\n\\n
Practical Examples
\\n\\nExample 1: Auto-run Tests
\\n\\nAutomatically run tests after Codex modifies a file:
\\n\\n## Automated test hook\\n\\n{\\n\\n "hooks": [\\n\\n {\\n\\n "event": "on_tool_call",\\n\\n "match": "edit_file",\\n\\n "action": "custom",\\n\\n "command": "npm test",\\n\\n "timeout": 60\\n\\n }\\n\\n ]\\n\\n }\\n\\nExample 2: Enforce Code Review
\\n\\nEnforce code review before committing:
\\n\\n## Code review hook\\n\\n{\\n\\n "hooks": [\\n\\n {\\n\\n "event": "on_tool_call",\\n\\n "match": "git_commit",\\n\\n "action": "custom",\\n\\n "command": "/review",\\n\\n "require_approval": true\\n\\n }\\n\\n ]\\n\\n }\\n\\nExample 3: Log Commands
\\n\\nLog all command executions:
\\n\\n## Logging hook\\n\\n{\\n\\n "hooks": [\\n\\n {\\n\\n "event": "on_tool_call",\\n\\n "match": "shell",\\n\\n "action": "log",\\n\\n "log_file": "/tmp/codex-commands.log"\\n\\n }\\n\\n ]\\n\\n }\\n\\n\\n\\n
Difference Between Rules and Hooks
\\n\\n| Feature | \\nRules | \\nHooks | \\n
|---|---|---|
| Timing | \\nApplied at conversation start | \\nTriggered on specific events | \\n
| Primary Use | \\nDefine coding standards | \\nAutomate actions | \\n
| Configuration Location | \\nrules/ directory | \\n hooks.json | \\n
\\n\\n\\nRules and hooks can be used together: Rules define βhowβ, hooks define βwhenβ.
\\n
\\n\\n
Best Practices
\\n\\nRule Best Practices
\\n\\n- \\n
- Keep rules concise and clear \\n
- Avoid overly restrictive constraints \\n
- Adjust rules based on project needs \\n
Hook Best Practices
\\n\\n- \\n
- Avoid long-running hooks \\n
- Set reasonable timeouts \\n
- Test hook behavior \\n
\\n\\n\\nOverusing hooks may impact Codex performanceβonly add hooks that are truly necessary.
\\n
\\n\\n
Frequently Asked Questions
\\n\\nQ: Rules are not taking effect?
\\n\\nEnsure the rule file is in the correct location and codex_hooks is enabled.
Q: If a hook fails, does it block the operation?
\\n\\nIt depends on the hook configurationβsome failures may block subsequent operations.
\\n\\nQ: Can rules be enabled/disabled dynamically?
\\n\\nYesβrules can be controlled via configuration files or environment variables.
YouTip