pre-post-hooks
Skill4 starsChanged 9 months agoMITView on GitHub
Guide for configuring PreToolUse, PostToolUse, and Stop hooks in Claude Code. Use when the user asks about hooks, wants to run commands before/after tool calls, needs auto-formatting, auto-linting, validation gates, or mentions settings.json hook config.
- Commits and pushes
In this repository78 other files it gives its agents.
- AGENTS.mdAGENTS.md
- rules/agents.mdAGENTS.md
- .github/copilot-instructions.mdCopilot instructions
- .cursor/rules/claude-initial-setup.mdcCursor rule
- parallel-task-executionSkill
- subagent-patternsSkill
- task-coordinationSkill
- agent-communicationSkill
- context-managementSkill
- multi-agent-orchestrationSkill
- deploy-strategiesSkill
- github-actions-guideSkill
- pipeline-patternsSkill
- anthropic-sdk-guideSkill
- prompt-engineeringSkill
- tool-use-patternsSkill
- automated-review-setupSkill
- pr-standardsSkill
- review-checklistSkill
- file-organizationSkill
- immutability-patternsSkill
- naming-conventionsSkill
- branching-strategySkill
- commit-conventionsSkill
- pr-workflowSkill
- migration-patternsSkill
- query-optimizationSkill
- schema-design-guideSkill
- error-tracingSkill
- log-analysisSkill
- systematic-debuggingSkill
- django-admin-customizationSkill
- django-orm-masterySkill
- drf-api-patternsSkill
- docker-compose-guideSkill
- dockerfile-best-practicesSkill
- multi-stage-buildsSkill
- express-error-handlingSkill
- middleware-patternsSkill
- rest-api-nodeSkill
- async-fastapiSkill
- dependency-injection-fastapiSkill
- pydantic-validationSkill
- concurrency-patterns-goSkill
- error-handling-goSkill
- idiomatic-goSkill
- automation-triggersSkill
- hook-recipesSkill
- junit-testingSkill
- maven-gradle-guideSkill
- spring-boot-patternsSkill
- mcp-resource-patternsSkill
- mcp-server-creationSkill
- mcp-tool-designSkill
- context-optimizationSkill
- knowledge-persistenceSkill
- memory-patternsSkill
- caching-strategiesSkill
- lazy-loading-patternsSkill
- profiling-guideSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 4 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 86
Use pipe | for multiple matchers:
- Distinctive: line 127
Run checks or formatting after tool execution without blocking:
- Distinctive: line 160
Slow hooks on hot paths: Do not run expensive commands (full test suite, large builds) in PostToolUse on every Edit.
- Distinctive: line 161
Blocking reads: Do not use PreToolUse to block Read or Grep calls.
- Very specific: line 163
Hardcoded paths: Use $CLAUDE_FILE_PATH instead of hardcoding file paths.
- Distinctive: line 176
Blocking: Only PreToolUse can block (non-zero exit code)
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Pre/Post Hooks
Claude Code hooks are shell commands that execute automatically in response to tool events. They enable validation, formatting, and guardrails without manual intervention.
When to Use
- User wants to run a command before or after a tool executes
- User asks about PreToolUse, PostToolUse, or Stop hooks
- User wants to add validation gates or blockers
- User needs to configure hooks in settings.json
- User mentions auto-running commands on file edit, git push, or session end
Core Patterns
Hook Types and Lifecycle
PreToolUse --> Tool Executes --> PostToolUse
|
(session ends)
|
Stop- PreToolUse: Runs before a tool call. Can block execution by returning a non-zero exit code.
- PostToolUse: Runs after a tool call completes. Used for side effects like formatting or checks.
- Stop: Runs when the agent session ends. Used for final audits or cleanup.
Hook Configuration in settings.json
Hooks live in ~/.claude/settings.json under the hooks key:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write|Edit",
"command": "echo 'About to write a file'",
"description": "Log before file writes"
}
],
"PostToolUse": [
{
"matcher": "Edit",
"command": "npx prettier --write \"$CLAUDE_FILE_PATH\"",
"description": "Auto-format after editing JS/TS files"
}
],
"Stop": [
{
"command": "echo 'Session ended'",
"description": "Log session end"
}
]
}
}Matcher Patterns
The matcher field filters which tool triggers the hook. It matches against tool names:
{
"matcher": "Edit",
"command": "..."
}Common tool names to match:
Write— file creationEdit— file modificationBash— shell command executionRead— file readingGlob— file searchGrep— content search
Use pipe | for multiple matchers:
{
"matcher": "Write|Edit",
"command": "npx prettier --write \"$CLAUDE_FILE_PATH\""
}Omit matcher entirely to match ALL tool calls (useful for Stop hooks).
Environment Variables in Hooks
Hooks receive context via environment variables:
| Variable | Description |
CLAUDE_FILE_PATH | The file path being operated on |
CLAUDE_TOOL_NAME | Name of the tool being called |
CLAUDE_SESSION_ID | Current session identifier |
Blocking with PreToolUse
A PreToolUse hook that exits non-zero blocks the tool from executing:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write",
"command": "echo \"$CLAUDE_FILE_PATH\" | grep -qv '\\.md$' || (echo 'BLOCKED: Do not create markdown files' && exit 1)",
"description": "Block creation of .md files"
}
]
}
}PostToolUse Side Effects
Run checks or formatting after tool execution without blocking:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit",
"command": "if echo \"$CLAUDE_FILE_PATH\" | grep -q '\\.ts$'; then npx tsc --noEmit \"$CLAUDE_FILE_PATH\" 2>&1 | head -20; fi",
"description": "Type-check TypeScript files after edit"
}
]
}
}Stop Hook for Final Audit
{
"hooks": {
"Stop": [
{
"command": "git diff --cached --name-only | xargs grep -l 'console.log' 2>/dev/null && echo 'WARNING: console.log found in staged files'",
"description": "Audit staged files for console.log before session ends"
}
]
}
}Anti-Patterns
- Slow hooks on hot paths: Do not run expensive commands (full test suite, large builds) in PostToolUse on every Edit. It blocks the agent loop. Use targeted, fast checks instead.
- Blocking reads: Do not use PreToolUse to block Read or Grep calls. These are non-destructive and blocking them disrupts exploration.
- Missing error handling: Hook commands that fail silently hide problems. Always include meaningful error messages in blocking hooks.
- Hardcoded paths: Use
$CLAUDE_FILE_PATHinstead of hardcoding file paths. Hooks should be project-agnostic. - Too many hooks: Each hook adds latency to every matching tool call. Keep the hook list focused on high-value checks.
Quick Reference
| Hook Type | Timing | Can Block? | Use Case |
| PreToolUse | Before tool | Yes (exit 1) | Validation, gates, warnings |
| PostToolUse | After tool | No | Formatting, type-checking, linting |
| Stop | Session end | No | Final audits, cleanup |
Config location: ~/.claude/settings.json > hooks
Matcher syntax: Tool name string, pipe-separated for multiple
Blocking: Only PreToolUse can block (non-zero exit code)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

