Codex Rules and Hooks
With Rules and Hooks, you can customize Codex's behavior to better fit your workflow. This section explains both features in detail.
Rules
Rules let you define custom behavior guidelines for Codex that apply to all conversations.
Rule File Locations
Codex loads rules from the following locations:
~/.codex/rules/- User-level rules{项目}/.codex/rules/- Project-level rules
Rule File Format
Create a Rule File
My Coding Rules
Code Style
Use 4-space indentation
Maximum 100 characters per line
Always use const/let, never var
Comment conventions
Public functions must include JSDoc comments
Add inline comments for complex logic
Remove all debugging console.log statements
Testing requirements
All new features must include test cases
Test file naming: *.test.js or *.spec.js
Git commits
Commit messages use the Conventional Commits format
Run lint and tests before committing
Enable Rules
Enable rules in the configuration
[features]
codex_hooks = true
Rules are injected into the system prompt of every conversation, ensuring Codex always follows your coding standards.
Hooks
Hooks let you perform custom actions when specific events occur.
Hook File Locations
Hooks are in ~/.codex/hooks.json Configure in :
Create a Hook Configuration
{
"hooks": [
{
"event": "on_tool_call",
"match": "shell",
"action": "allow"
},
{
"event": "on_task_complete",
"action": "notify",
"command": "echo 'Task completed'"
}
]
}
Available Events
| Events | Description |
|---|---|
on_tool_call |
Triggered before a tool call |
on_task_start |
Triggered when a task starts |
on_task_complete |
Triggered when a task completes |
on_error |
Triggered when an error occurs |
on_message |
Triggered when a message is received |
Hook Actions
| Operation | Description |
|---|---|
allow |
Allow the operation to continue |
deny |
Block operation |
notify |
Send notification |
log |
Record logs |
custom |
Execute a custom script |
Enabling hooks requires setting
[features].codex_hooks = true
Practical Examples
Example 1: Automatically Running Tests
Automatically run tests after every file change by Codex:
Automatic Test Hook
{
"hooks": [
{
"event": "on_tool_call",
"match": "edit_file",
"action": "custom",
"command": "npm test",
"timeout": 60
}
]
}
Example 2: Enforcing Code Review
Enforce code review before committing:
Code Review Hook
{
"hooks": [
{
"event": "on_tool_call",
"match": "git_commit",
"action": "custom",
"command": "/review",
"require_approval": true
}
]
}
Example 3: Logging
Log all command executions:
Logging Hook
{
"hooks": [
{
"event": "on_tool_call",
"match": "shell",
"action": "log",
"log_file": "/tmp/codex-commands.log"
}
]
}
Rules vs. Hooks
| Feature | Rule | Hook |
|---|---|---|
| When They Take Effect | At the start of a conversation | When an event is triggered |
| Main Use | Define coding standards | Automate actions |
| Configuration Location | rules/ directory | hooks.json |
Rules and hooks can be used together: rules define 'how to do it,' hooks define 'when to do it.'
Best Practices
Rule Best Practices
- Keep rules concise and clear
- Avoid overly strict restrictions
- Adjust rules to project needs
Hook Best Practices
- Avoid long-running hooks
- Set reasonable timeouts
- Test hook behavior
Overusing hooks may affect Codex's performance; only add the hooks you truly need.
Common questions
Q: Rules not taking effect?
Make sure the rule file is in the correct location and that the codex_hooks attributes.
Q: Does a failed hook block the operation?
It depends on the hook configuration; some hook failures block subsequent operations.
Q: Can rules be enabled/disabled dynamically?
Yes, via configuration files or environment variables.