A ready-to-run example is available here!
Overview
Hooks let you observe and customize key lifecycle moments in the SDK without forking core code. Typical uses include:- Logging and analytics
- Emitting custom metrics
- Auditing or compliance
- Tracing and debugging
Hook Types
Exit Codes
Hook scripts signal their result through their exit code. The SDK matches the Claude Code hook contract:0— success. The operation proceeds.stdoutis parsed as JSON for structured output (decision,reason,additionalContext,continue).2— block. The operation is denied. ForPreToolUseandUserPromptSubmitthis rejects the action; forStopit prevents the agent from finishing and the conversation continues.stderr/reasonis surfaced as feedback.- Any other non-zero exit code — non-blocking error.
successis set toFalseand the error is logged viaHookExecutionEvent, but the operation still proceeds.
Key Concepts
- Registration points: subscribe to events or attach pre/post hooks around LLM calls and tool execution
- Isolation: hooks run outside the agent loop logic, avoiding core modifications
- Composition: enable or disable hooks per environment (local vs. prod)
Ready-to-run Example
This example is available on GitHub: examples/01_standalone_sdk/33_hooks
examples/01_standalone_sdk/33_hooks/main.py
The model name should follow the LiteLLM convention:
provider/model_name (e.g., anthropic/claude-sonnet-4-5-20250929, openai/gpt-4o).
The LLM_API_KEY should be the API key for your chosen provider.Hook Scripts
The example uses external hook scripts in thehook_scripts/ directory:
block_dangerous.sh - PreToolUse hook
block_dangerous.sh - PreToolUse hook
log_tools.sh - PostToolUse hook
log_tools.sh - PostToolUse hook
inject_git_context.sh - UserPromptSubmit hook
inject_git_context.sh - UserPromptSubmit hook
require_summary.sh - Stop hook
require_summary.sh - Stop hook
Next Steps
- See also: Metrics and Observability
- Architecture: Events

