Back to PodcastsClaude
Claude Code 101
Hooks in Claude Code
Hooks let you run commands at different points in Claude code's life cycle.
Hooks ermöglichen es dir, Befehle an verschiedenen Punkten im Lebenszyklus von Claude Code auszuführen.
The key difference between hooks and everything else we covered is that hooks are deterministic.
Der wesentliche Unterschied zwischen hooks und allem anderen, was wir besprochen haben, ist, dass hooks deterministisch sind.
They always run.
Sie laufen immer.
So, put it this way.
So gesagt.
You can tell Claude in your claude.md file to run prettier after every file edit and most of the time it will do that, but sometimes it won't.
Du kannst Claude in deiner claude.md-Datei anweisen, nach jeder Dateibearbeitung prettier auszuführen, und die meiste Zeit wird es das tun, aber manchmal nicht.
It's not perfect.
Es ist nicht perfekt.
But a hook makes it happen every single time with no exceptions.
Aber ein hook sorgt dafür, dass es jedes Mal ohne Ausnahme passiert.
Common use cases could include auto formatting after file edits, logging all executed commands for compliance, blocking dangerous operations like modifying production files, and sending yourself notifications when Claude finishes a task.
Typische Anwendungsfälle sind automatisches Formatieren nach Dateibearbeitungen, Protokollieren aller ausgeführten Befehle für Compliance-Zwecke, Blockieren gefährlicher Aktionen wie das Ändern von Produktionsdateien und das Senden von Benachrichtigungen, wenn Claude eine Aufgabe abgeschlossen hat.
Hooks are configured in your settings.json file.
Hooks werden in deiner settings.json-Datei konfiguriert.
You pick an event, optionally set a matcher for which tool it applies to, and provide a command to run.
Du wählst ein Ereignis, setzt optional einen Matcher für das betreffende Tool und gibst einen auszuführenden Befehl an.
User prompts submit runs when you submit a prompt before Claude processes it.
UserPromptSubmit wird ausgeführt, wenn du einen Prompt sendest, bevor Claude ihn verarbeitet.
Pre-tool use which runs before a tool call, post-tool use runs after a tool call completes.
PreToolUse wird vor einem Tool-Aufruf ausgeführt, PostToolUse wird nach Abschluss eines Tool-Aufrufs ausgeführt.
Notification runs when Claude sends a notification, and stop runs when Claude finishes responding.
Notification wird ausgeführt, wenn Claude eine Benachrichtigung sendet, und Stop wird ausgeführt, wenn Claude die Antwort beendet.
The most common hook.
Der häufigste hook.
Auto formatting after edits.
Automatisches Formatieren nach Bearbeitungen.
You set a post-tool use hook with a matcher of edit or multi-edit, right?
Du setzt einen PostToolUse hook mit einem Matcher für edit oder multi-edit, richtig?
So, it fires whenever Claude modifies a file.
Damit wird er ausgelöst, wenn Claude eine Datei ändert.
The command checks the file extension and runs the appropriate formatter.
Der Befehl prüft die Dateiendung und führt den passenden Formatierer aus.
This could be prettier for TypeScript, go format for go, rough for Python, whatever your project uses.
Das könnte prettier für TypeScript, go format für Go, ruff für Python sein, was auch immer dein Projekt verwendet.
Pre-tool use hooks can block tool calls before they execute.
PreToolUse hooks können Tool-Aufrufe blockieren, bevor sie ausgeführt werden.
So, your hook receives the tool name and input as JSON on stdin.
Dein hook erhält den Tool-Namen und die Eingabe als JSON über stdin.
If it exits with code two, the action is blocked and the STD error message gets fed back to Claude's feedback so Claude knows why it was blocked and can adjust.
Wenn er mit Code 2 beendet wird, wird die Aktion blockiert und die Fehlermeldung wird an Claude zurückgemeldet, damit Claude weiß, warum es blockiert wurde, und sich anpassen kann.
Exit code zero means proceed.
Exit-Code 0 bedeutet fortfahren.
Exit code two means block.
Exit-Code 2 bedeutet blockieren.
This is how you enforce hard rules.
So erzwingst du harte Regeln.
Block writes to a production config directory, block bash commands that contain rm-rf, block commits to main, whatever your team needs to be guaranteed, not suggested.
Schreibzugriffe auf ein Produktions-Konfigurationsverzeichnis blockieren, bash-Befehle mit rm-rf blockieren, Commits auf main blockieren, was auch immer dein Team garantieren muss, nicht nur vorschlagen.
Hooks configured in .Claude/settings.json are project level and can be checked into your repo. This means that your entire team gets the same hooks automatically.
In .Claude/settings.json konfigurierte hooks sind auf Projektebene und können in dein Repository eingecheckt werden. Das bedeutet, dass dein gesamtes Team automatisch dieselben hooks erhält.
Use the Claude project dir environment variable in your commands to reference scripts stored in your project so they work regardless of Claude's current working directory.
Verwende die Umgebungsvariable des Claude-Projektverzeichnisses in deinen Befehlen, um auf Skripte zu verweisen, die in deinem Projekt gespeichert sind, damit sie unabhängig vom aktuellen Arbeitsverzeichnis von Claude funktionieren.
[music]
[music]
Hooks gives you deterministic control over Claude code's behavior.
Hooks geben dir deterministische Kontrolle über das Verhalten von Claude Code.
[music]
[music]
Use post-tool-use for auto-formatting and logging. Use pre-tool-use to block dangerous operations. Configure them in the /hooks or in settings.
Verwende PostToolUse für automatisches Formatieren und Protokollieren. Verwende PreToolUse, um gefährliche Operationen zu blockieren. Konfiguriere sie in /hooks oder in settings.
json
json
[music]
[music]
and check them into your repository so your team gets them, too.
und checke sie in dein Repository ein, damit dein Team sie auch erhält.
If something needs to happen every time without fail, don't put it in a prompt.
Wenn etwas jedes Mal ohne Ausnahme passieren muss, leg es nicht in einen Prompt.
Put it in a hook.
Leg es in einen hook.
[music]
[music]