Hooks
Hooks provide an extensible event-driven system for automating actions in response to agent commands and events. Hooks are automatically discovered from directories and can be managed via CLI commands, similar to how skills work in Clawdbot.Getting Oriented
Hooks are small scripts that run when something happens. There are two kinds:- Hooks (this page): run inside the Gateway when agent events fire, like
/new,/reset,/stop, or lifecycle events. - Webhooks: external HTTP webhooks that let other systems trigger work in Clawdbot. See Webhook Hooks or use
clawdbot webhooksfor Gmail helper commands.
- Save a memory snapshot when you reset a session
- Keep an audit trail of commands for troubleshooting or compliance
- Trigger follow-up automation when a session starts or ends
- Write files into the agent workspace or call external APIs when events fire
Overview
The hooks system allows you to:- Save session context to memory when
/newis issued - Log all commands for auditing
- Trigger custom automations on agent lifecycle events
- Extend Clawdbot’s behavior without modifying core code
Getting Started
Bundled Hooks
Clawdbot ships with three bundled hooks that are automatically discovered:- 💾 session-memory: Saves session context to your agent workspace (default
~/clawd/memory/) when you issue/new - 📝 command-logger: Logs all command events to
~/.clawdbot/logs/commands.log - 😈 soul-evil: Swaps injected
SOUL.mdcontent withSOUL_EVIL.mdduring a purge window or by random chance
Onboarding
During onboarding (clawdbot onboard), you’ll be prompted to enable recommended hooks. The wizard automatically discovers eligible hooks and presents them for selection.
Hook Discovery
Hooks are automatically discovered from three directories (in order of precedence):- Workspace hooks:
<workspace>/hooks/(per-agent, highest precedence) - Managed hooks:
~/.clawdbot/hooks/(user-installed, shared across workspaces) - Bundled hooks:
<clawdbot>/dist/hooks/bundled/(shipped with Clawdbot)
Hook Packs (npm/archives)
Hook packs are standard npm packages that export one or more hooks viaclawdbot.hooks in
package.json. Install them with:
package.json:
HOOK.md and handler.ts (or index.ts).
Hook packs can ship dependencies; they will be installed under ~/.clawdbot/hooks/<id>.
Hook Structure
HOOK.md Format
TheHOOK.md file contains metadata in YAML frontmatter plus Markdown documentation:
Metadata Fields
Themetadata.clawdbot object supports:
emoji: Display emoji for CLI (e.g.,"💾")events: Array of events to listen for (e.g.,["command:new", "command:reset"])export: Named export to use (defaults to"default")homepage: Documentation URLrequires: Optional requirementsbins: Required binaries on PATH (e.g.,["git", "node"])anyBins: At least one of these binaries must be presentenv: Required environment variablesconfig: Required config paths (e.g.,["workspace.dir"])os: Required platforms (e.g.,["darwin", "linux"])
always: Bypass eligibility checks (boolean)install: Installation methods (for bundled hooks:[{"id":"bundled","kind":"bundled"}])
Handler Implementation
Thehandler.ts file exports a HookHandler function:
Event Context
Each event includes:Event Types
Command Events
Triggered when agent commands are issued:command: All command events (general listener)command:new: When/newcommand is issuedcommand:reset: When/resetcommand is issuedcommand:stop: When/stopcommand is issued
Agent Events
agent:bootstrap: Before workspace bootstrap files are injected (hooks may mutatecontext.bootstrapFiles)
Future Events
Planned event types:session:start: When a new session beginssession:end: When a session endsagent:error: When an agent encounters an errormessage:sent: When a message is sentmessage:received: When a message is received
Creating Custom Hooks
1. Choose Location
- Workspace hooks (
<workspace>/hooks/): Per-agent, highest precedence - Managed hooks (
~/.clawdbot/hooks/): Shared across workspaces
2. Create Directory Structure
3. Create HOOK.md
4. Create handler.ts
5. Enable and Test
Configuration
New Config Format (Recommended)
Per-Hook Configuration
Hooks can have custom configuration:Extra Directories
Load hooks from additional directories:Legacy Config Format (Still Supported)
The old config format still works for backwards compatibility:CLI Commands
List Hooks
Hook Information
Check Eligibility
Enable/Disable
Bundled Hooks
session-memory
Saves session context to memory when you issue/new.
Events: command:new
Requirements: workspace.dir must be configured
Output: <workspace>/memory/YYYY-MM-DD-slug.md (defaults to ~/clawd)
What it does:
- Uses the pre-reset session entry to locate the correct transcript
- Extracts the last 15 lines of conversation
- Uses LLM to generate a descriptive filename slug
- Saves session metadata to a dated memory file
2026-01-16-vendor-pitch.md2026-01-16-api-design.md2026-01-16-1430.md(fallback timestamp if slug generation fails)
command-logger
Logs all command events to a centralized audit file. Events:command
Requirements: None
Output: ~/.clawdbot/logs/commands.log
What it does:
- Captures event details (command action, timestamp, session key, sender ID, source)
- Appends to log file in JSONL format
- Runs silently in the background
soul-evil
Swaps injectedSOUL.md content with SOUL_EVIL.md during a purge window or by random chance.
Events: agent:bootstrap
Docs: SOUL Evil Hook
Output: No files written; swaps happen in-memory only.
Enable:
Best Practices
Keep Handlers Fast
Hooks run during command processing. Keep them lightweight:Handle Errors Gracefully
Always wrap risky operations:Filter Events Early
Return early if the event isn’t relevant:Use Specific Event Keys
Specify exact events in metadata when possible:Debugging
Enable Hook Logging
The gateway logs hook loading at startup:Check Discovery
List all discovered hooks:Check Registration
In your handler, log when it’s called:Verify Eligibility
Check why a hook isn’t eligible:Testing
Gateway Logs
Monitor gateway logs to see hook execution:Test Hooks Directly
Test your handlers in isolation:Architecture
Core Components
src/hooks/types.ts: Type definitionssrc/hooks/workspace.ts: Directory scanning and loadingsrc/hooks/frontmatter.ts: HOOK.md metadata parsingsrc/hooks/config.ts: Eligibility checkingsrc/hooks/hooks-status.ts: Status reportingsrc/hooks/loader.ts: Dynamic module loadersrc/cli/hooks-cli.ts: CLI commandssrc/gateway/server-startup.ts: Loads hooks at gateway startsrc/auto-reply/reply/commands-core.ts: Triggers command events
Discovery Flow
Event Flow
Troubleshooting
Hook Not Discovered
-
Check directory structure:
-
Verify HOOK.md format:
-
List all discovered hooks:
Hook Not Eligible
Check requirements:- Binaries (check PATH)
- Environment variables
- Config values
- OS compatibility
Hook Not Executing
-
Verify hook is enabled:
- Restart your gateway process so hooks reload.
-
Check gateway logs for errors:
Handler Errors
Check for TypeScript/import errors:Migration Guide
From Legacy Config to Discovery
Before:-
Create hook directory:
-
Create HOOK.md:
-
Update config:
-
Verify and restart your gateway process:
- Automatic discovery
- CLI management
- Eligibility checking
- Better documentation
- Consistent structure