Skip to main content

Hooks

Hooks let you run shell commands automatically in response to Cody CLI events — before/after tool calls, on session start, on file writes, and more.

Configuration

Hooks are configured in settings.json under the hooks key:

{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "echo 'About to run bash tool'"
}
]
}
],
"PostToolUse": [
{
"matcher": "FileWrite",
"hooks": [
{
"type": "command",
"command": "prettier --write $CODY_TOOL_FILE_PATH"
}
]
}
]
}
}

Hook Events

EventWhen it firesCan block?
PreToolUseBefore a tool runsYes — exit non-zero to block
PostToolUseAfter a tool completesNo
NotificationWhen Cody sends a notificationNo
StopWhen Cody finishes a turnNo

Matchers

Each hook entry has a matcher field to filter which tool it applies to:

  • Bash — bash execution tool
  • FileWrite — file write tool
  • FileEdit — file edit tool
  • * — matches all tools

Environment Variables

Hook commands receive context via environment variables:

VariableValue
CODY_TOOL_NAMEName of the tool being called
CODY_TOOL_FILE_PATHFile path (for file tools)
CODY_SESSION_IDCurrent session ID

Example: Auto-format on write

{
"hooks": {
"PostToolUse": [
{
"matcher": "FileWrite",
"hooks": [{ "type": "command", "command": "prettier --write $CODY_TOOL_FILE_PATH 2>/dev/null || true" }]
}
]
}
}