Skip to main content
Droid hooks are user-defined shell commands that execute at various points in Droid’s lifecycle. Hooks provide deterministic control over Droid’s behavior, ensuring certain actions always happen rather than relying on Droid to choose to run them.
For reference documentation on hooks, see Hooks reference.
Example use cases for hooks include:
  • Notifications: Customize how you get notified when Droid is awaiting your input or permission to run something.
  • Automatic formatting: Run prettier on .ts files, gofmt on .go files, etc. after every file edit.
  • Logging: Track and count all executed commands for compliance or debugging.
  • Feedback: Provide automated feedback when Droid produces code that does not follow your codebase conventions.
  • Custom permissions: Block modifications to production files or sensitive directories.
By encoding these rules as hooks rather than prompting instructions, you turn suggestions into app-level code that executes every time it is expected to run.
You must consider the security implication of hooks as you add them, because hooks run automatically during Droid’s execution with your current environment’s credentials. For example, malicious hooks code can exfiltrate your data. Always review your hooks implementation before registering them.For full security best practices, see Security Considerations in the hooks reference documentation.
Important: Always use absolute paths when referencing scripts in your hook commands, not relative paths. Hooks execute from Droid’s current working directory, which may not be your project root. Use $FACTORY_PROJECT_DIR for project-relative scripts (e.g., "$FACTORY_PROJECT_DIR"/.factory/hooks/script.sh) or full paths for global scripts (e.g., /usr/local/bin/my-hook.sh or ~/.factory/hooks/script.sh).

Hook Events Overview

Droid provides several hook events that run at different points in the workflow:
  • PreToolUse: Runs before tool calls (can block them)
  • PostToolUse: Runs after tool calls complete
  • UserPromptSubmit: Runs when the user submits a prompt, before Droid processes it
  • Notification: Runs when Droid sends notifications
  • Stop: Runs when Droid finishes responding
  • SubagentStop: Runs when sub-droid tasks complete
  • PreCompact: Runs before Droid is about to run a compact operation
  • SessionStart: Runs when Droid starts a new session or resumes an existing session
  • SessionEnd: Runs when Droid session ends
Each event receives different data and can control Droid’s behavior in different ways.

Quickstart

In this quickstart, you’ll add a hook that logs the shell commands that Droid runs.

Prerequisites

Install jq for JSON processing in the command line.

Step 1: Open hooks configuration

Run the /hooks slash command and select the PreToolUse hook event. PreToolUse hooks run before tool calls and can block them while providing Droid feedback on what to do differently.

Step 2: Add a matcher

Select + Add new matcher… to run your hook only on Execute tool calls. Type Execute for the matcher.
You can use * to match all tools.

Step 3: Add the hook

Select + Add new hook… and enter this command:

Step 4: Save your configuration

For storage location, select User settings since you’re logging to your home directory. This hook will then apply to all projects, not just your current project. Then press Esc until you return to the REPL. Your hook is now registered!

Step 5: Verify your hook

Run /hooks again or open ~/.factory/hooks.json to see your configuration:

Step 6: Test your hook

Ask Droid to run a simple command like ls and check your log file:
You should see entries like:

More Examples

Code Formatting Hook

Automatically format TypeScript files after editing:

Markdown Formatting Hook

Automatically fix missing language tags and formatting issues in markdown files:
Create .factory/hooks/markdown_formatter.py with this content:
Make the script executable:
This hook automatically:
  • Detects programming languages in unlabeled code blocks
  • Adds appropriate language tags for syntax highlighting
  • Fixes excessive blank lines while preserving code content
  • Only processes markdown files (.md, .mdx)

Custom Notification Hook

Get desktop notifications when Droid needs input:

File Protection Hook

Block edits to sensitive files:

Learn more

  • For reference documentation on hooks, see Hooks reference.
  • For comprehensive security best practices and safety guidelines, see Security Considerations in the hooks reference documentation.
  • For troubleshooting steps and debugging techniques, see Debugging in the hooks reference documentation.