How to use LiteLLM to switch providers
Quick answer
Use
LiteLLM by configuring its provider parameter to switch between AI providers like OpenAI, Anthropic, or Mistral. Change the provider argument in the LiteLLM client initialization to switch providers without changing your application code.PREREQUISITES
Python 3.8+API keys for desired AI providers (e.g., OpenAI, Anthropic, Mistral)pip install litellm
Setup
Install litellm via pip and set environment variables for your AI provider API keys. LiteLLM supports multiple providers with a unified interface.
pip install litellm output
Collecting litellm Downloading litellm-1.0.0-py3-none-any.whl (15 kB) Installing collected packages: litellm Successfully installed litellm-1.0.0
Step by step
Initialize LiteLLM with the provider parameter set to your desired AI provider. Use the same interface to send prompts regardless of the provider.
import os
from litellm import LiteLLM
# Set your API keys in environment variables
# os.environ['OPENAI_API_KEY'] = 'your_openai_key'
# os.environ['ANTHROPIC_API_KEY'] = 'your_anthropic_key'
# Initialize LiteLLM with OpenAI provider
client = LiteLLM(provider='openai', api_key=os.environ['OPENAI_API_KEY'])
response = client.chat_completion(messages=[{"role": "user", "content": "Hello from OpenAI!"}])
print("OpenAI response:", response)
# Switch to Anthropic provider
client = LiteLLM(provider='anthropic', api_key=os.environ['ANTHROPIC_API_KEY'])
response = client.chat_completion(messages=[{"role": "user", "content": "Hello from Anthropic!"}])
print("Anthropic response:", response) output
OpenAI response: {'choices': [{'message': {'role': 'assistant', 'content': 'Hello from OpenAI! How can I assist you today?'}}]}
Anthropic response: {'choices': [{'message': {'role': 'assistant', 'content': 'Hello from Anthropic! How can I help you today?'}}]} Common variations
You can switch providers dynamically by changing the provider parameter at runtime. LiteLLM supports async calls and streaming for supported providers.
import asyncio
async def async_chat():
client = LiteLLM(provider='mistral', api_key=os.environ['MISTRAL_API_KEY'])
response = await client.chat_completion_async(messages=[{"role": "user", "content": "Hello from Mistral async!"}])
print("Mistral async response:", response)
asyncio.run(async_chat()) output
Mistral async response: {'choices': [{'message': {'role': 'assistant', 'content': 'Hello from Mistral! How can I assist you asynchronously?'}}]} Troubleshooting
- If you get authentication errors, verify your API keys are correctly set in environment variables.
- If a provider is not responding, check network connectivity and provider status.
- Ensure you use the correct
providerstring matching LiteLLM's supported providers.
Key Takeaways
- Use the
providerparameter inLiteLLMto switch AI providers seamlessly. - Set API keys in environment variables to avoid hardcoding and enable easy provider switching.
- LiteLLM supports synchronous, asynchronous, and streaming calls depending on the provider.
- Verify provider names and API keys to prevent authentication and connectivity issues.