Hooks
Event-driven actions triggered during agent execution
The hooks command manages event-driven hooks configured in .praisonai/hooks.json.
Quick Start
# List configured hooks
praisonai hooks list
Usage
List Hooks
praisonai hooks listExpected Output:
╭─ Configured Hooks ───────────────────────────────────────────────────────────╮
│ 🪝 pre_write_code - Validate before writing code │
│ 🪝 post_write_code - Format after writing code │
│ 🪝 pre_run_command - Validate before running a command │
╰──────────────────────────────────────────────────────────────────────────────╯When no hooks are configured, list prints an empty-state hint pointing at .praisonai/hooks.json.
Show Statistics
praisonai hooks statsInitialize Hooks
praisonai hooks initCreates a template .praisonai/hooks.json file.
Hooks Configuration
hooks init writes this template to .praisonai/hooks.json:
{
"enabled": true,
"hooks": {
"pre_write_code": [],
"pre_run_command": [],
"post_write_code": []
},
"settings": {
"timeout_seconds": 30,
"fail_on_error": false,
"log_hook_output": true
}
}Available Hook Events
| Event | Trigger |
|---|---|
pre_write_code / post_write_code | Around code writes |
pre_run_command / post_run_command | Around command execution |
pre_user_prompt / post_user_prompt | Around user prompt handling |
pre_mcp_tool_use / post_mcp_tool_use | Around MCP tool calls |
Hook Types
Shell Hooks
{
"post_write_code": {
"type": "shell",
"command": "black {file} && isort {file}"
}
}Python Hooks
{
"pre_run_command": {
"type": "python",
"module": "my_hooks",
"function": "validate_command"
}
}# my_hooks.py
def validate_command(context):
print(f"Validating command: {context['command']}")How It Works
- Load: Hooks are loaded from
.praisonai/hooks.json - Register: Hooks are registered for specific events
- Trigger: Events trigger corresponding hooks
- Execute: Hook commands/functions are executed with context
Context Variables
Hooks receive context variables that can be used in commands:
| Variable | Description |
|---|---|
{file} | File path being processed |
{content} | Content being written |
{command} | Command being run |
{result} | Result of operation |
Examples
Code Formatting Hook
{
"post_write_code": {
"type": "shell",
"command": "black {file} && isort {file}"
}
}Linting Hook
{
"pre_write_code": {
"type": "shell",
"command": "pylint {file} --errors-only"
}
}Command Validation Hook
{
"pre_run_command": {
"type": "python",
"module": "monitoring",
"function": "send_alert"
}
}Programmatic Usage
HooksManager is exported from praisonaiagents.memory, not the top-level package. The event fires only because your code calls .execute() — an Agent never does.
from praisonaiagents.memory import HooksManager
hooks = HooksManager(workspace_path=".")
# Register Python hooks
hooks.register("pre_write_code", lambda ctx: print(f"Writing {ctx['file_path']}"))
# You trigger the event yourself
result = hooks.execute("pre_write_code", context={"file_path": "main.py"})Best Practices
| Do | Don’t |
|---|---|
| Keep hooks fast and focused | Run long-running processes |
| Use for formatting and linting | Use for complex business logic |
| Log errors for debugging | Silently ignore failures |
| Test hooks independently | Deploy untested hooks |
Related
- Memory HooksManager — the standalone utility this CLI wraps
- Live Agent Hooks — hooks that fire automatically around Agent tool/LLM calls
- Rules CLI
- Workflow CLI