A smart proxy that routes LLM API requests to different models based on task complexity, tracks costs, and handles fallbacks.
- Smart Routing: Automatically routes requests to appropriate models based on task type
- Simple queries → fast/cheap models (gpt-4o-mini, llama3.2)
- Complex/coding tasks → smart models (claude-sonnet, gpt-4o)
- Long context → large context window models
- OpenAI-Compatible API: Drop-in replacement for OpenAI's
/v1/chat/completions - Fallback Chains: Automatic failover when primary model is unavailable
- Cost Tracking: Per-request logging with SQLite storage
- YAML Configuration: Easy model and routing customization
pip install -r requirements.txt
export OPENAI_API_KEY="sk-..." export ANTHROPIC_API_KEY="sk-ant-..."
uvicorn src.model_router.main:app --reload
# Simple query (routes to gpt-4o-mini) curl -X POST http://localhost:8000/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{"messages": [{"role": "user", "content": "What is 2+2?"}]}' # Coding query (routes to claude-sonnet) curl -X POST http://localhost:8000/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{"messages": [{"role": "user", "content": "Write a Python function to sort a list"}]}' # Override model selection curl -X POST http://localhost:8000/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "Hello"}]}'
OpenAI-compatible chat completions endpoint.
Request:
{
"messages": [{"role": "user", "content": "Hello"}],
"model": null,
"temperature": 1.0,
"max_tokens": null
}Response:
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-4o-mini",
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "Hello!"},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 5,
"total_tokens": 15
},
"routed_model": "gpt-4o-mini",
"task_type": "simple",
"cost_usd": 0.000015,
"latency_ms": 234.5
}Health check endpoint.
Usage and cost statistics.
{
"total_requests": 150,
"total_cost_usd": 0.45,
"total_input_tokens": 50000,
"total_output_tokens": 25000,
"requests_by_model": {"gpt-4o-mini": 100, "claude-sonnet": 50},
"costs_by_model": {"gpt-4o-mini": 0.05, "claude-sonnet": 0.40}
}Edit config.yaml to customize models and routing rules:
models: gpt-4o-mini: provider: openai endpoint: https://api.openai.com/v1 cost_per_1k_input: 0.00015 cost_per_1k_output: 0.0006 max_context: 128000 claude-sonnet: provider: anthropic endpoint: https://api.anthropic.com/v1 model_id: claude-sonnet-4-20250514 cost_per_1k_input: 0.003 cost_per_1k_output: 0.015 max_context: 200000 llama3.2: provider: ollama endpoint: http://localhost:11434/v1 cost_per_1k_input: 0.0 cost_per_1k_output: 0.0 max_context: 128000 routing: simple: [gpt-4o-mini, llama3.2] complex: [claude-sonnet, gpt-4o] coding: [claude-sonnet, gpt-4o] long_context: [claude-sonnet] classification: coding_keywords: [code, function, debug, implement] complex_keywords: [analyze, compare, explain in detail] context_threshold: 32000
- openai: OpenAI API (gpt-4o, gpt-4o-mini, etc.)
- anthropic: Anthropic API (claude-sonnet, claude-opus, etc.)
- ollama: Local Ollama server (llama3.2, mistral, etc.)
| Type | Triggers | Default Models |
|---|---|---|
simple |
Short queries, greetings | gpt-4o-mini |
coding |
Keywords: code, function, debug | claude-sonnet |
complex |
Keywords: analyze, compare | claude-sonnet |
long_context |
>32K tokens | claude-sonnet |
pytest tests/ -v
model-router/
├── config.yaml # Model and routing configuration
├── requirements.txt # Python dependencies
├── src/model_router/
│ ├── main.py # FastAPI application
│ ├── config.py # YAML config loader
│ ├── classifier.py # Task complexity classifier
│ ├── router.py # Model selection + fallback
│ ├── cost_tracker.py # SQLite cost logging
│ ├── schemas.py # Pydantic models
│ └── clients/ # LLM API clients
│ ├── base.py
│ ├── openai.py
│ └── anthropic.py
└── tests/ # Pytest test suite
| Variable | Description |
|---|---|
OPENAI_API_KEY |
OpenAI API key |
ANTHROPIC_API_KEY |
Anthropic API key |
CONFIG_PATH |
Path to config file (default: ./config.yaml) |