VernLLMVernLLM
Guides

Provider Fallback

Falling back to a secondary provider when the primary is down

vern-llm does not have built-in multi-provider fallback, but since every adapter produces the same VernLLM interface, wiring it yourself is only a few lines:

import OpenAI from 'openai';
import Anthropic from '@anthropic-ai/sdk';
import { VernLLM, fromAnthropic, isLLMError } from 'vern-llm';

const primary = new VernLLM({
  client: new OpenAI({ apiKey: process.env.OPENAI_API_KEY }),
  model: 'gpt-4o',
  circuitBreaker: true,
});

const fallback = new VernLLM({
  client: fromAnthropic(new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY })),
  model: 'claude-sonnet-4-6',
});

async function resilientCall(params: Parameters<VernLLM['call']>[0]) {
  try {
    return await primary.call(params);
  } catch (err) {
    if (
      isLLMError(err) &&
      (err.type === 'circuit_open' || (err.type === 'api' && err.status >= 500))
    ) {
      return fallback.call(params);
    }

    throw err;
  }
}

Each VernLLM instance tracks its own circuit breaker independently. If the primary provider's breaker opens, the fallback instance is unaffected.

After the cooldown period, the primary breaker enters half-open and allows a trial request. If that request succeeds, the breaker returns to closed and traffic can continue using the primary again.

Do not fall back on validation or parse errors. These indicate that the response was invalid, not that the provider is unavailable. Switching providers may produce the same invalid result for the same prompt.