Agentic Tutor/Terminal Forest/Hooks & Smart Routing
80 XP · 11 min
Sandbox replay

PreToolUse & PostToolUse Hooks

Hooks let you intercept the agent before it acts or right after — for validation, logging, or hard-stopping unsafe operations.

Sandbox transcript · canned, illustrative
user
Add a PreToolUse hook that blocks any Bash command containing `rm -rf` or `git push --force`.
assistant
Let me check the existing hooks config.
tool
tool: Read · .claude/settings.json
{ "permissions": { "allow": [...] }, "hooks": {} }
assistant
Adding a PreToolUse hook that matches the Bash tool and runs a guard script.
tool
tool: Edit · .claude/settings.json
+ "hooks": { + "PreToolUse": [{ + "matcher": "Bash", + "hooks": [{ "type": "command", "command": ".claude/hooks/guard-bash.sh" }] + }] + }
tool
tool: Write · .claude/hooks/guard-bash.sh
#!/bin/bash input=$(cat) cmd=$(echo "$input" | jq -r '.tool_input.command') if echo "$cmd" | grep -qE 'rm -rf|git push --force'; then echo '{"permissionDecision": "deny", "reason": "blocked by guard"}' exit 0 fi echo '{"permissionDecision": "allow"}'
tool
tool: Bash · chmod +x .claude/hooks/guard-bash.sh
assistant
Hook installed. From now on any Bash call containing `rm -rf` or `git push --force` is blocked before it runs. Want me to add a test by trying a blocked command?
Check your understanding
Q1.What does a PreToolUse hook with a 'deny' decision do?
· Score 100% on the quiz.