Claude Code Hooks: Tự động hóa mọi thứ trong development pipeline
ISC Team
ISC Vibe Marketplace
Một trong những tính năng mạnh nhất nhưng ít được biết đến nhất của Claude Code là Hooks. Trong khi Skills và Agents tận dụng AI reasoning, Hooks hoạt động hoàn toàn khác: chúng là pure automation — deterministic, nhanh, và không tốn một token nào.
Hooks là gì?
Hooks là shell commands hoặc scripts được Claude Code tự động thực thi tại các lifecycle events nhất định trong session. Chúng chạy bên ngoài agentic loop — không có AI reasoning, không có latency từ LLM.
Nguyên tắc cốt lõi: Hooks xử lý những gì luôn luôn phải xảy ra — formatting, linting, testing, notifications. Đây là những việc không cần AI thinking, chỉ cần execute.
Các Hook Events
| Event | Khi nào kích hoạt | Use case phổ biến |
|---|---|---|
| PreToolUse | Trước khi tool chạy | Validate, block lệnh nguy hiểm |
| PostToolUse | Sau khi tool chạy thành công | Auto-format, auto-test |
| Stop | Khi agent hoàn thành response | Notification, status update |
| UserPromptSubmit | Trước khi xử lý message user | Log prompt, filter input |
Cấu hình Hooks
Hooks được định nghĩa trong ~/.claude/settings.json (cho toàn bộ user) hoặc .claude/settings.json trong project (chỉ dự án đó):
{
"hooks": {
"post-tool-use": [
{
"matcher": {
"tools": ["Write", "Edit"],
"paths": ["src/**/*.ts"]
},
"handler": {
"type": "command",
"command": "prettier --write"
}
}
],
"pre-tool-use": [
{
"matcher": {
"tools": ["Bash"],
"args": ["rm.*-rf|sudo"]
},
"handler": {
"type": "command",
"command": "sh -c 'echo "Blocked" >&2; exit 2'"
}
}
]
}
}6 Hook Thực Chiến Quan Trọng Nhất
1. Auto-Format Sau Khi Edit
Vấn đề: AI sinh ra code không đúng style của dự án. Giải pháp: PostToolUse hook chạy Prettier tự động.
{
"post-tool-use": [
{
"matcher": {
"tools": ["Write", "Edit", "MultiEdit"],
"paths": ["src/**/*.{ts,tsx,js,jsx}"]
},
"handler": {
"type": "command",
"command": "npx prettier --write"
}
}
]
}Kết quả: Mỗi lần Claude edit file → Prettier tự động format → Code luôn đúng style.
2. Chạy Tests Sau Khi Code Thay Đổi
{
"post-tool-use": [
{
"matcher": {
"tools": ["Write", "Edit"],
"paths": ["src/**/*.{ts,tsx}"]
},
"handler": {
"type": "command",
"command": "npm run test -- --passWithNoTests --bail"
}
}
]
}3. Block Lệnh Bash Nguy Hiểm
Exit code 2 sẽ block operation. Exit code 0 hoặc 1 cho phép tiếp tục.
{
"pre-tool-use": [
{
"matcher": {
"tools": ["Bash"],
"args": ["rm.*-rf|sudo|dd if=|chmod 000"]
},
"handler": {
"type": "command",
"command": "sh -c 'echo "Lệnh nguy hiểm bị block" >&2; exit 2'"
}
}
]
}4. Thông Báo Git Push
{
"post-tool-use": [
{
"matcher": {
"tools": ["Bash"],
"args": ["git.*push|git.*commit"]
},
"handler": {
"type": "command",
"command": "osascript -e 'display notification "Git operation detected" with title "Claude Code"'"
}
}
]
}5. ESLint Trước Khi Commit
{
"pre-tool-use": [
{
"matcher": {
"tools": ["Bash"],
"args": ["git.*commit"]
},
"handler": {
"type": "command",
"command": "npm run lint || exit 2"
}
}
]
}6. Audit Log Toàn Bộ Tool Usage
{
"post-tool-use": [
{
"matcher": { "tools": ["*"] },
"handler": {
"type": "command",
"command": "sh -c 'echo "$(date): $CLAUDE_TOOL - $CLAUDE_FILEPATH" >> ~/.claude_audit.log'"
}
}
]
}Output audit log:
2026-03-02T10:15:22Z: Edit - src/App.tsx
2026-03-02T10:15:35Z: Bash - npm run test
2026-03-02T10:15:45Z: Write - src/components/Button.tsxEnvironment Variables Trong Hooks
Claude Code cung cấp một số biến môi trường hữu ích:
$CLAUDE_TOOL # Tên tool (Write, Edit, Bash, ...)
$CLAUDE_FILEPATH # File đang được modify
$CLAUDE_ARGS # Arguments của toolBest Practices
- Dùng matchers cụ thể: Chỉ match đúng paths và tools cần thiết, tránh
tools: ["*"]cho PreToolUse - Hiệu suất: Hooks nên chạy dưới 1 giây. Tránh operations nặng trong PreToolUse
- Exit codes: Chỉ dùng
exit 2để block trong PreToolUse - Test hooks: Test script hook thủ công trước khi enable
Kết Luận
Hooks là lớp automation giúp development pipeline của bạn trở nên deterministic và reliable. Trong khi AI handling creativity và problem-solving, Hooks xử lý những việc lặp đi lặp lại một cách đáng tin cậy — không cần token, không cần latency. Đây là sức mạnh thực sự của Claude Code ecosystem.