Hook
An event-driven interception mechanism in Claude Code that allows developers to insert custom inspection logic at specific execution points, such as before tool calls or when a task stops. Rules can be described in natural language, which the model interprets and enforces at runtime.
Timeline (last 90 days)
Hook commands receive a JSON description of the event via standard input, can control flow via exit codes, and exit code 2 means blocking the operation and feeding stderr back to Claude.
Hooks execute in the local shell, have the user's permissions, and do not consume model tokens.
Claude Code's Hook mechanism allows user-defined scripts to run automatically at specific lifecycle points (such as before a tool call, after a tool call, or at session start); configuration goes in the hooks field of settings.json, and scripts go in the agreed directory.
Among the components Harness covers, orchestration logic, hooks, and middleware are the most easily underestimated parts, as they determine when the model is called, what context it receives, and how its output is intercepted and corrected.
For things that absolutely must not happen (such as pushing to the main branch), writing them in Markdown is merely a request; a hook or permission setting should be used to enforce it.
Hooks are scripts automatically executed at specific lifecycle events that can intercept, modify or reject an operation; permission settings declare which commands, paths or tools can run without asking, which must be manually confirmed, and which are completely forbidden; both are enforced by the runtime and do not rely on the model voluntarily complying.
DSupport's guardrails are deterministic code running at three fixed points in the agent loop, exposing three aspects—beforeTool, afterTool, and beforeFinal—through the Hook interface, and each aspect can allow, reject, or modify.
Policy hooks are an extension mechanism provided by coding agents: developers attach custom scripts or functions before and after the agent executes a tool call, to audit, modify, or even veto the operation.
v2.1.295 fixes the issue where a mod's Hook received truncated deeply nested tool input without any error, as well as the vulnerability where a guard Hook with .catch was bypassed during plugin worker restarts.
Claude Code v2.1.295 introduces the onFailure: "block" option for command-type and HTTP-type Hooks, which allows a failed Hook to directly block the corresponding operation.
8 more timeline events
All Facts (20)
Hooks配置在.claude/settings.json中,可提交到代码仓库实现团队共享
90%UnverifiedClaude Code v2.1.295 introduces the onFailure: "block" option for command-type and HTTP-type Hooks, which allows a failed Hook to directly block the corresponding operation.
70%UnverifiedHooks are scripts automatically executed at specific lifecycle events that can intercept, modify or reject an operation; permission settings declare which commands, paths or tools can run without asking, which must be manually confirmed, and which are completely forbidden; both are enforced by the runtime and do not rely on the model voluntarily complying.
60%UnverifiedPolicy hooks are an extension mechanism provided by coding agents: developers attach custom scripts or functions before and after the agent executes a tool call, to audit, modify, or even veto the operation.
60%UnverifiedHook event types include PreToolUse, PostToolUse, and Notification; if a PreToolUse script returns exit code 2, the tool call is blocked.
60%UnverifiedHooks are a deterministic mechanism enforced by the Claude Code software, do not rely on model judgment, and can guarantee 100% execution.
60%UnverifiedClaude Code内置了Hook机制,允许开发者在请求发送到模型之前拦截并处理输入内容
60%UnverifiedHook commands receive a JSON description of the event via standard input, can control flow via exit codes, and exit code 2 means blocking the operation and feeding stderr back to Claude.
50%UnverifiedHooks execute in the local shell, have the user's permissions, and do not consume model tokens.
50%UnverifiedClaude Code's Hook mechanism allows user-defined scripts to run automatically at specific lifecycle points (such as before a tool call, after a tool call, or at session start); configuration goes in the hooks field of settings.json, and scripts go in the agreed directory.
50%UnverifiedAmong the components Harness covers, orchestration logic, hooks, and middleware are the most easily underestimated parts, as they determine when the model is called, what context it receives, and how its output is intercepted and corrected.
50%UnverifiedFor things that absolutely must not happen (such as pushing to the main branch), writing them in Markdown is merely a request; a hook or permission setting should be used to enforce it.
50%UnverifiedDSupport's guardrails are deterministic code running at three fixed points in the agent loop, exposing three aspects—beforeTool, afterTool, and beforeFinal—through the Hook interface, and each aspect can allow, reject, or modify.
50%UnverifiedPreviously, a Hook failure was a soft failure (the operation continued to proceed); onFailure: "block" allows a Hook to declare a hard failure, defaulting to a fail-closed rather than fail-open strategy when infrastructure failures occur.
50%Unverifiedv2.1.295 fixes the issue where a mod's Hook received truncated deeply nested tool input without any error, as well as the vulnerability where a guard Hook with .catch was bypassed during plugin worker restarts.
50%UnverifiedFor hook injection, the recommended first choice is automatic Hook injection; if the IDE does not support hooks, writing to a rules file (such as agents.md) serves as the fallback solution.
50%UnverifiedWithout hook injection, even if the Skill files are in place, the Agent will not actively call them.
50%UnverifiedRoot or hook solutions are the fastest and most stable, but require flashing the device, carry the risk of account bans, and hook implementations often break after WeChat upgrades.
50%UnverifiedFor high-risk operations such as deleting data, charging fees, and mass messaging, lifecycle hooks should be designed to automatically pause and return control to a human when triggered.
50%UnverifiedThe two pathways for data to enter the Ledger are: automatic capture of tool errors via a Hook mechanism, and manual retrospectives initiated by user commands.
50%