Provider swap with OpenRouter
A good AI codebase makes swapping providers a one line change. The chat route already does this. Let us look at why the pattern matters and how OpenRouter simplifies the whole story.
const provider = process.env.CHAT_PROVIDER ?? 'openrouter';
const model =
provider === 'mistral' && process.env.MISTRAL_API_KEY
? mistral('mistral-large-latest')
: createOpenRouter({
apiKey: process.env.OPENROUTER_API_KEY ?? '',
}).chat(process.env.OPENROUTER_MODEL ?? 'google/gemini-2.5-flash-lite');The @openrouter/ai-sdk-provider returns a model object that streamText accepts exactly like any native provider adapter. That is the whole integration.
CHAT_PROVIDER=openrouter
OPENROUTER_API_KEY=your_openrouter_api_key_here
OPENROUTER_MODEL=google/gemini-2.5-flash-lite
# Needed only if you set CHAT_PROVIDER=mistral
MISTRAL_API_KEY=your_mistral_api_key_hereYou can try dozens of models behind one OpenRouter key. The Mistral branch is there to show how native provider adapters plug into the same streamText call.
Native adapters are great when you know exactly which provider you will use. OpenRouter shines when you want to compare models quickly, fall back automatically when a model is down, or expose a model selector in your own product. You trade a small latency overhead for flexibility.
Quiz: Quiz
Loading practice…
AI prompt: Try it: design a provider selector
Loading practice…
Checkpoint: Streaming chat checkpoint
Loading practice…