Claude Code中生命周期Hooks的使用指南介紹
概述
Hooks 是用戶自定義的腳本/命令,在 Claude Code 的特定生命周期節(jié)點(diǎn)自動執(zhí)行。支持 4 種類型、4 個生命周期事件。
Hook 的 4 種類型
| 類型 | type 字段 | 說明 |
|---|---|---|
| Shell 命令 | "command" | 執(zhí)行 shell 命令,stdout 作為輸出 |
| HTTP 請求 | "http" | 向指定 URL 發(fā)送 POST 請求(含 hook input JSON) |
| LLM 提示 | "prompt" | 用 LLM 評估 prompt,返回結(jié)構(gòu)化結(jié)果 |
| Agent 驗(yàn)證 | "agent" | 用 agent 執(zhí)行驗(yàn)證任務(wù)(如檢查測試是否通過) |
4 個生命周期事件
Setup
在 Claude Code 啟動時運(yùn)行一次,用于項(xiàng)目初始化。
{
"hooks": {
"Setup": [
{
"matcher": "",
"hooks": [
{ "type": "command", "command": "npm install", "timeout": 60 }
]
}
]
}
}觸發(fā)時機(jī): 進(jìn)程啟動后、REPL 渲染前 Hook Input 字段:
| 字段 | 類型 | 說明 |
|---|---|---|
hook_event_name | "Setup" | — |
trigger | "init" | "maintenance" | 觸發(fā)來源 |
session_id | string | 當(dāng)前會話 ID |
cwd | string | 工作目錄 |
transcript_path | string | 會話日志路徑 |
stdout 處理: ? 不注入模型
SessionStart
每次會話開始時執(zhí)行。新啟動、/resume、/clear 后都會觸發(fā)。stdout 內(nèi)容作為 system message 注入模型上下文。
{
"hooks": {
"SessionStart": [
{
"matcher": "",
"hooks": [
{ "type": "command", "command": "cat .claude/session-context.md", "timeout": 10 }
]
}
]
}
}
觸發(fā)時機(jī):
| source | 場景 |
|---|---|
startup | 進(jìn)程首次啟動 |
resume | --resume 或 /resume 恢復(fù)會話 |
clear | /clear 后 |
compact | 上下文壓縮后 |
Hook Input 字段:
| 字段 | 類型 | 說明 |
|---|---|---|
hook_event_name | "SessionStart" | — |
source | "startup" | "resume" | "clear" | "compact" | 觸發(fā)來源 |
agent_type | string (optional) | agent 類型 |
model | string (optional) | 當(dāng)前模型 |
stdout 處理: ? 注入為 system message,模型可見
UserPromptSubmit
用戶每次提交消息前執(zhí)行??梢詫τ脩糨斎胱鲱A(yù)處理、追加安全檢查或上下文。
{
"hooks": {
"UserPromptSubmit": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "node scripts/validate-input.js",
"timeout": 10,
"statusMessage": "Validating input..."
}
]
}
]
}
}Hook Input 字段:
| 字段 | 類型 | 說明 |
|---|---|---|
hook_event_name | "UserPromptSubmit" | — |
prompt | string | 用戶輸入的原始文本 |
stdout 處理: ? 注入為 system message,模型可見
SessionEnd
會話結(jié)束時執(zhí)行(進(jìn)程退出、/clear)。用于清理資源、記錄日志。
{
"hooks": {
"SessionEnd": [
{
"matcher": "",
"hooks": [
{ "type": "command", "command": "node scripts/cleanup.js", "timeout": 5 }
]
}
]
}
}Hook Input 字段:
| 字段 | 類型 | 說明 |
|---|---|---|
hook_event_name | "SessionEnd" | — |
reason | "clear" | "resume" | "logout" | "prompt_input_exit" | "other" | 結(jié)束原因 |
stdout 處理: ? 不注入模型,靜默執(zhí)行
超時控制: 默認(rèn) 1.5s,可通過 CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS 環(huán)境變量配置
Hook 命令通用字段
所有類型的 hook 都支持以下字段:
| 字段 | 類型 | 必填 | 說明 |
|---|---|---|---|
type | "command" | "http" | "prompt" | "agent" | ? | Hook 類型 |
command / prompt / url | string | ? | 具體命令/prompt/URL(依 type 而異) |
timeout | number | ? | 超時時間(秒) |
statusMessage | string | ? | 執(zhí)行時 spinner 顯示的自定義消息 |
once | boolean | ? | 僅執(zhí)行一次后自動移除 |
if | string | ? | 條件過濾(權(quán)限規(guī)則語法,如 "Bash(git *)") |
async | boolean | ? | 后臺異步執(zhí)行,不阻塞主流程(僅 command 類型) |
asyncRewake | boolean | ? | 后臺執(zhí)行且 exit code 2 時喚醒模型(僅 command 類型,隱含 async) |
HTTP 類型特有字段
| 字段 | 類型 | 說明 |
|---|---|---|
url | string | POST 請求目標(biāo) URL(hook input 以 JSON body 發(fā)送) |
headers | object | 自定義請求頭,支持 $VAR 環(huán)境變量引用 |
allowedEnvVars | string[] | 允許在 header 值中插值的環(huán)境變量白名單 |
Prompt 類型特有字段
| 字段 | 類型 | 說明 |
|---|---|---|
prompt | string | LLM 評估 prompt,用 $ARGUMENTS 占位符引用 hook input JSON |
model | string | 使用的模型(默認(rèn)小模型) |
配置位置
hooks 配置在 settings.json 中,支持兩級:
用戶級(全局生效)
~/.claude/settings.json:
{
"hooks": {
"SessionStart": [
{
"matcher": "",
"hooks": [
{ "type": "command", "command": "echo 'Hello from global hook'", "timeout": 5 }
]
}
]
}
}項(xiàng)目級(僅當(dāng)前項(xiàng)目)
.claude/settings.json(放在項(xiàng)目根目錄):
{
"hooks": {
"Setup": [
{
"matcher": "",
"hooks": [
{ "type": "command", "command": "npm install", "timeout": 120 }
]
}
],
"SessionStart": [
{
"matcher": "",
"hooks": [
{ "type": "command", "command": "cat .claude/instructions.md", "timeout": 5 }
]
}
],
"UserPromptSubmit": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "scripts/lint-check.sh",
"timeout": 15,
"if": "Write"
}
]
}
],
"SessionEnd": [
{
"matcher": "",
"hooks": [
{ "type": "command", "command": "scripts/cleanup.sh", "timeout": 5 }
]
}
]
}
}實(shí)用示例
1. 每次啟動加載項(xiàng)目文檔
{
"hooks": {
"SessionStart": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "cat .claude/project-guide.md",
"timeout": 5,
"statusMessage": "Loading project guide..."
}
]
}
]
}
}2. 提交前做代碼規(guī)范檢查
{
"hooks": {
"UserPromptSubmit": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "node scripts/check-code-style.js \"$CLAUD_PROMPT\"",
"timeout": 10
}
]
}
]
}
}3. HTTP webhook 通知
{
"hooks": {
"SessionEnd": [
{
"matcher": "",
"hooks": [
{
"type": "http",
"url": "https://api.example.com/claude-end",
"timeout": 5,
"headers": {
"Authorization": "Bearer $MY_API_TOKEN"
},
"allowedEnvVars": ["MY_API_TOKEN"]
}
]
}
]
}
}4. 項(xiàng)目初始化時安裝依賴
{
"hooks": {
"Setup": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "test -f package.json && npm install || echo 'No package.json found'",
"timeout": 120,
"statusMessage": "Installing dependencies..."
}
]
}
]
}
}注意事項(xiàng)
- 超時控制:
timeout字段以秒為單位。Setup/SessionStart 默認(rèn)無嚴(yán)格限制,SessionEnd 默認(rèn) 1.5s 且被 failsafe(5s)兜底 - stdout 注入:SessionStart 和 UserPromptSubmit 的 stdout 會作為 system message 發(fā)給模型;Setup 和 SessionEnd 不會
- 異步執(zhí)行:
async: true的 command hook 在后臺運(yùn)行,不阻塞主流程。asyncRewake: true在 exit code 2 時可喚醒模型處理錯誤 - 條件過濾:
if字段使用權(quán)限規(guī)則語法(如"Bash(git *)"、"Write"),僅在匹配時執(zhí)行,避免無關(guān)操作觸發(fā) hook - once:設(shè)置為
true的 hook 執(zhí)行一次后自動從配置中移除 - 安全:http hook 的 header 環(huán)境變量插值需要顯式在
allowedEnvVars中聲明
以上就是Claude Code中生命周期Hooks的使用指南介紹的詳細(xì)內(nèi)容,更多關(guān)于Claude Code生命周期Hooks的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章

Claude Code Hooks 選裝實(shí)戰(zhàn)指南
Claudede Hooks實(shí)戰(zhàn)指南,涵蓋三級攔截體系,兩腳本開箱即用,覆蓋致命、高風(fēng)險、警告三級操作,通過配置settings.json靈活定制攔截策略,支持PowerShell與原生Toast通知,適用于2026-06-03
Claude Code 每次調(diào)用工具、等待輸入、結(jié)束會話,都會觸發(fā)對應(yīng)的Hook生命周期事件,Hook 腳本除了做判斷和記錄,還可以把事件轉(zhuǎn)發(fā)到本地 socket,讓一個常駐進(jìn)程處理所有狀2026-06-02


