← klaro.services
SentinelConsentraKlaroShieldBundlesPricing
Providers

Claude fallback when GPT-5 is rate limited

Wrap each provider in its own Klaro instance, then catch a retry-exhausted OpenAI call and fail over to Claude.

← All recipes
import { Klaro, retries } from "@klaroshield/sdk";
import OpenAI from "openai";
import Anthropic from "@anthropic-ai/sdk";

const openai = new OpenAI();
const anthropic = new Anthropic();

const primary = new Klaro()
  .use(retries({ max: 2 }))
  .wrap(openai.chat.completions.create.bind(openai.chat.completions));

const fallback = new Klaro()
  .use(retries({ max: 2 }))
  .wrap(anthropic.messages.create.bind(anthropic.messages));

async function chat(prompt: string) {
  try {
    return await primary({ model: "gpt-4o-mini", messages: [{ role: "user", content: prompt }] });
  } catch {
    return await fallback({ model: "claude-3-5-haiku-20241022", max_tokens: 1024, messages: [{ role: "user", content: prompt }] });
  }
}

Checked against the shipped @klaroshield/sdk API. Full docs at klaro.services/klaroshield/docs.

More in Providers

OpenAI + exponential backoffAnthropic Claude with retries + secret maskingGoogle GeminiOpenRouter (OpenAI-compatible endpoint)