Serverless AI Gateway and Smart Router for Corvo Labs applications.
Corvo Cortex is a serverless AI Gateway built on Cloudflare Workers that decouples frontend applications from specific LLM providers. It provides:
- Smart Provider Routing - Intelligently routes to prioritize free credits (OpenAI, Anthropic, Z.ai)
- Credit-Aware Fallback - Automatically retries via OpenRouter when direct provider credits are exhausted
- Authentication - App-specific API keys stored in Cloudflare KV
- Rate Limiting Middleware - Available in codebase, currently disabled on request-serving routes
- Circuit Breaker - Prevents cascading failures with auto-recovery
- Streaming Support - Real-time SSE streaming for all providers
- Telemetry - LangFuse integration for cost tracking and analytics
- Retry Logic - Exponential backoff for transient failures
| Provider | Models | Notes |
|---|---|---|
| Anthropic | Claude 3.5 Sonnet, Haiku, Opus | Direct API |
| OpenAI | GPT-4o, GPT-4o-mini | Direct API |
| Z.ai | GLM-4 Plus | Direct API |
| MiniMax | MiniMax-M2 | Direct API |
| OpenRouter | 100+ models | Fallback/paid |
Comprehensive documentation is available in the /docs directory:
| Document | Description |
|---|---|
| spec.md | Full API specification, data models, request flow |
| changelog.md | Release history and change log |
| project-status.md | Current status, known issues |
| Feature | Documentation |
|---|---|
| Smart Routing | provider-routing.md |
| Authentication | authentication.md |
| Rate Limiting | rate-limiting.md |
| Circuit Breaker | circuit-breaker.md |
| Streaming | streaming.md |
| Telemetry | telemetry.md |
- Node.js 18+
- Cloudflare account with Workers subscription
- API keys for LLM providers
- LangFuse account (for telemetry)
# Clone the repository
git clone https://github.com/corvolabs/corvo-cortex.git
cd corvo-cortex
# Install dependencies
npm install# Start development server
npm run devThe API will be available at http://localhost:8787.
All requests require an API key in the Authorization header:
Authorization: Bearer sk-corvo-<app_name>-<random_string>Returns available models and defaults:
curl -H "Authorization: Bearer sk-corvo-kinisi-xxx" \
https://cortex.corvolabs.com/v1/modelsResponse:
{
"object": "list",
"data": [
{ "id": "gpt-4o", "provider": "openai", "name": "GPT-4o (Reasoning)" },
{ "id": "claude-3-5-sonnet", "provider": "anthropic", "name": "Claude 3.5 Sonnet (Coding)" },
{ "id": "glm-4-plus", "provider": "z-ai", "name": "GLM-4 (Creative)" }
],
"defaults": {
"system_default": "gpt-4o",
"client_default": "claude-3-5-sonnet"
}
}OpenAI-compatible chat completions:
curl -X POST \
-H "Authorization: Bearer sk-corvo-kinisi-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-sonnet",
"messages": [{"role": "user", "content": "Hello!"}]
}' \
https://cortex.corvolabs.com/v1/chat/completionsWith streaming:
{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Tell me a joke"}],
"stream": true
}-
Authenticate with Cloudflare
wrangler login
-
Create KV Namespaces
./scripts/create-kv-namespaces.sh
Update the namespace IDs in
wrangler.tomlwith the returned values. -
Set Production Secrets
./scripts/setup-secrets.sh
-
Seed Initial Data
./scripts/seed-data.sh
-
Deploy
npm run deploy
If you prefer manual setup:
# Create KV namespaces
wrangler kv namespace create CORTEX_CLIENTS --env production
wrangler kv namespace create CORTEX_CONFIG --env production
# Set secrets
wrangler secret put ANTHROPIC_API_KEY --env production
wrangler secret put OPENAI_API_KEY --env production
wrangler secret put ZAI_API_KEY --env production
wrangler secret put OPENROUTER_API_KEY --env production
wrangler secret put OPENROUTER_PROVISIONING_API_KEY --env production
wrangler secret put LANGFUSE_PUBLIC_KEY --env production
wrangler secret put LANGFUSE_SECRET_KEY --env production
# Deploy
wrangler deploy --env productionwrangler kv key put --namespace-id=<CLIENTS_NAMESPACE_ID> "sk-corvo-myapp-xxx" '{
"appId": "myapp",
"name": "My Application",
"defaultModel": "gpt-4o",
"allowZai": true,
"fallbackStrategy": "openrouter",
"rateLimit": {
"requestsPerMinute": 100,
"tokensPerMinute": 50000
}
}' --env production --remoteFor CLI reads/writes against Cloudflare (dashboard data), include --remote.
Client App
↓
Auth Layer (KV validation)
↓
Schema Validation (Zod)
↓
Smart Routing
├── Z.ai (glm-* models)
├── Anthropic (claude-* + credits)
├── OpenAI (gpt-* + credits)
└── OpenRouter (fallback)
↓
Retry Logic (exponential backoff)
↓
Circuit Breaker (Durable Objects)
↓
Telemetry (LangFuse)
↓
Response (Streaming or JSON)
| State | Description |
|---|---|
| Closed | Normal operation |
| Open | Failing - fast fail for 60s |
| Half-Open | Testing recovery |
interface ClientConfig {
appId: string; // Unique app identifier
name: string; // Display name
defaultModel: string; // Default model for requests
allowZai: boolean; // Allow Z.ai Pro routing
fallbackStrategy: 'fail-fast' | 'openrouter';
rateLimit: {
requestsPerMinute: number;
tokensPerMinute: number;
};
}| Variable | Description |
|---|---|
ENVIRONMENT |
development or production |
CREDITS_ANTHROPIC |
Set to "true" if Anthropic credits available |
CREDITS_OPENAI |
Set to "true" if OpenAI credits available |
# Run all tests
npm test
# Unit tests only
npm run test:unit
# Integration tests only
npm run test:integration
# Coverage report
npm run test:coverageRun all analysis tools:
npm run analyze- Linting:
npm run lintornpm run lint:fix - Type Checking:
npm run type-check - Circular Dependencies:
npm run complexity - Security Audit:
npm run audit - CodeRabbit Review:
coderabbit --prompt-only
Analysis reports are generated in the reports/ directory (auto-generated, not in git).
# View ESLint HTML report
open reports/eslint-report.htmlAccess https://us.cloud.langfuse.com to monitor:
- Request volume per app
- Error rates by provider
- Latency metrics
- Cost tracking per application
Access Cloudflare Dashboard for:
- Worker request metrics
- Error logs
- KV storage usage
| Error | Cause | Solution |
|---|---|---|
| 401 Unauthorized | Invalid API key | Verify KV contains client config |
| 503 Unavailable | Circuit breaker open | Check /health/providers endpoint |
| 402 Payment Required | Credits exhausted | Add credits or change fallback strategy |
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
npm test - Update documentation (run
/update-docsworkflow) - Submit a pull request
We maintain documentation with every commit. Before committing:
# Install pre-commit hook
cp scripts/pre-commit-docs.sh .git/hooks/pre-commit
chmod +x .git/hooks/pre-commitOr manually run the /update-docs workflow.
MIT License - see LICENSE file for details.
- Engineering: eng@corvolabs.workers.dev
- Documentation: docs.corvolabs.workers.dev
- Issues: GitHub Issues
Corvo Cortex v2.2 - Built with Cloudflare Workers + Hono