Skip to main content
The ChatCLI Hooks System lets you execute automatic actions in response to application lifecycle events. With hooks, you can auto-format code after edits, send notifications, block dangerous commands, log audit trails, and much more.
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:
LevelFileScope
Global~/.chatcli/hooks.jsonAll projects and sessions
Workspace.chatcli/hooks.jsonCurrent project only
Workspace hooks are additive — they combine with global hooks. If the same event has hooks at both levels, all are executed (global first, then workspace).

File Structure

{
  "hooks": [
    {
      "event": "PostToolUse",
      "type": "command",
      "command": "gofmt -w {{.FilePath}}",
      "toolPattern": "write|patch",
      "description": "Auto-format Go files after edits"
    },
    {
      "event": "SessionEnd",
      "type": "http",
      "url": "https://hooks.example.com/chatcli",
      "description": "Notify team of session end"
    }
  ]
}

Available Events

ChatCLI emits 9 lifecycle events that hooks can bind to:
EventWhen it firesBlocking
SessionStartWhen a new ChatCLI session startsNo
SessionEndWhen the session ends (exit/quit)No
PromptSubmitWhen the user submits a promptNo
PreToolUseBefore executing a toolYes
PostToolUseAfter a tool executes successfullyNo
ToolErrorWhen a tool failsNo
AgentStartWhen entering agent/coder modeNo
AgentEndWhen exiting agent/coder modeNo
CompactDoneAfter a history compactionNo
The PreToolUse event is blocking: if the hook returns exit code 2, the tool execution is blocked. This allows you to create guardrails that prevent dangerous operations.

Hook Types

Executes a shell command on the operating system. The command has access to environment variables with event context.
{
  "event": "PostToolUse",
  "type": "command",
  "command": "gofmt -w {{.FilePath}}",
  "toolPattern": "write|patch",
  "description": "Auto-format Go files after write/patch"
}
Exit codes:
  • 0 — Success (execution continues normally)
  • 1 — Error (logged, but does not block)
  • 2Blocks the operation (only for PreToolUse)
The command is executed via sh -c on Linux/macOS and cmd /c on Windows.

ToolPattern — Tool Filtering

The toolPattern field lets you filter which tools trigger the hook. It accepts glob patterns:
PatternMatched tools
writeOnly write
write|patchwrite or patch
exec*exec, exec_background, etc.
mcp_*All MCP tools
*All tools (default if omitted)
{
  "event": "PreToolUse",
  "type": "command",
  "command": "echo 'BLOCKED: exec not allowed' && exit 2",
  "toolPattern": "exec*",
  "description": "Block all exec commands"
}

Environment Variables

Command hooks receive environment variables with event context:
VariableDescriptionExample
CHATCLI_HOOK_EVENTName of the event that triggered the hookPostToolUse
CHATCLI_HOOK_TOOLTool name (if applicable)write
CHATCLI_HOOK_SESSIONCurrent session IDsession_abc123
CHATCLI_HOOK_FILEAffected file path (if applicable)/project/main.go
CHATCLI_HOOK_CWDCurrent working directory/Users/dev/project
CHATCLI_HOOK_MODECurrent mode (chat, agent, coder)agent

Complete Examples

Auto-format Go files after any edit:
{
  "hooks": [
    {
      "event": "PostToolUse",
      "type": "command",
      "command": "if [[ \"$CHATCLI_HOOK_FILE\" == *.go ]]; then gofmt -w \"$CHATCLI_HOOK_FILE\"; fi",
      "toolPattern": "write|patch",
      "description": "Auto-format Go files"
    }
  ]
}

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.