Switch providers
Type: Pattern
Outcome
Select a provider-backed completion model at the application boundary while keeping agent behavior provider-neutral. Use this for controlled environment routing, migration tests, or deliberate model experiments—not unbounded fallback after a side effect.
Prerequisites
pnpm add @anvia/core @anvia/openai @anvia/anthropic @anvia/gemini @anvia/mistral- Credentials only for providers enabled by deployment configuration
- A live contract suite for every selected model
Model factory and agent
import { AnthropicClient } from '@anvia/anthropic'
import { AgentBuilder } from '@anvia/core/agent'
import type { StreamingCompletionModel } from '@anvia/core/completion'
import { GeminiClient } from '@anvia/gemini'
import { MistralClient } from '@anvia/mistral'
import { OpenAIClient } from '@anvia/openai'
function completionModel(provider: string): StreamingCompletionModel {
if (provider === 'anthropic') {
return new AnthropicClient({ apiKey: process.env.ANTHROPIC_API_KEY })
.completionModel(process.env.ANTHROPIC_MODEL ?? 'claude-sonnet-4-20250514')
}
if (provider === 'gemini') {
return new GeminiClient({ apiKey: process.env.GEMINI_API_KEY })
.completionModel(process.env.GEMINI_MODEL ?? 'gemini-2.5-flash')
}
if (provider === 'mistral') {
return new MistralClient({ apiKey: process.env.MISTRAL_API_KEY })
.completionModel(process.env.MISTRAL_MODEL ?? 'mistral-large-latest')
}
if (provider === 'openai') {
return new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY })
.completionModel(process.env.OPENAI_MODEL ?? 'gpt-5')
}
throw new Error(`Unsupported provider: ${provider}`)
}
const model = completionModel(process.env.ANVIA_PROVIDER ?? 'openai')
const agent = new AgentBuilder('assistant', model)
.instructions('Answer in two sentences or less.')
.build()
console.log((await agent.prompt('What is a model boundary?').send()).output)Run and expected behavior
Set ANVIA_PROVIDER and its matching key, then run the file with pnpm tsx. The same agent code uses the selected adapter. Model IDs are examples from the current cookbook; confirm availability for your account.
Boundaries
Provider-neutral contracts do not imply identical quality or capabilities. Tools, schemas, reasoning, media, context limits, safety controls, usage, and errors vary. Do not accept provider or model names directly from an untrusted user. If retrying through another provider, do so only before non-idempotent work and record which model produced each result.
In production, use an allow-listed typed configuration, validate declared capabilities, smoke-test the exact account and model, compare normalized usage and quality, and keep rollback explicit.
Source and extensions
Compare the runnable OpenAI, Anthropic, Gemini, and Mistral examples. Next, build a provider conformance test and an offline quality evaluation.