Hooks
Intercept and modify agent behavior at various lifecycle points. Unlike callbacks (which are for UI events), hooks can intercept, modify, or block tool execution.Quick Start
Simplest Usage
Tip: Hook returns are simple:
Noneor no return → AllowFalse→ Deny"reason"→ Deny with custom message
Agent-Centric Usage
Hook Events
Core Events
| Event | Trigger | Use Case |
|---|---|---|
BEFORE_TOOL | Before tool execution | Security checks, logging |
AFTER_TOOL | After tool execution | Result logging, validation |
BEFORE_AGENT | Before agent runs | Setup, initialization |
AFTER_AGENT | After agent completes | Cleanup, reporting |
BEFORE_LLM | Before LLM API call | Request modification, logging |
AFTER_LLM | After LLM API response | Response logging, validation |
SESSION_START | When session starts | Session initialization |
SESSION_END | When session ends | Session cleanup |
ON_ERROR | When an error occurs | Error handling, recovery |
ON_RETRY | Before a retry attempt | Retry logic, backoff |
Extended Events
| Event | Trigger | Use Case |
|---|---|---|
USER_PROMPT_SUBMIT | When user submits a prompt | Input validation, logging |
NOTIFICATION | When notification is sent | Alert routing, logging |
SUBAGENT_STOP | When subagent completes | Subagent result handling |
SETUP | On initialization/maintenance | System setup, config loading |
BEFORE_COMPACTION | Before context compaction | Pre-compaction hooks |
AFTER_COMPACTION | After context compaction | Post-compaction validation |
MESSAGE_RECEIVED | When message is received | Message preprocessing |
MESSAGE_SENDING | Before message is sent | Message modification |
MESSAGE_SENT | After message is sent | Delivery confirmation |
GATEWAY_START | When gateway starts | Gateway initialization |
GATEWAY_STOP | When gateway stops | Gateway cleanup |
TOOL_RESULT_PERSIST | Before tool result storage | Result modification |
Hook Decisions
| Decision | Description |
|---|---|
allow | Allow the operation to proceed |
deny | Deny the operation with a reason |
block | Block the operation silently |
ask | Prompt for user confirmation |
CLI Commands
Low-level API Reference
HookRegistry Direct Usage
Shell Command Hooks
Register external scripts as hooks:Matcher Patterns
Configuration File
Create.praison/hooks.json in your project:
LLM Hooks
Intercept LLM API calls for logging, modification, or validation:Error and Retry Hooks
Handle errors and customize retry behavior:Best Practices
- Keep hooks lightweight - Hooks run synchronously, avoid heavy operations
- Use matchers - Only run hooks for relevant tools
- Return early - Return
allowquickly for non-matching cases - Log decisions - Log why hooks deny operations for debugging
- Handle errors - Wrap hook logic in try/except to avoid breaking agents

