Hooks are additive: global and workspace configurations are merged. Workspace hooks complement global ones — they never replace them.
Configuration
Hooks are defined in JSON files at two levels:| Level | File | Scope |
|---|---|---|
| Global | ~/.chatcli/hooks.json | All projects and sessions |
| Workspace | .chatcli/hooks.json | Current project only |
File Structure
Available Events
ChatCLI emits 9 lifecycle events that hooks can bind to:| Event | When it fires | Blocking |
|---|---|---|
SessionStart | When a new ChatCLI session starts | No |
SessionEnd | When the session ends (exit/quit) | No |
PromptSubmit | When the user submits a prompt | No |
PreToolUse | Before executing a tool | Yes |
PostToolUse | After a tool executes successfully | No |
ToolError | When a tool fails | No |
AgentStart | When entering agent/coder mode | No |
AgentEnd | When exiting agent/coder mode | No |
CompactDone | After a history compaction | No |
Hook Types
- Command (Shell)
- HTTP (Webhook)
Executes a shell command on the operating system. The command has access to environment variables with event context.Exit codes:
0— Success (execution continues normally)1— Error (logged, but does not block)2— Blocks the operation (only forPreToolUse)
The command is executed via
sh -c on Linux/macOS and cmd /c on Windows.ToolPattern — Tool Filtering
ThetoolPattern field lets you filter which tools trigger the hook. It accepts glob patterns:
| Pattern | Matched tools |
|---|---|
write | Only write |
write|patch | write or patch |
exec* | exec, exec_background, etc. |
mcp_* | All MCP tools |
* | All tools (default if omitted) |
Environment Variables
Command hooks receive environment variables with event context:| Variable | Description | Example |
|---|---|---|
CHATCLI_HOOK_EVENT | Name of the event that triggered the hook | PostToolUse |
CHATCLI_HOOK_TOOL | Tool name (if applicable) | write |
CHATCLI_HOOK_SESSION | Current session ID | session_abc123 |
CHATCLI_HOOK_FILE | Affected file path (if applicable) | /project/main.go |
CHATCLI_HOOK_CWD | Current working directory | /Users/dev/project |
CHATCLI_HOOK_MODE | Current mode (chat, agent, coder) | agent |
Complete Examples
- Auto-Format
- Notifications
- Block Commands
- Audit
Auto-format Go files after any edit:
Use Cases
Auto-Format
Run formatters (gofmt, prettier, black) automatically after file edits.
Notifications
Send alerts to Slack, Discord, or email at the end of sessions or on errors.
Guardrails
Block dangerous commands (rm -rf, DROP TABLE, force push) with PreToolUse.
Audit
Log all agent actions to files for compliance.
Auto-Test
Run tests automatically after every code edit.
Linting
Run linters (golangci-lint, eslint) after every write/patch.
Next Steps
Coder Security
Security policies and approval for coder operations.
Security
Understand the ChatCLI security model.
Compact UI
Minimalist display mode for coder mode.
Coder Mode
The full engineering cycle with integrated hooks.