Intelligent LLM router that reduces AI API costs by up to 60% without sacrificing quality.
Automatically routes your prompts to the most cost-efficient model for each task:
- Smart routing analyzes each prompt and selects the optimal provider
- Cost tracking monitors spending across all requests
- Multi-provider support works with Google Gemini, Anthropic Claude, OpenRouter, and more
- MCP integration seamlessly integrates with Claude Desktop
No configuration needed - just add your API keys and let the optimizer do the rest.
You need at least one of these:
- Google Gemini (recommended - free tier): https://aistudio.google.com/app/apikey
- Anthropic Claude: https://console.anthropic.com/
- OpenRouter (all models): https://openrouter.ai/keys
# Clone or navigate to project
cd ai-cost-optimizer
# Copy environment template
cp .env.example .env
# Edit .env and add your API key(s)
nano .env
# Install dependencies
pip install -r requirements.txt# Run the optimizer
python app/main.py
# You should see:
# "AI Cost Optimizer initialized with providers: ['gemini']"
# "Uvicorn running on http://0.0.0.0:8000"Keep this terminal running!
Edit your Claude Desktop config:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Add the MCP server:
{
"mcpServers": {
"ai-cost-optimizer": {
"command": "python3",
"args": ["/ABSOLUTE/PATH/TO/ai-cost-optimizer/mcp/server.py"],
"env": {
"COST_OPTIMIZER_API_URL": "http://localhost:8000"
}
}
}
}Important: Use the absolute path to mcp/server.py on your system!
curl -X POST http://localhost:8000/complete \
-H "Content-Type: application/json" \
-d '{"prompt": "What is quantum computing?", "max_tokens": 1000}'Response:
{
"response": "Quantum computing is...",
"provider": "gemini",
"model": "gemini-1.5-flash",
"tokens_in": 4,
"tokens_out": 50,
"cost": 0.000015,
"total_cost_today": 0.000015
}Please use the cost optimizer to answer: What is quantum computing?
You should see the response along with cost tracking.
POST /complete
{
"prompt": "Your prompt here",
"max_tokens": 1000
}Returns the response with cost breakdown.
GET /statsReturns total cost and request statistics.
GET /providersLists all available providers and their status.
GET /healthReturns service health status.
Create a .env file with your API keys:
# Provider API keys (add at least one)
GOOGLE_API_KEY=your-key-here
ANTHROPIC_API_KEY=your-key-here
OPENROUTER_API_KEY=your-key-here
CEREBRAS_API_KEY=your-key-here
# Optional configuration
PORT=8000
LOG_LEVEL=INFOThe optimizer uses an intelligent routing engine that:
- Analyzes incoming prompts
- Selects the most cost-efficient provider for the task
- Tracks all costs and usage metrics
- Automatically falls back to alternative providers if needed
Result: Up to 60% cost reduction compared to always using premium models.
# Check if port 8000 is in use
lsof -i :8000
# Verify API keys are set
cat .env- Verify absolute path in
claude_desktop_config.json - Check service is running:
curl http://localhost:8000/health - Completely quit and restart Claude Desktop (Cmd+Q on Mac)
- Check Claude Desktop logs
- Verify API keys are valid and active
- Check provider service status
- Review logs for detailed error messages
# Install test dependencies
pip install pytest pytest-asyncio
# Run test suite
pytest
# Run with coverage
pytest --cov=app tests/MIT - do whatever you want with it!
This is a learning project built to help developers reduce AI costs. Feel free to fork, modify, and make it yours!