Model Providers Management Guide
This guide shows you how to manage AI model providers (OpenAI, Anthropic, Ollama) in your application.
🎯 Quick Start: Try the Demo
Your project includes a live demo page at /demo/model-providers that shows:
- ✅ How to configure multiple AI providers
- ✅ Secure API key storage (AES-256 encryption)
- ✅ Dynamic provider switching
- ✅ Connection testing
- ✅ Code examples
Visit http://localhost:3001/demo/model-providers (after logging in) to see it in action!
What is Model Providers Management?
Model Providers Management allows you to:
- Configure multiple AI providers (OpenAI, Anthropic, Ollama, custom)
- Store API keys securely with encryption
- Switch between providers at runtime
- Test connections before using them
- Support for failover and A/B testing
Supported Providers
OpenAI
- Models: GPT-4, GPT-3.5, DALL-E, Whisper
- Provider Code:
'openai' - Requires: API key from platform.openai.com
Anthropic
- Models: Claude 3.5 Sonnet, Opus, Haiku
- Provider Code:
'anthropic' - Requires: API key from console.anthropic.com
Ollama
- Models: Llama, Mistral, Mixtral (local models)
- Provider Code:
'ollama' - Requires: Local Ollama installation
- Base URL: Usually
http://localhost:11434
Custom
- Models: Any OpenAI-compatible endpoint
- Provider Code:
'custom' - Requires: Custom base URL and API key
Quick Setup
1. Configure a Provider via UI
- Navigate to
/providers-demoin your app - Click "Add Provider"
- Fill in the details:
- Name (e.g., "OpenAI GPT-4")
- Provider type (openai, anthropic, ollama)
- API Key
- Base URL (optional, for Ollama or custom)
- Set as active
- Test connection
2. Configure via API
// Add a provider
const response = await fetch('/api/providers', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'OpenAI GPT-4',
provider: 'openai',
apiKey: 'sk-...',
isActive: true,
}),
});
const provider = await response.json();
3. Use in Your App
// The system automatically uses the active provider
const response = await fetch('/api/ai/chat', {
method: 'POST',
body: JSON.stringify({
messages: [{ role: 'user', content: 'Hello!' }],
}),
});
const data = await response.json();
console.log(data.message);
Advanced Usage
Using a Specific Provider
// Override active provider for a specific request
const response = await fetch('/api/ai/chat', {
method: 'POST',
body: JSON.stringify({
providerId: 'specific-provider-id',
messages: [{ role: 'user', content: 'Test' }],
}),
});
Fallback Strategy
// Server-side: Try primary, fallback to secondary
async function callAI(messages: Message[]) {
const providers = await prisma.modelProvider.findMany({
where: { isActive: true },
orderBy: { priority: 'asc' },
});
for (const provider of providers) {
try {
return await callProvider(provider, messages);
} catch (error) {
console.error(`Provider ${provider.name} failed:`, error);
continue; // Try next provider
}
}
throw new Error('All providers failed');
}
Cost Optimization
// Route based on complexity
function selectProvider(message: string) {
const isComplex =
message.length > 500 || message.includes('analyze') || message.includes('explain');
if (isComplex) {
return 'gpt-4-provider-id'; // More capable
} else {
return 'gpt-3.5-provider-id'; // Cheaper
}
}
Security
API Key Encryption
All API keys are encrypted before storage using AES-256:
// Automatic in the system
import { encrypt, decrypt } from '@/lib/encryption';
// Encryption happens automatically when creating providers
const encryptedKey = encrypt(apiKey);
// Decryption happens when using providers
const originalKey = decrypt(encryptedKey);
Environment Variable
Set your encryption key in .env:
ENCRYPTION_KEY=your-32-character-secret-key!!
⚠️ Important: Use a strong, random 32-character key in production!
Common Use Cases
1. Multi-Provider Fallback
Scenario: OpenAI is down, automatically switch to Anthropic
Solution: Configure multiple active providers with priority ordering
2. Local Development
Scenario: Use free local models during development
Solution: Use Ollama locally, switch to cloud providers in production
const provider = process.env.NODE_ENV === 'production' ? 'openai-prod' : 'ollama-local';
3. A/B Testing
Scenario: Compare GPT-4 vs Claude responses
Solution: Randomly assign users to different providers
const providerId = Math.random() > 0.5 ? 'gpt4-provider' : 'claude-provider';
4. Cost Management
Scenario: Control API costs per user
Solution: Route free users to cheaper models, premium to GPT-4
const providerId = user.isPremium ? 'gpt4-provider' : 'gpt35-provider';
API Reference
GET /api/providers
List all providers for the current user.
Response:
[
{
"id": "clx123...",
"name": "OpenAI GPT-4",
"provider": "openai",
"apiKey": "sk-...",
"baseUrl": null,
"isActive": true,
"createdAt": "2026-01-16T..."
}
]
POST /api/providers
Create a new provider.
Request:
{
"name": "Claude Sonnet",
"provider": "anthropic",
"apiKey": "sk-ant-...",
"isActive": true
}
PATCH /api/providers/:id
Update a provider.
Request:
{
"isActive": false
}
DELETE /api/providers/:id
Delete a provider.
POST /api/providers/:id/test
Test provider connection.
Response:
{
"success": true,
"message": "Connection successful"
}
Troubleshooting
"Invalid API Key" Error
- Check that the API key is correct
- Verify the key hasn't expired
- Ensure encryption key is consistent
"Connection Failed" for Ollama
- Make sure Ollama is running (
ollama serve) - Check base URL is correct (usually
http://localhost:11434) - Verify model is installed (
ollama list)
API Keys Not Decrypting
- Encryption key must be the same as when encrypted
- Check
ENCRYPTION_KEYin.envhasn't changed - 32 characters required for AES-256
Next Steps
- Configure Providers: Add your API keys at
/providers-demo - Test Connections: Verify each provider works
- Try the Chat: Use providers in
/chatinterface - Prompt Templates: Combine with
/admin/promptsfor powerful workflows