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

  • PreToolUse — Before a tool runs. Can block execution by exiting non-zero.
  • PostToolUse — After a tool completes.
  • Notification — When Cody sends a notification.
  • Stop — When Cody finishes a turn.

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:

  • CODY_TOOL_NAME — name of the tool being called
  • CODY_TOOL_FILE_PATH — file path (for file tools)
  • CODY_SESSION_ID — current session ID