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:Agent-Level Error Callbacks
Agents now support anon_error callback that is called when LLM errors occur, separate from the HookEvent system:
Error Hook vs Agent Callback
| Type | Purpose | When Called | Scope |
|---|---|---|---|
| HookEvent.ON_ERROR | General error handling | Any error in hook system | All registered hooks |
| agent.on_error | LLM-specific errors | When _chat_completion fails | Single agent instance |
Error Handler Behavior
- Exception Swallowing: Exceptions inside the
on_errorcallback are swallowed and logged at debug level - Execution Order:
on_erroris called before theLLMErroris raised - No Return Value: The callback cannot prevent error propagation (unlike hooks)
Strict mode: fail loud on hook errors
By default, exceptions inside a hook are logged as warnings and the agent continues. Settingagent._strict_hooks = True re-raises the exception so failures are surfaced immediately — useful in tests, staging, or when a hook enforces a compliance check that must not be silently ignored.
| Mode | Hook raises | Default? |
|---|---|---|
| Relaxed (default) | Warning is logged, execution continues | ✅ |
Strict (_strict_hooks = True) | Exception propagates, call aborts |
BEFORE_COMPACTION, AFTER_COMPACTION) and the outer compaction block; follow-up PRs may extend coverage.
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
- Hook failures now log warnings by default - Check logs for
BEFORE_COMPACTION hook failed/AFTER_COMPACTION hook failedlines - Use
agent._strict_hooks = Truein tests or staging - To fail loudly when a hook raises

