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. Example use cases for hooks include:Documentation Index
Fetch the complete documentation index at: https://factory-docs-academy-content-candidates.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
- Notifications: Customize how you get notified when Droid is awaiting your input or permission to run something.
- Automatic formatting: Run
prettieron .ts files,gofmton .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.
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
Quickstart
In this quickstart, you’ll add a hook that logs the shell commands that Droid runs.Prerequisites
Installjq 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, selectUser 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 check ~/.factory/settings.json to see your configuration:
Step 6: Test your hook
Ask Droid to run a simple command likels and check your log file:
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:.factory/hooks/markdown_formatter.py with this content:
- 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.
