Overview
Hooks commands manage event-driven automation that triggers actions based on switchAILocal events like request completion, provider failures, quota warnings, and health changes.Commands
list
Display all configured hooks.Hooks are loaded from
~/.switchailocal/hooks/*.yaml files.enable
Enable a disabled hook.Hook ID to enable
disable
Disable an active hook without deleting it.test
Test a hook with sample event data.Hook ID to test
Event type to simulate (e.g.,
request_completed, provider_unhealthy)JSON data payload for the event (default:
{})logs
View recent hook execution logs.Filter logs for specific hook ID
Number of log entries to show (default: 50)
Output format:
table or json (default: table)reload
Reload all hooks from disk without restarting the server.Hook Configuration Format
Hooks are defined in YAML files:~/.switchailocal/hooks/alerts.yaml
Hook Fields
Unique identifier for the hook
Hook type:
webhook, file, or scriptWhether the hook is active (default:
true)List of events that trigger this hook
Optional conditions for hook execution
Hook-type specific configuration
Supported Events
Request Events
request_received- New request receivedrequest_completed- Request successfully completedrequest_failed- Request failed with errorrequest_timeout- Request exceeded timeout
Provider Events
provider_healthy- Provider became healthyprovider_unhealthy- Provider became unhealthyprovider_degraded- Provider experiencing high latencyprovider_recovered- Provider recovered from unhealthy state
Quota Events
quota_threshold- Quota usage exceeded thresholdquota_exceeded- Quota fully exhaustedquota_reset- Quota period reset
Routing Events
routing_decision- Cortex Router made a routing decisionsteering_applied- Steering rule was appliedfallback_triggered- Failover to backup provider
Hook Types
Webhook Hooks
Send HTTP requests to external services:File Hooks
Write events to log files:Script Hooks
Execute custom scripts:Template Variables
Hooks support Go template syntax with these variables:{{.event}}- Event name{{.timestamp}}- ISO 8601 timestamp{{.provider}}- Provider name (provider events){{.model}}- Model name (request events){{.status}}- Status code or state{{.reason}}- Error or status reason{{.user}}- API key hash{{.latency}}- Request latency in ms{{.quota}}- Quota usage percentage
Use Cases
Slack Alerts
Get notified when providers go down or quotas are exceeded
Request Logging
Log all requests for audit and compliance
PagerDuty Integration
Create incidents for critical failures
Metrics Collection
Send metrics to Datadog, Prometheus, or Grafana
Best Practices
Rate limiting - Hooks are automatically rate-limited to 100 executions per minute per hook.
Troubleshooting
Webhook not triggering
Webhook not triggering
- Check if hook is enabled:
hooks list - Verify event name matches supported events
- Test webhook URL manually with curl
- Check hook logs:
hooks logs --id <hook-id>
Template rendering errors
Template rendering errors
- Verify template syntax with
hooks test - Ensure all variables are available for the event type
- Check for typos in variable names (case-sensitive)
Script hook timing out
Script hook timing out
- Increase timeout in config
- Ensure script has execute permissions
- Run script manually to verify it completes
- Check script logs for errors
See Also
- Heartbeat Commands - Monitor provider health
- Steering Commands - Configure routing rules
- Memory Commands - Analyze routing history