Terug naar podcastsClaude
Claude Code 101
Hooks in Claude Code
Hooks let you run commands at different points in Claude code's life cycle.
Hooks stellen je in staat om opdrachten uit te voeren op verschillende momenten in de levenscyclus van Claude Code.
The key difference between hooks and everything else we covered is that hooks are deterministic.
Het belangrijkste verschil tussen hooks en alles wat we hebben besproken, is dat hooks deterministisch zijn.
They always run.
Ze worden altijd uitgevoerd.
So, put it this way.
Laat me het zo zeggen.
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.
Je kunt Claude in je claude.md-bestand vertellen om prettier uit te voeren na elke bestandsbewerking en het doet dat de meeste tijd, maar soms niet.
It's not perfect.
Het is niet perfect.
But a hook makes it happen every single time with no exceptions.
Maar een hook zorgt ervoor dat het elke keer zonder uitzondering plaatsvindt.
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.
Veelvoorkomende gebruiksscenario's zijn automatisch formatteren na bestandsbewerkingen, het loggen van alle uitgevoerde opdrachten voor compliance, het blokkeren van gevaarlijke bewerkingen zoals het wijzigen van productiebestanden, en het sturen van meldingen aan jezelf wanneer Claude een taak heeft voltooid.
Hooks are configured in your settings.json file.
Hooks worden geconfigureerd in je settings.json-bestand.
You pick an event, optionally set a matcher for which tool it applies to, and provide a command to run.
Je kiest een gebeurtenis, stelt optioneel een matcher in voor welk tool het van toepassing is, en geeft een opdracht om uit te voeren.
User prompts submit runs when you submit a prompt before Claude processes it.
UserPromptSubmit wordt uitgevoerd wanneer je een prompt indient voordat Claude het verwerkt.
Pre-tool use which runs before a tool call, post-tool use runs after a tool call completes.
PreToolUse wordt uitgevoerd voor een toolaanroep, PostToolUse wordt uitgevoerd nadat een toolaanroep is voltooid.
Notification runs when Claude sends a notification, and stop runs when Claude finishes responding.
Notification wordt uitgevoerd wanneer Claude een melding stuurt, en Stop wordt uitgevoerd wanneer Claude klaar is met reageren.
The most common hook.
De meest voorkomende hook.
Auto formatting after edits.
Automatisch formatteren na bewerkingen.
You set a post-tool use hook with a matcher of edit or multi-edit, right?
Je stelt een PostToolUse hook in met een matcher voor edit of multi-edit, toch?
So, it fires whenever Claude modifies a file.
Zo wordt het elke keer geactiveerd als Claude een bestand wijzigt.
The command checks the file extension and runs the appropriate formatter.
De opdracht controleert de bestandsextensie en voert de juiste formatter uit.
This could be prettier for TypeScript, go format for go, rough for Python, whatever your project uses.
Dat kan prettier zijn voor TypeScript, go format voor Go, ruff voor Python, wat je project ook gebruikt.
Pre-tool use hooks can block tool calls before they execute.
PreToolUse hooks kunnen toolaanroepen blokkeren voordat ze worden uitgevoerd.
So, your hook receives the tool name and input as JSON on stdin.
Je hook ontvangt de toolnaam en invoer als JSON op 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.
Als het afsluit met code 2, wordt de actie geblokkeerd en wordt de standaardfoutmelding teruggekoppeld aan Claude zodat Claude weet waarom het geblokkeerd was en kan aanpassen.
Exit code zero means proceed.
Afsluitcode 0 betekent doorgaan.
Exit code two means block.
Afsluitcode 2 betekent blokkeren.
This is how you enforce hard rules.
Zo dwing je harde regels af.
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.
Schrijfbewerkingen naar een productie-configuratiemap blokkeren, bash-opdrachten die rm-rf bevatten blokkeren, commits naar main blokkeren, wat je team ook zeker moet stellen, niet alleen suggereren.
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.
Hooks geconfigureerd in .Claude/settings.json zijn op projectniveau en kunnen worden ingecheckt in je repository. Dit betekent dat je hele team automatisch dezelfde hooks krijgt.
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.
Gebruik de omgevingsvariabele voor de Claude-projectmap in je opdrachten om te verwijzen naar scripts die in je project zijn opgeslagen, zodat ze werken ongeacht de huidige werkmap van Claude.
[music]
[music]
Hooks gives you deterministic control over Claude code's behavior.
Hooks geven je deterministische controle over het gedrag van 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.
Gebruik PostToolUse voor automatisch formatteren en loggen. Gebruik PreToolUse om gevaarlijke bewerkingen te blokkeren. Configureer ze in /hooks of in settings.
json
json
[music]
[music]
and check them into your repository so your team gets them, too.
en check ze in je repository in zodat je team ze ook krijgt.
If something needs to happen every time without fail, don't put it in a prompt.
Als iets elke keer zonder mankeren moet plaatsvinden, zet het dan niet in een prompt.
Put it in a hook.
Zet het in een hook.
[music]
[music]