Production-ready AI Cost Optimizer with:
- Smart Routing: Auto-selects optimal model based on complexity analysis
- Semantic Caching: pgvector-powered fuzzy matching (3x better cache hit rate!)
- Multi-Tenancy: Row-Level Security for data isolation
- 5 Providers: Gemini (free tier), Cerebras (fast), Claude (quality), OpenRouter (fallback)
- A/B Testing: Built-in experiment framework for testing routing strategies
- MCP Integration: Works with Claude Desktop
- Python 3.10+
- Supabase account (free tier works)
- At least one AI provider API key
- Create a new Supabase project at https://supabase.com
- Run the SQL migrations in order:
migrations/supabase_part1_extensions.sql(enables pgvector)migrations/supabase_create_tables.sql(creates tables)migrations/supabase_part2_schema_fixed.sql(RLS policies)
Create .env from the template:
cp .env.example .envEdit .env with your credentials:
# REQUIRED - Supabase Configuration
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_ANON_KEY=eyJhbGc...
SUPABASE_SERVICE_KEY=eyJhbGc...
SUPABASE_JWT_SECRET=your-jwt-secret
# AI PROVIDERS - Pick at least ONE
GOOGLE_API_KEY=your-key # FREE tier available - recommended to start
ANTHROPIC_API_KEY=your-key # Best quality for complex queries
CEREBRAS_API_KEY=your-key # Ultra-fast, cheap
OPENROUTER_API_KEY=your-key # Fallback, access 100+ modelscd /Users/tmkipper/Desktop/tk_projects/ai-cost-optimizer
pip install -r requirements.txtpython app/main.pyYou should see:
INFO: AI Cost Optimizer v4.0.0 initialized
INFO: Providers enabled: ['gemini', 'claude', 'cerebras']
INFO: Semantic caching: ENABLED (pgvector)
INFO: Uvicorn running on http://0.0.0.0:8000
# Test routing
curl -X POST http://localhost:8000/complete \
-H "Content-Type: application/json" \
-d '{"prompt": "What is AI?", "max_tokens": 100}'
# Check stats
curl http://localhost:8000/stats
# Check cache performance
curl http://localhost:8000/cache/statsEdit ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"ai-cost-optimizer": {
"command": "python3",
"args": ["/Users/tmkipper/Desktop/tk_projects/ai-cost-optimizer/mcp/server.py"],
"env": {
"COST_OPTIMIZER_API_URL": "http://localhost:8000"
}
}
}
}Restart Claude Desktop completely (Cmd+Q, then relaunch).
Hybrid Strategy (default with auto_route=true):
- Learning-based recommendation from feedback data
- Validated against complexity analysis
- Fallback to complexity-based routing if no learning data
Simple queries → Gemini/Cerebras (cheapest) Complex queries → Claude Haiku (best quality)
| Endpoint | Method | Description |
|---|---|---|
/complete |
POST | Route and execute prompt |
/stats |
GET | Usage statistics |
/cache/stats |
GET | Semantic cache performance |
/routing/metrics |
GET | Routing analytics |
/feedback |
POST | Submit quality feedback |
/health |
GET | Service health check |
/admin/learning/status |
GET | ML learning pipeline status |
┌─────────────────────────────────────────────────────────┐
│ FastAPI Service │
│ ┌─────────┐ ┌─────────────┐ ┌───────────────────┐ │
│ │ Routing │──│ Semantic │──│ Supabase │ │
│ │ Engine │ │ Cache │ │ PostgreSQL │ │
│ └────┬────┘ └─────────────┘ │ + pgvector │ │
│ │ │ + RLS │ │
│ ┌────▼────────────────────┐ └───────────────────┘ │
│ │ Providers: Gemini, │ │
│ │ Claude, Cerebras, etc. │ │
│ └─────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
- Start with Gemini - Free tier, no credit card needed
- Enable Semantic Caching - Repeat queries cost $0
- Submit Feedback - Improves routing over time via ML
- Check
/routing/metrics- See how routing decisions perform
Service won't start:
- Check Supabase credentials in
.env - Verify at least one AI provider API key is set
- Check port 8000:
lsof -i :8000
Cache not working:
- Ensure pgvector extension is enabled in Supabase
- Check
migrations/supabase_part1_extensions.sqlwas run
MCP not appearing:
- Verify absolute path in Claude Desktop config
- Check service:
curl http://localhost:8000/health - Completely restart Claude Desktop
app/
├── main.py # FastAPI service (900+ lines)
├── auth.py # JWT authentication
├── routing/ # Strategy-based routing engine
├── database/ # Supabase async client + semantic cache
├── embeddings/ # ML embedding generator
├── learning/ # Feedback-based retraining
└── services/ # Admin & routing services
mcp/
└── server.py # Claude Desktop integration
migrations/
├── supabase_part1_extensions.sql
├── supabase_create_tables.sql
└── supabase_part2_schema_fixed.sql
- Deploy the dashboard:
cd frontend && vercel --prod - Set up real-time monitoring with Supabase Realtime
- Configure A/B tests for routing strategies
- Review
docs/DEPLOYMENT.mdfor production setup
Built with ❤️ using FastAPI, Supabase, and pgvector