Skip to content

Hooks

Hooks allow you to run scripts or commands in response to AI agent events. This is useful for:

  • Logging agent activity
  • Running security checks before tool use
  • Setting up environment context when a session starts
  • Cleaning up resources when a session ends

Define hooks in ai.json:

{
"hooks": {
"session_start": [
{
"matcher": ".*",
"hooks": [
{
"command": "./scripts/init-session.sh",
"show_output": true
}
]
}
],
"pre_tool_use": [
{
"matcher": "write_file",
"hooks": [
{
"command": "echo 'AI is writing to a file...'"
}
]
}
]
}
}
EventDescription
session_startTriggered when a new chat session begins.
session_endTriggered when a session ends.
agent_stopTriggered when the user interrupts the agent.
pre_tool_useBefore the agent executes a tool.
post_tool_useAfter tool execution completes.
pre_file_readBefore reading a file.
post_file_readAfter reading a file.
pre_file_writeBefore writing to a file.
post_file_writeAfter writing to a file.
pre_commandBefore executing a shell command.
post_commandAfter executing a shell command.
pre_mcp_toolBefore executing an MCP tool.
post_mcp_toolAfter MCP tool execution completes.
pre_promptBefore submitting a prompt.

Hooks rely on the underlying editor’s support for event callbacks.

  • Claude Code: Supports all hook events. Written to .claude/settings.json.
  • Cursor: Supports pre_tool_use, post_tool_use, pre_file_read, pre_command, post_command, pre_mcp_tool, post_mcp_tool, post_file_write, pre_prompt, session_start, session_end, and agent_stop. Written to .cursor/hooks.json.
  • Windsurf: Supports pre_file_read, post_file_read, pre_file_write, post_file_write, pre_command, post_command, pre_mcp_tool, post_mcp_tool, pre_prompt, and agent_stop. Written to .windsurf/hooks.json.
  • Other Editors: VS Code, Zed, and Codex do not support hooks. aix will warn if you define hooks that aren’t supported by your target editors.