Automate workflows with hooks
3 Min. Lesezeit•Docs von Anthropic Docs•Tags: tooling/claude-code, workflows/automation, hooks, docs
Hooks sind benutzerdefinierte Shell-Befehle, die an bestimmten Punkten im Lebenszyklus von Claude Code ausgeführt werden. Sie bieten deterministische Kontrolle über das Verhalten von Claude Code.
Erster Hook: Desktop-Benachrichtigung
Erstelle einen Hook, der dich benachrichtigt, wenn Claude Input benötigt.
- Öffne das Hooks-Menü mit
/hooks. - Wähle
Notification. - Setze Matcher auf
*. - Füge den Befehl für dein OS hinzu:
- macOS:
osascript -e 'display notification "Claude Code needs your attention" with title "Claude Code"' - Linux:
notify-send 'Claude Code' 'Claude Code needs your attention' - Windows:
powershell.exe -Command "[System.Windows.Forms.MessageBox]::Show('Claude Code needs your attention', 'Claude Code')"
- macOS:
- Speichere in
User settings.
Was du automatisieren kannst
Code nach Edits formatieren
Führe Prettier automatisch nach jedem Datei-Edit aus. Füge dies zu .claude/settings.json hinzu:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}
]
}
]
}
}
Geschützte Dateien blockieren
Blockiere Änderungen an .env oder package-lock.json.
- Erstelle
.claude/hooks/protect-files.sh:#!/bin/bash INPUT=$(cat) FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty') if [[ "$FILE_PATH" == *".env"* ]]; then echo "Blocked: .env is protected" >&2 exit 2 fi exit 0 - Mache es ausführbar:
chmod +x .claude/hooks/protect-files.sh. - Registriere den Hook in
settings.jsonunterPreToolUse.
Wie Hooks funktionieren
| Event | Wann es feuert |
|---|---|
SessionStart | Session-Beginn |
PreToolUse | Vor Tool-Ausführung (kann blockieren) |
PostToolUse | Nach Tool-Ausführung |
Stop | Wenn Claude fertig ist |
Input & Output
- Input: JSON via
stdin(enthälttool_name,tool_input, etc.). - Output:
- Exit 0: Weitermachen.
- Exit 2: Blockieren (Grund nach
stderrschreiben). - JSON nach
stdout: Strukturierte Steuerung (z.B.permissionDecision: "deny").
Matcher
Filtere Hooks nach Tool-Namen oder Event-Typ.
matcher: "Bash"-> Feuert nur bei Bash-Commands.matcher: "Edit|Write"-> Feuert bei Datei-Änderungen.
Prompt- & Agent-Based Hooks
- Prompt-Based (
type: "prompt"): Nutzt ein LLM für Entscheidungen (Ja/Nein). - Agent-Based (
type: "agent"): Startet einen Subagent, der Tools nutzen kann, um Bedingungen zu prüfen (z.B. "Laufen alle Tests?").
Troubleshooting
- Hook feuert nicht: Prüfe Matcher und Event-Typ.
- JSON-Fehler: Stelle sicher, dass deine Shell-Profile (
.bashrc) keine unbedingtenecho-Ausgaben machen. - Debug: Nutze
Ctrl+Ofür Verbose-Mode.
Verbindungen
- [[Hooks]]
- [[Automation]]
- [[PreToolUse]]
- [[PostToolUse]]
- [[Prettier]]
- [[Subagents]]
- [[Claude Code]]
- [[Deterministic Control]]