CLAUDE CODE · HOOKS
Claude Code Hooks
Automate your workflow with hooks — run shell commands at deterministic lifecycle points when Claude edits files, completes tasks, or needs your input.Tự động hóa workflow với hooks — chạy lệnh shell tại các điểm vòng đời xác định khi Claude sửa file, hoàn thành task, hoặc cần input từ bạn.
Hooks là lệnh shell do bạn định nghĩa, được Claude Code chạy đúng tại các điểm vòng đời xác định — không phụ thuộc vào quyết định của LLM. Đây là cơ chế deterministic để tự động hóa format, audit, bảo vệ file, và thông báo desktop.
3 điều cần nhớ
- Hơn 20 sự kiện vòng đời.
PreToolUse,PostToolUse,Notification,SessionStart,Stop,PreCompact,FileChanged… mỗi event là một điểm chèn deterministic. - 5 loại hook.
command(shell),http(webhook),mcp_tool,prompt(LLM judge),agent(Claude đa lượt). Chọn đúng mô hình thực thi cho từng use case. - Exit code điều khiển flow.
exit 0→ tiến hành;exit 2→ chặn hành động và gửi stderr cho Claude làm phản hồi; JSON stdout → quyết định có cấu trúc (allow/deny + reason).
Pattern phổ biến
- Thông báo desktop khi Claude cần input qua
Notification. - Auto-format chạy Prettier/ESLint sau mỗi
Edit|WritequaPostToolUse. - Bảo vệ file chặn sửa
.env,package-lock.json,.gitquaPreToolUse. - Audit log ghi lại mọi
ConfigChangera file để theo dõi tuân thủ. - Auto-approve bỏ qua hộp thoại xin quyền cho tool đáng tin (vd.
ExitPlanMode).
Mẹo vận hành
- Dùng
matcherđể giới hạn phạm vi — càng hẹp càng tốt, tránh tự động hóa quá rộng. - Xác minh bằng
/hooks(hooks browser) — nếu không thấy event, kiểm tra regex vàchmod +x. - Không trộn
exit 2với JSON stdout — chọn một. Stophook chạy cho mỗi chunk stream → cần logic thoát sớm để khỏi loop.
Slide deck (10 trang, tiếng Việt) có sẵn để tải về bên dưới — tham chiếu đầy đủ events, hook types, matchers, troubleshooting.
Hooks are shell commands you define that Claude Code runs at exact lifecycle points — independent of any LLM decision. This is the deterministic mechanism for automating formatting, auditing, file protection, and desktop notifications.
3 things to remember
- 20+ lifecycle events.
PreToolUse,PostToolUse,Notification,SessionStart,Stop,PreCompact,FileChanged… each event is a deterministic injection point. - 5 hook types.
command(shell),http(webhook),mcp_tool,prompt(LLM judge),agent(multi-turn Claude). Pick the right execution model for each use case. - Exit codes control flow.
exit 0→ proceed;exit 2→ block the action and send stderr to Claude as feedback; JSON stdout → structured decision (allow/deny + reason).
Common patterns
- Desktop notification when Claude needs input via
Notification. - Auto-format run Prettier/ESLint after every
Edit|WriteviaPostToolUse. - File protection block edits to
.env,package-lock.json,.gitviaPreToolUse. - Audit log record every
ConfigChangeto a file for compliance tracking. - Auto-approve skip permission dialogs for trusted tools (e.g.
ExitPlanMode).
Operational tips
- Use
matcherto narrow scope — the tighter the better, avoid over-automating. - Verify with
/hooks(hooks browser) — if you don’t see the event, check regex andchmod +x. - Don’t mix
exit 2with JSON stdout — pick one. Stophook fires per stream chunk → add an early-exit guard to avoid loops.
Slide deck (10 slides, English version) available for download below — full reference for events, hook types, matchers, and troubleshooting.