The Switchboard - Unified LLM API Gateway with fail-closed semantics
Project description
The Switchboard ๐
Unified LLM API Gateway with Fail-Closed Semantics
The Switchboard is a high-performance proxy service that provides intelligent routing to multiple LLM providers. It serves as the central nervous system for all Aperion agents (Sentinel, AR, Aether), ensuring they can access LLMs reliably and cost-effectively.
๐ฏ Core Features
- OpenAI-Compatible API: Drop-in replacement - just change
base_url - Intelligent Task Routing: Security tasks โ Premium, Docs โ Free tier
- Fail-Closed Semantics: Never silently falls back to Echo in production
- Cost Optimization: Target 75% savings by routing volume to free tiers
- Telemetry Injection: X-Correlation-ID propagation for tracing
- Structured Logging: JSON cost/latency metrics (Constitution D3)
๐ Quick Start
Installation
pip install aperion-switchboard
# From source
pip install -e .
# With dev dependencies
pip install -e ".[dev]"
Configuration
Set environment variables for your providers:
# OpenAI (Premium tier)
export OPENAI_API_KEY=sk-...
# Google Gemini (Free tier)
export GEMINI_API_KEY=AIza...
# Cloudflare Workers AI (Low-cost tier)
export WORKERS_AI_API_KEY=your-cf-token
export WORKERS_AI_BASE_URL=https://api.cloudflare.com/client/v4/accounts/ACCT/ai/run
Running
# Development
python -m aperion_switchboard.main
# Production
uvicorn aperion_switchboard.main:app --host 0.0.0.0 --port 8080
# Docker
docker build -t switchboard .
docker run -p 8080:8080 \
-e OPENAI_API_KEY=sk-... \
-e GEMINI_API_KEY=AIza... \
switchboard
๐ก API Usage
OpenAI-Compatible Endpoint
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-H "X-Aperion-Task-Type: security_audit" \
-d '{
"model": "gpt-4.1-mini",
"messages": [{"role": "user", "content": "Analyze this code for vulnerabilities"}]
}'
Task Types
Use the X-Aperion-Task-Type header to trigger intelligent routing:
| Task Type | Routes To | Use Case |
|---|---|---|
security_audit |
OpenAI | Critical security analysis |
production_decision |
OpenAI | High-stakes decisions |
strategic_analysis |
OpenAI | Complex reasoning |
code_review |
OpenAI | Quality reviews |
doc_update |
Gemini | Documentation updates |
doc_generation |
Gemini | Batch doc creation |
lint_analysis |
Gemini | Fast batch processing |
test_generation |
Gemini | High-volume generation |
general |
Gemini | Default (cost-optimized) |
๐ Constitution Compliance
A6: Fail-Closed Semantics (Iron Rule)
The Switchboard MUST NEVER silently fall back to the Echo provider in production.
- If no real providers are configured AND
APERION_ALLOW_ECHOis not "true":- Service crashes on startup
- Returns 503 for all requests
- Logs CRITICAL error with remediation steps
# Production mode (default) - will crash if no providers configured
export APERION_ALLOW_ECHO=false
# Development mode - allows echo fallback
export APERION_ALLOW_ECHO=true
B1: Secrets Management
All credentials are loaded from environment variables:
OPENAI_API_KEYGEMINI_API_KEYWORKERS_AI_API_KEYSWITCHBOARD_API_KEY(optional - for Switchboard auth)
D1: Telemetry Injection
- Extracts
X-Correlation-IDfrom incoming requests - Generates one if missing (format:
sw_{uuid}) - Propagates to all upstream provider requests
- Adds to response headers
D3: Structured Logging
All cost/latency metrics are logged as JSON:
{
"event": "llm_request_cost",
"correlation_id": "sw_abc123",
"provider": "openai",
"model": "gpt-4.1-mini",
"estimated_cost_usd": 0.00015,
"tokens": {"prompt": 100, "completion": 50, "total": 150},
"latency_ms": 1234,
"task_type": "security_audit"
}
๐งช Testing
# Run all tests
pytest
# Run safety tests (fail-closed verification)
pytest -m safety
# Run unit tests only
pytest -m unit
# Run integration tests (requires API keys)
pytest -m integration
# With coverage
pytest --cov=aperion_switchboard --cov-report=html
๐ Endpoints
| Endpoint | Method | Description |
|---|---|---|
/v1/chat/completions |
POST | OpenAI-compatible chat |
/health |
GET | Health check |
/healthz |
GET | Kubernetes health probe |
/docs |
GET | OpenAPI documentation |
๐๏ธ Architecture
src/aperion_switchboard/
โโโ core/
โ โโโ router.py # Task routing & fallback logic
โ โโโ protocol.py # LLMClient abstract base class
โ โโโ fail_closed.py # Constitution A6 enforcement
โโโ providers/
โ โโโ openai.py # OpenAI/compatible providers
โ โโโ gemini.py # Google Gemini
โ โโโ workers.py # Cloudflare Workers AI
โ โโโ echo.py # Test-only echo provider
โโโ service/
โ โโโ app.py # FastAPI application
โ โโโ middleware.py # Auth, telemetry, cost logging
โ โโโ schemas.py # OpenAI-compatible Pydantic models
โโโ main.py # Entry point
๐ Cost Optimization
The Switchboard achieves ~75% cost savings by:
- Routing 80% of requests (docs, linting, tests) to free tiers
- Reserving premium providers for critical tasks only
- Tracking and reporting cost per request
View cost summary:
from aperion_switchboard.core.router import get_router
router = get_router()
summary = router.get_cost_summary()
print(f"Savings: {summary['savings_percent']:.1f}%")
๐ง Development
# Install dev dependencies
pip install -e ".[dev]"
# Run linter
ruff check src tests
# Run type checker
mypy src
# Run tests with coverage
pytest --cov=aperion_switchboard --cov-report=term-missing
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aperion_switchboard-1.2.1.tar.gz.
File metadata
- Download URL: aperion_switchboard-1.2.1.tar.gz
- Upload date:
- Size: 72.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b26e962b891e2a0de008c34b0bfd4b479d92bce46f6f2d999095d14b010f7660
|
|
| MD5 |
79163b691970c72869df476835ca88c2
|
|
| BLAKE2b-256 |
27b4ed152663e35b003d829cd976b2118cb9dfe41f6121799611efe210a92b19
|
Provenance
The following attestation bundles were made for aperion_switchboard-1.2.1.tar.gz:
Publisher:
release.yml on invictustitan2/aperion-llm-router
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aperion_switchboard-1.2.1.tar.gz -
Subject digest:
b26e962b891e2a0de008c34b0bfd4b479d92bce46f6f2d999095d14b010f7660 - Sigstore transparency entry: 941975649
- Sigstore integration time:
-
Permalink:
invictustitan2/aperion-llm-router@66190974bed3ebe3dd1cccb41357f7a6c25d7675 -
Branch / Tag:
refs/tags/v1.2.1 - Owner: https://github.com/invictustitan2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@66190974bed3ebe3dd1cccb41357f7a6c25d7675 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aperion_switchboard-1.2.1-py3-none-any.whl.
File metadata
- Download URL: aperion_switchboard-1.2.1-py3-none-any.whl
- Upload date:
- Size: 47.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ec31a1323e65150552dbda35b50a28387d2b99f1e88ae04a78d8ef451521db5
|
|
| MD5 |
ae96b73e44c5764d0f2f4529b2b99aa2
|
|
| BLAKE2b-256 |
526b1517d2355cc038e1ad67451d627f8e296b163a9afcde85cabcdee0dda630
|
Provenance
The following attestation bundles were made for aperion_switchboard-1.2.1-py3-none-any.whl:
Publisher:
release.yml on invictustitan2/aperion-llm-router
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aperion_switchboard-1.2.1-py3-none-any.whl -
Subject digest:
7ec31a1323e65150552dbda35b50a28387d2b99f1e88ae04a78d8ef451521db5 - Sigstore transparency entry: 941975687
- Sigstore integration time:
-
Permalink:
invictustitan2/aperion-llm-router@66190974bed3ebe3dd1cccb41357f7a6c25d7675 -
Branch / Tag:
refs/tags/v1.2.1 - Owner: https://github.com/invictustitan2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@66190974bed3ebe3dd1cccb41357f7a6c25d7675 -
Trigger Event:
push
-
Statement type: