← Back to Journal
中文版 →

Making Rules Happen: Claude Code and Codex Hooks

What actually helps is not writing longer rules. It is putting a small rule at the right moment.

Three panels: a notebook workflow, the four parts of a Hook, and a safe implementation scenario
01 Put the rule at the right moment 02 Event, Matcher, Handler, Stop rule 03 Start narrow and verify

Reminders Are Not Always Reliable

An agent may forget to read the project instructions first, or forget to verify its work after editing a file. When it comes to a dangerous Git command, a reminder in the conversation is not always enough. The value of a Hook is that it turns these reminders into working rules with a fixed trigger, a fixed scope, and a fixed response.

Every Hook Answers Four Questions

  1. Event: When does it fire? For example SessionStart, PreToolUse, PostToolUse, or Stop.
  2. Matcher: Which cases should it run for? For example only Bash, or only after Edit/Write.
  3. Handler: What should it check, or what context should it add?
  4. Stop rule: When should it allow, deny, or hand the decision to a person?

These four parts make automation more predictable. The rule runs whenever it is triggered, instead of hoping the model happens to remember. The matcher narrows the scope so that slow checks do not hold up every action. The stop rule keeps the automation from turning into an endless loop.

Start with a Safe, Narrow Scope

I put together 15 practical scenarios, ranging from correcting project scope and privacy reminders at startup, to intercepting dangerous Git commands before a tool runs, to minimal verification after edits and quality checks on public content. The first thing worth trying is usually not complex auto-repair, but two kinds of narrow Hooks:

A Hook is not about letting the agent do more on its own. It is about making it stop more reliably at the points it must not forget, check its work, or hand the decision back to a person.

Claude Code and Codex Settings Are Not Interchangeable

The two share similar concepts, but their configuration and supported capabilities cannot simply be copied from one to the other. When I implement Hooks, I first check each tool's official documentation, then start with a local command handler that makes no external network calls. Existing settings stay in place; a new Hook is first verified with simulated input, then reviewed and trusted by a person.

Starting with one narrow matcher usually shows the value more clearly than building a whole rule set at once.

Claude CodeCodexHooksAgent WorkflowSafety