-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Hooks System
The Claude-Flow Hooks System provides automated workflow orchestration through pre and post operation hooks, enabling seamless integration of custom logic into the development lifecycle. This powerful system allows for automatic task tracking, memory persistence, agent coordination, and performance optimization.
The Hooks System intercepts key operations in your workflow, executing custom logic before and after critical actions. This enables:
- Automated Task Management: Track all operations with unique IDs
- Memory Persistence: Store context and results for future reference
- Agent Coordination: Synchronize multi-agent workflows
- Performance Monitoring: Track execution times and resource usage
- Custom Integrations: Add your own automation logic
Executes before starting any task, initializing tracking and context.
npx claude-flow@alpha hooks pre-task --description "Implement user authentication"
Parameters:
-
--description
: Task description (required) -
--priority
: Task priority (low/medium/high/critical) -
--metadata
: Additional JSON metadata
Example Output:
🔄 Executing pre-task hook...
📋 Task: Implement user authentication
🆔 Task ID: task-1753483207250-u7wbmsetj
💾 Saved to .swarm/memory.db
Executes after task completion, storing results and metrics.
npx claude-flow@alpha hooks post-task --task-id "task-1753483207250-u7wbmsetj" --status "completed"
Parameters:
-
--task-id
: Task identifier (required) -
--status
: Task status (completed/failed/partial) -
--results
: JSON results data -
--metrics
: Performance metrics
Executes before file modifications, creating backups and tracking changes.
npx claude-flow@alpha hooks pre-edit --file "src/auth.js" --operation "update authentication logic"
Parameters:
-
--file
: File path (required) -
--operation
: Operation description -
--backup
: Create backup (true/false)
Executes after file modifications, validating changes and updating memory.
npx claude-flow@alpha hooks post-edit --file "src/auth.js" --memory-key "auth/implementation"
Parameters:
-
--file
: File path (required) -
--memory-key
: Memory storage key -
--validate
: Run validation (true/false) -
--sync-agents
: Notify other agents
Initializes a new development session with context restoration.
npx claude-flow@alpha hooks session-start --restore-context --load-agents
Parameters:
-
--restore-context
: Restore previous session context -
--load-agents
: Initialize configured agents -
--workspace
: Set workspace directory
Finalizes session, saving state and generating reports.
npx claude-flow@alpha hooks session-end --save-state --generate-report
Parameters:
-
--save-state
: Save current session state -
--generate-report
: Create session summary -
--cleanup
: Remove temporary files
Executes when creating new agents, configuring their environment.
npx claude-flow@alpha hooks agent-spawn --type "coder" --config '{"language":"typescript"}'
Parameters:
-
--type
: Agent type (required) -
--config
: Agent configuration JSON -
--parent-task
: Parent task ID
Executes when agents finish their tasks, collecting results.
npx claude-flow@alpha hooks agent-complete --agent-id "agent-123" --merge-results
Parameters:
-
--agent-id
: Agent identifier (required) -
--merge-results
: Merge with parent task -
--propagate
: Send results to other agents
Begins performance monitoring for an operation.
npx claude-flow@alpha hooks perf-start --operation "database-query" --track-memory
Parameters:
-
--operation
: Operation name (required) -
--track-memory
: Monitor memory usage -
--track-cpu
: Monitor CPU usage
Completes performance monitoring and stores metrics.
npx claude-flow@alpha hooks perf-end --operation "database-query" --alert-threshold 1000
Parameters:
-
--operation
: Operation name (required) -
--alert-threshold
: Alert if ms exceeds threshold -
--store-metrics
: Save to metrics database
Configure hooks in .claude/settings.json
:
{
"hooks": {
"enabled": true,
"autoExecute": {
"preTask": true,
"postTask": true,
"preEdit": true,
"postEdit": true,
"sessionStart": true,
"sessionEnd": true
},
"customHooks": {
"beforeCommit": {
"command": "npm test && npm run lint",
"failOnError": true
},
"afterDeploy": {
"command": "npx claude-flow@alpha notify --channel deployment",
"async": true
}
},
"memory": {
"persistence": true,
"location": ".swarm/memory.db",
"syncInterval": 30000
},
"performance": {
"tracking": true,
"alertThresholds": {
"task": 300000,
"edit": 5000,
"agent": 60000
}
}
}
}
Automatically run tests and validation on file changes:
{
"hooks": {
"customHooks": {
"postEdit": {
"pattern": "*.test.js",
"command": "npm test -- --findRelatedTests ${file}",
"continueOnError": false
}
}
}
}
Synchronize work across multiple agents:
# Master coordinator
npx claude-flow@alpha hooks pre-task --description "Refactor authentication system" --metadata '{"agents":["architect","coder","tester"]}'
# Each agent registers
npx claude-flow@alpha hooks agent-spawn --type "architect" --parent-task "task-123"
npx claude-flow@alpha hooks agent-spawn --type "coder" --parent-task "task-123"
npx claude-flow@alpha hooks agent-spawn --type "tester" --parent-task "task-123"
# Coordination through hooks
npx claude-flow@alpha hooks post-edit --file "architecture.md" --sync-agents --memory-key "refactor/architecture"
Track and optimize critical operations:
// Before operation
await executeHook('perf-start', { operation: 'data-processing' });
// Your operation
const results = await processLargeDataset();
// After operation
await executeHook('perf-end', {
operation: 'data-processing',
alertThreshold: 5000,
storeMetrics: true
});
Maintain context across sessions:
# Start session with context
npx claude-flow@alpha hooks session-start --restore-context
# Work with automatic memory updates
npx claude-flow@alpha hooks post-edit --file "feature.js" --memory-key "feature/implementation"
# Query memory during development
npx claude-flow@alpha memory search --pattern "feature/*"
# End session with state preservation
npx claude-flow@alpha hooks session-end --save-state
- Parallel Execution: Enable concurrent hook execution
{
"hooks": {
"parallel": true,
"maxConcurrent": 4
}
}
- Selective Hooks: Only run necessary hooks
{
"hooks": {
"filters": {
"preEdit": {
"include": ["src/**/*.js"],
"exclude": ["**/*.test.js"]
}
}
}
}
- Async Operations: Non-blocking hook execution
{
"hooks": {
"customHooks": {
"backgroundSync": {
"command": "npx claude-flow@alpha sync --remote",
"async": true,
"timeout": 30000
}
}
}
}
Hooks automatically collect performance metrics:
# View hook performance
npx claude-flow@alpha hooks metrics --last 24h
# Output:
┌─────────────┬──────────┬─────────┬─────────┐
│ Hook │ Calls │ Avg (ms)│ Max (ms)│
├─────────────┼──────────┼─────────┼─────────┤
│ pre-task │ 156 │ 23 │ 145 │
│ post-task │ 156 │ 67 │ 234 │
│ pre-edit │ 423 │ 12 │ 89 │
│ post-edit │ 423 │ 45 │ 187 │
└─────────────┴──────────┴─────────┴─────────┘
Hooks provide seamless integration with the agent system:
// Agent creation with hooks
const agent = await spawnAgent({
type: 'coder',
hooks: {
onStart: 'agent-start',
onComplete: 'agent-complete',
onError: 'agent-error'
}
});
// Automatic hook execution during agent operations
agent.on('taskComplete', async (result) => {
await executeHook('post-task', {
taskId: result.taskId,
agentId: agent.id,
results: result.data
});
});
# Agent A completes analysis
npx claude-flow@alpha hooks post-task --task-id "analysis-123" --sync-agents --broadcast "analysis-complete"
# Agent B receives notification through hook
# Automatically triggered: pre-task hook with context from Agent A
{
"hooks": {
"swarmHooks": {
"onSwarmInit": {
"command": "npx claude-flow@alpha swarm prepare --topology hierarchical"
},
"onConsensus": {
"command": "npx claude-flow@alpha memory store --key 'consensus/${timestamp}'"
},
"onSwarmComplete": {
"command": "npx claude-flow@alpha report generate --type swarm-summary"
}
}
}
}
- Always use pre/post pairs: Ensure operations are properly tracked
- Store context in memory: Use memory keys for cross-session persistence
- Monitor performance: Set appropriate thresholds for alerts
-
Handle errors gracefully: Use
continueOnError
for non-critical hooks - Document custom hooks: Maintain clear documentation for team members
- Test hook configurations: Validate hooks work as expected
- Use appropriate timeouts: Prevent hooks from blocking operations
- Leverage parallel execution: Optimize for concurrent operations
-
Hook not executing
- Check if hooks are enabled in settings
- Verify hook pattern matches
- Ensure claude-flow@alpha is installed
-
Performance degradation
- Review hook execution metrics
- Consider async execution
- Optimize hook commands
-
Memory synchronization issues
- Check database connectivity
- Verify memory keys are unique
- Review sync intervals
Enable detailed hook logging:
# Set debug environment variable
export CLAUDE_FLOW_DEBUG=hooks
# Or in settings.json
{
"hooks": {
"debug": true,
"logLevel": "verbose"
}
}
Create custom hooks in .claude/hooks/
:
// .claude/hooks/security-scan.js
module.exports = async function securityScan({ file, operation }) {
const results = await runSecurityAnalysis(file);
if (results.vulnerabilities.length > 0) {
await executeHook('alert', {
level: 'critical',
message: `Security vulnerabilities found in ${file}`,
details: results
});
}
return results;
};
Register in settings:
{
"hooks": {
"customHooks": {
"securityScan": {
"handler": ".claude/hooks/security-scan.js",
"triggers": ["post-edit"],
"filePattern": "**/*.js"
}
}
}
}
Create complex workflows by chaining hooks:
{
"hooks": {
"chains": {
"deploymentPipeline": [
{ "hook": "pre-deploy", "params": { "environment": "staging" } },
{ "hook": "run-tests", "params": { "suite": "integration" } },
{ "hook": "security-scan", "params": { "deep": true } },
{ "hook": "deploy", "params": { "strategy": "blue-green" } },
{ "hook": "post-deploy", "params": { "notify": true } }
]
}
}
}
Execute chain:
npx claude-flow@alpha hooks chain --name deploymentPipeline
The Hooks System is a cornerstone of Claude-Flow's automation capabilities, providing the foundation for sophisticated workflow orchestration. By leveraging hooks effectively, you can create self-documenting, self-optimizing development workflows that scale with your project's complexity.