A comprehensive Python library for managing fallback mechanisms for Large Language Model (LLM) API calls using the LiteLLM library. This library helps you handle API failures gracefully by providing alternative models to try when a primary model fails.
- Comprehensive Model Management: Access to thousands of LLM models across multiple providers
- Intelligent Fallback Strategies: Automatic fallback configuration based on model capabilities and cost
- Cost Optimization: Built-in cost calculation and model sorting by price and performance
- Multi-Modal Support: Support for chat, completion, embedding, vision, audio, and more model types
- Provider Management: Custom provider configuration and API key management
- LiteLLM Integration: Seamless integration with LiteLLM proxy for production deployments
- GUI Interface: Interactive model filtering and selection interface
- Configuration Export: Generate ready-to-use LiteLLM YAML configurations
pip install llm-fallbacks
git clone https://github.com/bodencrouch/llm-fallbacks.git
cd llm-fallbacks
pip install -e .
git clone https://github.com/th3w1zard1/llm_fallbacks.git
cd llm_fallbacks
uv sync
uv run src/tests/test_core.py
from llm_fallbacks import get_chat_models, get_fallback_list
# Get all available chat models
chat_models = get_chat_models()
# Get fallback models for a specific model
fallbacks = get_fallback_list("gpt-4", model_type="chat")
print(f"Fallback models for GPT-4: {fallbacks}")
from llm_fallbacks import filter_models
# Get free chat models only
free_models = filter_models(
model_type="chat",
free_only=True
)
# Get models with specific capabilities
vision_models = filter_models(
model_type="chat",
vision=True
)
Sort the models by cost:
from llm_fallbacks import get_litellm_models, sort_models_by_cost_and_limits
# Get all models with cost information
models = get_litellm_models()
# Sort models by cost (cheapest first)
sorted_models = sort_models_by_cost_and_limits(models, free_only=True)
print(repr(sorted_models))
Calculate cost for a specific model:
from llm_fallbacks import get_litellm_models, calculate_cost_per_token
model_spec = get_litellm_models()["gpt-5"]
cost_per_token = calculate_cost_per_token(model_spec)
print(f"Cost per token: ${cost_per_token:.6f}")
from llm_fallbacks.generate_configs import to_litellm_config_yaml
# Generate LiteLLM configuration
config = to_litellm_config_yaml(
providers=[], # Your custom providers
free_only=True
)
# Save to YAML file
import yaml
with open("litellm_config.yaml", "w") as f:
yaml.dump(config, f)
or run generate_configs.py
:
uv run src/generate_configs.py
get_litellm_models()
: Retrieve all available LiteLLM models with specificationsget_chat_models()
: Get models supporting chat completionget_completion_models()
: Get models supporting text completionget_embedding_models()
: Get models supporting text embeddingsget_vision_models()
: Get models supporting vision tasksget_audio_models()
: Get models supporting audio processing
- Model Specifications: Comprehensive model metadata including capabilities, costs, and limits
- Provider Configuration: Custom provider setup for private or specialized models
- Fallback Strategies: Intelligent fallback configuration based on model compatibility
- LiteLLM YAML Export: Generate production-ready LiteLLM proxy configurations
- Fallback Mapping: Automatic fallback model assignment based on capabilities
- Cost Optimization: Prioritize models by cost and performance
- GUI Application: Tkinter-based interface for model exploration (experimental)
- Advanced Filtering: Multiple filtering methods (regex, quantile, outlier detection)
- Data Export: Export filtered results to various formats
The library generates several configuration files that are stored in the configs/
directory:
litellm_config.yaml
: Full LiteLLM configuration with all modelslitellm_config_free.yaml
: Configuration with free models onlyall_models.json
: Complete model database in JSON formatfree_chat_models.json
: Free chat models onlycustom_providers.json
: Custom provider configurations
These files are automatically updated daily at 12:00 AM UTC via GitHub Actions to ensure you always have the latest model information and configurations.
from llm_fallbacks.config import CustomProviderConfig
custom_provider = CustomProviderConfig(
name="my-custom-provider",
base_url="https://api.myprovider.com",
api_key="your-api-key",
models=["custom-model-1", "custom-model-2"]
)
from llm_fallbacks.config import RouterSettings
router_settings = RouterSettings(
allowed_fails=3,
cooldown_time=30,
fallbacks=[{"gpt-4": ["gpt-3.5-turbo", "claude-3-sonnet"]}]
)
python -m llm_fallbacks
python -m llm_fallbacks.generate_configs
python test_system.py
- Python 3.12+
- Poetry or pip for dependency management
# Install development dependencies
pip install -r requirements-dev.txt
# Install pre-commit hooks
pre-commit install
# Run tests
pytest tests/
The project includes automated workflows:
- Python Package: Runs on every push/PR with linting, testing, and building
- Python Publish: Automatically publishes to PyPI on releases
- Daily Config Update: Updates model configurations daily at 12:00 AM UTC
The library automatically maintains up-to-date model configurations through:
- Daily Updates: GitHub Actions workflow runs every day at 12:00 AM UTC
- Model Database: Fetches latest model information from LiteLLM
- Fallback Strategies: Generates intelligent fallback configurations
- Version Control: All changes are automatically committed and tracked
This ensures your applications always have access to the latest models, pricing, and capabilities without manual intervention.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the Business Source License 1.1 (BSL 1.1) - see the LICENSE file for details.
For support and questions:
- Open an issue on GitHub
- Check the documentation
- Contact: [email protected]
- Built on top of LiteLLM
- Inspired by the need for robust LLM fallback strategies
- Community contributions and feedback
Note: This library is designed to work with the LiteLLM ecosystem and provides fallback mechanisms for production LLM applications. Always test fallback configurations in your specific environment before deploying to production.