Was sind Hooks?
Hooks sind Shell-Befehle, die an bestimmten Lifecycle-Punkten ausgeführt werden. Sie laufen deterministisch (immer, ohne LLM-Entscheidung).
- Code formatieren nach Datei-Änderungen
- Geschützte Dateien blocken vor Edits
- Benachrichtigungen senden bei Permission-Requests
- Kontext injizieren nach Session-Start
- Änderungen auditieren für Compliance
Lifecycle Events
| Event | Wann | Blockiert? |
SessionStart | Session beginnt/wird fortgesetzt | Nein |
UserPromptSubmit | Vor Prompt-Verarbeitung | Nein |
PreToolUse | Vor Tool-Ausführung | Ja |
PostToolUse | Nach Tool-Erfolg | Nein |
PostToolUseFailure | Nach Tool-Fehler | Nein |
PermissionRequest | Permission-Dialog erscheint | Nein |
Stop | Claude beendet Antwort | Ja |
Notification | Benachrichtigung gesendet | Nein |
ConfigChange | Config-Datei geändert | Ja |
SessionEnd | Session endet | Nein |
Hook-Typen
| Typ | Beschreibung | Use Case |
command | Shell-Befehl (empfohlen) | Deterministische Regeln |
prompt | Einmal-LLM-Abfrage | Urteilsentscheidungen |
agent | Multi-Turn mit Tools | Verifikation mit Datei-Zugriff |
http | POST an URL | Externe Services |
Exit Codes: 0 = Aktion erlauben (stdout → Claude Context), 2 = Aktion blockieren (stderr → Feedback an Claude)
Ersten Hook einrichten
# Interaktives Menü
/hooks
# → Event wählen (z.B. "Notification")
# → "Add new hook" → Command eingeben
# → Scope wählen (User/Project)
powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Claude braucht Aufmerksamkeit', 'Claude Code')"
Praxisbeispiele
{
"hooks": {
"PostToolUse": [{
"matcher": "Edit|Write",
"hooks": [{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}]
}]
}
}
#!/bin/bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
PROTECTED=(".env" "package-lock.json" ".git/")
for pattern in "${PROTECTED[@]}"; do
if [[ "$FILE_PATH" == *"$pattern"* ]]; then
echo "Blocked: $FILE_PATH" >&2
exit 2
fi
done
exit 0
{
"hooks": {
"SessionStart": [{
"matcher": "compact",
"hooks": [{
"type": "command",
"command": "echo 'Reminder: Nutze pnpm, nicht npm.'"
}]
}]
}
}
{
"hooks": {
"PostToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "jq -r '.tool_input.command' >> ~/.claude/command-log.txt"
}]
}]
}
}
CI/CD Integration
# Einmal-Abfrage
claude -p "erkläre diese Funktion"
# Mit Budget-Limit
claude -p "review code" --max-budget-usd 5.00
# Turn-Limit
claude -p "fix tests" --max-turns 3
# JSON Output
claude -p "analyse" --output-format json
name: Claude Code
on:
pull_request:
types: [opened, synchronize]
jobs:
claude:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: anthropics/claude-code-action@v1
with:
api_key: $
command: "Review this PR"
Konfiguration Scopes
| Ort | Scope | Teilbar? |
~/.claude/settings.json | Alle Projekte | Nein |
.claude/settings.json | Einzelnes Projekt | Ja (Git) |
.claude/settings.local.json | Einzelnes Projekt | Nein (.gitignore) |
Nutze /hooks zum interaktiven Hinzufügen — Änderungen wirken sofort.