Hooks
Hooks are small scripts that run when something happens inside the Gateway. They are automatically discovered from directories and can be inspected withvelaclaw hooks.
There are two kinds of hooks in Velaclaw:
- Internal hooks (this page): run inside the Gateway when agent events fire, like
/new,/reset,/stop, or lifecycle events. - Webhooks: external HTTP endpoints that let other systems trigger work in Velaclaw. See Webhooks.
velaclaw hooks list shows both standalone hooks and plugin-managed hooks.
Quick start
Event types
Writing hooks
Hook structure
Each hook is a directory containing two files:HOOK.md format
metadata.velaclaw):
Handler implementation
type, action, sessionKey, timestamp, messages (push to send to user), and context (event-specific data).
Event context highlights
Command events (command:new, command:reset): context.sessionEntry, context.previousSessionEntry, context.commandSource, context.workspaceDir, context.cfg.
Message events (message:received): context.from, context.content, context.channelId, context.metadata (provider-specific data including senderId, senderName, guildId).
Message events (message:sent): context.to, context.content, context.success, context.channelId.
Message events (message:transcribed): context.transcript, context.from, context.channelId, context.mediaPath.
Message events (message:preprocessed): context.bodyForAgent (final enriched body), context.from, context.channelId.
Bootstrap events (agent:bootstrap): context.bootstrapFiles (mutable array), context.agentId.
Session patch events (session:patch): context.sessionEntry, context.patch (only changed fields), context.cfg. Only privileged clients can trigger patch events.
Compaction events: session:compact:before includes messageCount, tokenCount. session:compact:after adds compactedCount, summaryLength, tokensBefore, tokensAfter.
Hook discovery
Hooks are discovered from these directories, in order of increasing override precedence:- Bundled hooks: shipped with Velaclaw
- Plugin hooks: hooks bundled inside installed plugins
- Managed hooks:
~/.velaclaw/hooks/(user-installed, shared across workspaces). Extra directories fromhooks.internal.load.extraDirsshare this precedence. - Workspace hooks:
<workspace>/hooks/(per-agent, disabled by default until explicitly enabled)
Hook packs
Hook packs are npm packages that export hooks viavelaclaw.hooks in package.json. Install with:
Bundled hooks
Enable any bundled hook:
session-memory details
Extracts the last 15 user/assistant messages, generates a descriptive filename slug via LLM, and saves to<workspace>/memory/YYYY-MM-DD-slug.md. Requires workspace.dir to be configured.
bootstrap-extra-files config
AGENTS.md, SOUL.md, TOOLS.md, IDENTITY.md, USER.md, HEARTBEAT.md, BOOTSTRAP.md, MEMORY.md).
command-logger details
Logs every slash command to~/.velaclaw/logs/commands.log.
boot-md details
RunsBOOT.md from the active workspace when the gateway starts.
Plugin hooks
Plugins can register hooks through the Plugin SDK for deeper integration: intercepting tool calls, modifying prompts, controlling message flow, and more. The Plugin SDK exposes 28 hooks covering model resolution, agent lifecycle, message flow, tool execution, subagent coordination, and gateway lifecycle. For the complete plugin hook reference includingbefore_tool_call, before_agent_reply, before_install, and all other plugin hooks, see Plugin Architecture.
Configuration
The legacy
hooks.internal.handlers array config format is still supported for backwards compatibility, but new hooks should use the discovery-based system.CLI reference
Best practices
- Keep handlers fast. Hooks run during command processing. Fire-and-forget heavy work with
void processInBackground(event). - Handle errors gracefully. Wrap risky operations in try/catch; do not throw so other handlers can run.
- Filter events early. Return immediately if the event type/action is not relevant.
- Use specific event keys. Prefer
"events": ["command:new"]over"events": ["command"]to reduce overhead.
Troubleshooting
Hook not discovered
Hook not eligible
Hook not executing
- Verify the hook is enabled:
velaclaw hooks list - Restart your gateway process so hooks reload.
- Check gateway logs:
./scripts/clawlog.sh | grep hook
Related
- CLI Reference: hooks
- Webhooks
- Plugin Architecture — full plugin hook reference
- Configuration