Skip to main content
The Lua Plugin System enables you to intercept and modify requests and responses in real-time using sandboxed Lua scripts. This is the foundation of the Cortex Router intelligent routing engine and enables powerful customization without modifying switchAILocal’s core code.

Overview

Plugins run in a sandboxed Lua environment with access to the switchai 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
Plugins are explicitly enabled in config.yaml. They are disabled by default for security.

Quick Start

1. Enable Plugins

Add the plugin section to your config.yaml:

2. Enable Intelligence Services (Optional)

For Phase 2 features (semantic matching, skill matching, cascading):
See Intelligent Systems for complete configuration options.

3. Restart switchAILocal

You should see:

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

  1. Cache Tier (<1ms): Semantic cache lookup
  2. Reflex Tier (<1ms): Fast pattern matching (PII, code, images)
  3. Semantic Tier (<20ms): Embedding-based intent matching
  4. Cognitive Tier (200-500ms): LLM classification with confidence
  5. Verification: Cross-validates results
  6. Cascade: Quality-based model escalation
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
Phase 2: Intelligent Path
  • Semantic Tier: Embed query and match against intent vectors (85% confidence)
  • Cognitive Tier: LLM classification with confidence scoring
  • Verification: Cross-validate low-confidence classifications
Phase 3: Quality Path
  • 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:
Each skill provides:
  • 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.execute are disabled
Plugins have access to request bodies, which may contain sensitive data. Only use trusted plugins in production.

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
The Cortex Router uses a multi-tier approach to minimize latency: Cache (<1ms) → Reflex (<1ms) → Semantic (<20ms) → Cognitive (200-500ms)

See Also