How to Switch Providers
BAML separates function definitions from client configuration, making it easy to switch providers.
The Problem
Hardcoding providers makes it difficult to:
- Test with different models
- Switch providers based on environment
- A/B test model performance
Solution: Environment-Based Clients
// Define multiple clients
client<llm> OpenAIClient {
provider openai
model gpt-4
}
client<llm> AnthropicClient {
provider anthropic
model claude-3-opus
}
// Use environment variable to select
client<llm> DefaultClient {
provider env.LLM_PROVIDER // "openai" or "anthropic"
model env.LLM_MODEL
}
Runtime Selection
In your application code:
import { b } from './baml_client';
// Override client at runtime
const result = await b.MyFunction(input, {
clientOverride: 'AnthropicClient'
});
See Also
- Add Fallbacks - Automatic failover between providers
- Getting Started - Initial BAML setup
- Type System - How BAML ensures consistent outputs across providers