A production-grade Multi-LLM Reinforcement Learning Router that serves as the brain of an agentic infrastructure. Routes AI inference requests to optimal providers using Q-Learning, with circuit breaker safety, multi-tenant isolation, and a CRT-styled control dashboard.
User Request
|
v
+----+----+ +-------------+ +------------------+
| Chat | --> | Router | --> | Provider Registry |
| API | | Engine | | (Groq/OpenAI/AWS) |
+---------+ +------+------+ +------------------+
|
+------------+------------+
| |
+------v------+ +-------v--------+
| Q-Table RL | | Circuit Breaker |
| (Epsilon- | | (Factory Safety) |
| Greedy) | +----------------+
+------+------+
|
+------v------+ +----------------+
| Telemetry | -----> | Redis Streams |
| Fire/Forget| | (Tenant Isolated|
+-------------+ +--------+-------+
|
+-----------------+-----------------+
| |
+-------v-------+ +-------v--------+
| Epoch Worker | | Global Brain |
| (Mini-Batch | | (Anti-Whale |
| Q-Learning) | | Aggregation) |
+---------------+ +----------------+
- Node.js 18+
- API keys for LLM providers (Groq, OpenAI recommended)
- Upstash Redis, Turso, QStash accounts (free tiers available)
# Clone the repository
git clone <repository-url>
cd agentic-os
# Install dependencies
npm install
# Set up environment variables
cp .env.local.example .env.local
# Edit .env.local with your credentials
# Run development server
npm run dev| Variable | Description | Required |
|---|---|---|
UPSTASH_REDIS_REST_URL |
Upstash Redis REST URL | Yes |
UPSTASH_REDIS_REST_TOKEN |
Upstash Redis token | Yes |
TURSO_DATABASE_URL |
Turso database URL | Yes |
TURSO_AUTH_TOKEN |
Turso auth token | Yes |
QSTASH_TOKEN |
QStash token | Yes |
PINECONE_API_KEY |
Pinecone API key | Yes |
OPENAI_API_KEY |
OpenAI API key | For OpenAI provider |
GROQ_API_KEY |
Groq API key | For Groq provider |
AWS_ACCESS_KEY_ID |
AWS access key | For Bedrock provider |
NVIDIA_API_KEY |
NVIDIA API key | For NIM provider |
| Endpoint | Method | Description |
|---|---|---|
/api/chat |
POST | Main chat interface with memory |
/api/router |
POST | Low-level routing execution |
/api/inference |
POST | Raw model execution (debug) |
| Endpoint | Method | Description |
|---|---|---|
/api/providers |
GET/POST | Provider registry management |
/api/circuit |
GET/POST | Circuit breaker control |
/api/metrics |
GET | System metrics and performance |
/api/telemetry |
POST | Telemetry event ingestion |
/api/health |
GET | Health check |
| Endpoint | Method | Description |
|---|---|---|
/api/cron/epoch |
GET | Local Q-table learning epoch |
/api/cron/global |
GET | Global brain aggregation |
| Route | Description |
|---|---|
/dashboard |
CRT terminal control panel |
curl -X POST http://localhost:3000/api/chat \
-H "Content-Type: application/json" \
-d '{
"message": "Explain quantum computing",
"orgId": "default",
"taskType": "reasoning",
"complexity": 0.8
}'curl -X POST http://localhost:3000/api/router \
-H "Content-Type: application/json" \
-d '{
"prompt": "Write a Python function to sort a list",
"orgId": "default",
"type": "code",
"complexity": 0.6
}'| Provider | Models | Cost/1M | Status |
|---|---|---|---|
| Groq | Llama 3.1 8B/70B, Mixtral | $0.05/$0.08 | Active |
| OpenAI | GPT-4o, GPT-4o-mini | $0.15/$0.60 | Active |
| AWS Bedrock | Claude 3.5, Titan, Llama 3 | $3.00/$15.00 | Disabled |
| NVIDIA NIM | Llama 3.1 70B, Nemotron | $0.50/$0.50 | Disabled |
The router analyzes incoming requests to determine:
- Task Type:
reasoning,fast-tool,code,creative,analysis,debugging - Complexity: 0.0-1.0 score based on prompt analysis
- User Priority:
fast,cheap,quality,balanced
Uses Epsilon-Greedy Q-Learning:
- With probability
1-epsilon(90%): Selects provider with highest Q-value - With probability
epsilon(10%): Random exploration for learning
Per-provider health monitoring:
- Tracks error rate and latency
- Opens circuit after 5 consecutive failures or 20% error rate
- 30-second cooldown, then half-open testing
- Closes after 3 successful test calls
reward = quality * 1.0 - latency_norm * 0.3 - cost_norm * 0.2
Bounded to [-1.0, 1.0] with safeguards:
- Hard veto on failures
- Quality floor of 0.4
- Anomaly smoothing for perfect scores
Q_new = Q_old + alpha * (reward - Q_old)
State-action key format: {task_type}|{complexity_bucket}|{provider_id}
Each tenant (org) has:
- Isolated Redis Stream for telemetry
- Independent Q-table for learning
- Trust score for weighting contributions
- Rate limiting and cost controls
Anti-whale protection ensures fair learning:
weight = min(5.0, log10(1 + volume)) * trust_score
Request --> Task Profile --> Q-Table Lookup --> Provider Selection
|
Execution --> Telemetry --> Reward Governor --> Q-Table Update
|
(Async) --> Global Aggregation
| Layer | Technology |
|---|---|
| Framework | Next.js 14 (App Router) |
| Language | TypeScript |
| Styling | Tailwind CSS |
| Short-Term Memory | Upstash Redis |
| Persistent Store | Turso (SQLite) |
| Long-Term Memory | Pinecone (1024d vectors) |
| Job Queue | QStash |
| Runtime | Vercel Edge |
agentic-os/
├── src/
│ ├── app/
│ │ ├── api/
│ │ │ ├── chat/ # Chat inference endpoint
│ │ │ ├── router/ # Core routing engine
│ │ │ ├── inference/ # Raw model execution
│ │ │ ├── metrics/ # System metrics
│ │ │ ├── circuit/ # Circuit breaker control
│ │ │ ├── providers/ # Provider registry
│ │ │ ├── telemetry/ # Telemetry ingestion
│ │ │ ├── cron/
│ │ │ │ ├── epoch/ # Q-table learning worker
│ │ │ │ └── global/ # Global brain aggregation
│ │ │ └── health/ # Health check
│ │ ├── dashboard/ # CRT control panel UI
│ │ ├── layout.tsx # Root layout
│ │ ├── page.tsx # Root (redirects to dashboard)
│ │ └── globals.css # Global styles
│ ├── lib/
│ │ ├── core/ # Core engine
│ │ │ ├── router.ts # Main router
│ │ │ ├── circuitBreaker.ts
│ │ │ ├── meter.ts # Metering system
│ │ │ ├── registry.ts # Provider registry
│ │ │ ├── rewardGovernor.ts
│ │ │ ├── qtable.ts # Q-table engine
│ │ │ └── trustEngine.ts # Trust scoring
│ │ ├── db/ # Database layer
│ │ │ ├── redis.ts # Upstash Redis client
│ │ │ ├── turso.ts # Turso SQLite client
│ │ │ ├── pinecone.ts # Pinecone client (Node.js)
│ │ │ └── pinecone-edge.ts # Edge-compatible interface
│ │ ├── providers/ # Provider adapters
│ │ │ └── executor.ts # Unified provider execution
│ │ ├── types/ # TypeScript types
│ │ ├── config/ # Configuration
│ │ └── utils/ # Utilities
│ └── ...
├── .env.local # Environment variables
├── next.config.mjs # Next.js config
├── tailwind.config.ts # Tailwind config
└── package.json
# Epoch worker (Q-table learning)
npm run cron:epoch
# Global aggregation worker
npm run cron:globalThe Turso database includes:
providers- Provider configurationsrequest_logs- Detailed request logsprovider_metrics- Aggregated metricscircuit_history- Circuit breaker state changesq_table_snapshots- Q-table checkpointstrust_profiles- Tenant trust scorestenant_configs- Tenant settings
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT
Built for the agentic age. Route intelligently. Learn continuously. Scale infinitely.