Get the Model Router AI service up and running in 5 minutes!
- Python 3.8 or higher
- pip package manager
- OpenAI API key (get from https://platform.openai.com/api-keys)
git clone https://github.com/nandth/model-router-ai.git
cd model-router-ai
pip install -r requirements.txtcp .env.example .envEdit .env and add your OpenAI API key:
OPENAI_API_KEY=sk-your-actual-key-here
MONTHLY_BUDGET_LIMIT=100.0Don't have an API key? You can still run the demo to see the routing logic in action!
See the routing system in action (no API key required):
python demo.pyThis demonstrates:
- ✅ Difficulty estimation (easy/medium/hard)
- ✅ Model selection (GPT-3.5 / GPT-4 / GPT-4 Turbo)
- ✅ Cost estimation
- ✅ Escalation logic
python -m app.mainThe API server will start at http://localhost:8000
Interactive Documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
curl http://localhost:8000/api/healthResponse: {"status":"healthy"}
curl http://localhost:8000/api/budgetShows: monthly limit, spent, remaining, request count
curl http://localhost:8000/api/statsShows: total requests, costs, latency, success rates
curl -X POST http://localhost:8000/api/prompt \
-H "Content-Type: application/json" \
-d '{"prompt": "What is Python?", "max_tokens": 100}'Response includes:
- Selected model (gpt-3.5-turbo, gpt-4, or gpt-4-turbo)
- Difficulty score and level
- Token usage and cost
- Latency in milliseconds
- Whether escalation occurred
pytest tests/ -vAll tests should pass, demonstrating the routing logic works correctly.
- Explore the API: Use the Swagger UI at http://localhost:8000/docs to interactively test all endpoints
- Adjust Budget: Set your monthly budget limit in
.env - Understand the Code:
app/services/difficulty_estimator.py- How difficulty is calculatedapp/services/routing_policy.py- How models are selectedapp/services/llm_client.py- How OpenAI is called with retry logic
- Customize: Adjust difficulty thresholds, modify routing logic, or add new features
┌─────────────────┐
│ Your Prompt │
└────────┬────────┘
│
▼
┌─────────────────────────┐
│ Difficulty Estimator │ Analyzes: length, keywords, structure
│ Score: 0.0 - 1.0 │ Level: easy / medium / hard
└────────┬────────────────┘
│
▼
┌─────────────────────────┐
│ Routing Policy │ Selects model:
│ - Easy → GPT-3.5 │ • gpt-3.5-turbo (cheap)
│ - Medium → GPT-4 │ • gpt-4 (balanced)
│ - Hard → GPT-4 Turbo │ • gpt-4-turbo (powerful)
└────────┬────────────────┘
│
▼
┌─────────────────────────┐
│ OpenAI Client │ Features:
│ - API call │ • Auto-retry (up to 3x)
│ - Retry on failure │ • Exponential backoff
│ - Check confidence │ • Smart escalation
└────────┬────────────────┘
│
▼
┌─────────────────────────┐
│ Response + Metadata │ Returns:
│ - Model used │ • Generated text
│ - Tokens & cost │ • Performance metrics
│ - Latency │ • Cost tracking
└─────────────────────────┘
Issue: "OpenAI API key not configured"
- Solution: Add
OPENAI_API_KEYto.envfile with a valid key from https://platform.openai.com/api-keys
Issue: "Monthly budget limit exceeded"
- Solution: Increase
MONTHLY_BUDGET_LIMITin.envor wait for next month
Issue: "Failed after 3 retries"
- Solution: Check your OpenAI API key is valid, check network connection, and verify OpenAI API status
Issue: Dependencies not installing correctly
- Solution: Make sure you have Python 3.8+ and run
pip install -r requirements.txtagain
- Test without API keys first: Run
python demo.pyto understand the routing logic - Monitor costs: Use
GET /api/budgetto track spending in real-time - Start small: Set a low budget limit initially while testing
- Use interactive docs: The Swagger UI at
/docsmakes testing easy - Check logs: The SQLite database stores all request history for analysis