Skip to main content

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.
Output:
Hooks are loaded from ~/.switchailocal/hooks/*.yaml files.

enable

Enable a disabled hook.
string
required
Hook ID to enable
Example:
Output:

disable

Disable an active hook without deleting it.
Example:

test

Test a hook with sample event data.
string
required
Hook ID to test
string
required
Event type to simulate (e.g., request_completed, provider_unhealthy)
string
JSON data payload for the event (default: {})
Example:
Output:

logs

View recent hook execution logs.
string
Filter logs for specific hook ID
integer
Number of log entries to show (default: 50)
string
Output format: table or json (default: table)
Example:
Output:

reload

Reload all hooks from disk without restarting the server.
Output:

Hook Configuration Format

Hooks are defined in YAML files:
~/.switchailocal/hooks/alerts.yaml

Hook Fields

string
required
Unique identifier for the hook
string
required
Hook type: webhook, file, or script
boolean
Whether the hook is active (default: true)
array
required
List of events that trigger this hook
array
Optional conditions for hook execution
object
required
Hook-type specific configuration

Supported Events

Request Events

  • request_received - New request received
  • request_completed - Request successfully completed
  • request_failed - Request failed with error
  • request_timeout - Request exceeded timeout

Provider Events

  • provider_healthy - Provider became healthy
  • provider_unhealthy - Provider became unhealthy
  • provider_degraded - Provider experiencing high latency
  • provider_recovered - Provider recovered from unhealthy state

Quota Events

  • quota_threshold - Quota usage exceeded threshold
  • quota_exceeded - Quota fully exhausted
  • quota_reset - Quota period reset

Routing Events

  • routing_decision - Cortex Router made a routing decision
  • steering_applied - Steering rule was applied
  • fallback_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

Use webhook retries - Configure retry logic for critical webhooks to handle temporary network failures.
Avoid infinite loops - Don’t trigger hooks that make requests to switchAILocal itself.
Rate limiting - Hooks are automatically rate-limited to 100 executions per minute per hook.

Troubleshooting

  • 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>
  • Verify template syntax with hooks test
  • Ensure all variables are available for the event type
  • Check for typos in variable names (case-sensitive)
  • Increase timeout in config
  • Ensure script has execute permissions
  • Run script manually to verify it completes
  • Check script logs for errors

See Also