Overview
Plugins run in a sandboxed Lua environment with access to theswitchai host API for logging, caching, LLM classification, and intelligent routing features.
Key Capabilities
- Request/Response Interception: Modify requests before they reach providers
- Intelligent Routing: Route requests to optimal models based on content analysis
- Skill-Based Augmentation: Enhance prompts with domain-specific expertise
- Multi-Tier Routing: Reflex → Semantic → Cognitive routing with verification
- Semantic Caching: Sub-millisecond routing for repeated queries
Quick Start
1. Enable Plugins
Add theplugin section to your config.yaml:
2. Enable Intelligence Services (Optional)
For Phase 2 features (semantic matching, skill matching, cascading):3. Restart switchAILocal
Plugin Structure
Plugins are folder-based with a standardized structure:schema.lua (Metadata)
Defines the plugin’s identity:handler.lua (Logic)
Implements the plugin hooks:The Cortex Router Plugin
The cortex-router plugin implements intelligent multi-tier routing with 21 pre-built skills.Routing Tiers
- Cache Tier (<1ms): Semantic cache lookup
- Reflex Tier (<1ms): Fast pattern matching (PII, code, images)
- Semantic Tier (<20ms): Embedding-based intent matching
- Cognitive Tier (200-500ms): LLM classification with confidence
- Verification: Cross-validates results
- Cascade: Quality-based model escalation
How Cortex Router Works
How Cortex Router Works
Phase 1: Fast Path
- Semantic Cache: Check for similar previous queries (95% similarity threshold)
- Reflex Tier: Pattern-match for PII, code blocks, images, language detection
- Semantic Tier: Embed query and match against intent vectors (85% confidence)
- Cognitive Tier: LLM classification with confidence scoring
- Verification: Cross-validate low-confidence classifications
- Cascade: Evaluate response quality and escalate to higher-tier model if needed
- Feedback: Record outcomes for continuous learning
Pre-Built Skills
Cortex Router includes 21 domain-specific skills:- Intent patterns for semantic matching
- System prompts for domain-specific augmentation
- Model preferences for optimal routing
The switchai Host API
Plugins access host functionality through the switchai bridge:
Core Functions (Phase 1)
Intelligence Functions (Phase 2)
Creating Custom Plugins
1. Create Plugin Directory
2. Create schema.lua
3. Create handler.lua
4. Enable in config.yaml
5. Test
Advanced Examples
Content-Based Routing
User-Based Model Selection
Response Caching
Load Balancing
Security & Isolation
Plugins run in a sandboxed Lua environment with restricted capabilities:- Sandboxed Execution: Plugins run in a restricted Lua VM
- No Direct I/O: Cannot access network or filesystem directly
- Allowlisted Commands: Only safe commands available via
switchai.exec() - Timeout Protection: Execution bound by request context timeout
- No Dangerous Globals:
dofile,loadfile,os.executeare disabled
Debugging
Enable Debug Logging
View Plugin Logs
Test Plugin Logic
Create a test script:Performance Considerations
- Keep plugins fast: Each plugin adds latency to every request
- Cache expensive operations: Use
switchai.set_cache()for repeated computations - Avoid blocking calls: Never use sleep or long-running operations
- Use Reflex Tier for patterns: Pattern matching is faster than LLM classification
- Enable Semantic Cache: Bypass classification for repeated queries
See Also
- Intelligent Systems - Cortex Router configuration
- Payload Injection - Static parameter injection
- API Reference - HTTP API documentation