Skip to main content
Build flexible AI applications that work with multiple providers by abstracting provider logic, implementing fallback strategies, and routing requests based on cost, speed, or feature requirements.
Multi-provider architecture provides resilience. By supporting multiple AI providers, your application can gracefully handle rate limits, outages, and pricing changes without code changes.
For automatic failover, use built-in fallbacks. The rewrite endpoint supports automatic provider failover via the x-lava-fallbacks header — no try/catch required. The patterns on this page are for cases where you need custom routing logic beyond simple failover.

Provider Configuration Pattern

Create a configuration-driven approach instead of hardcoding providers:
For a complete provider abstraction class that wraps this configuration with unified request/response handling, see the full example in the Node.js SDK reference.

Provider Switching with Configuration

Environment-Based Selection

Switch providers via environment variables:
Environment configuration:

Dynamic Provider Selection

Choose provider based on request characteristics:

Model-Based Routing

Route to providers based on desired model:

Fallback Strategies

Sequential Fallback

Try providers in priority order until one succeeds:

Smart Retry with Exponential Backoff

Add exponential backoff for transient errors before falling back to the next provider:

Cost-Aware Routing

Fallback to cheaper providers when budget is a concern:

Feature Detection

Check provider capabilities before using advanced features:

Graceful Feature Degradation

Fallback when features aren’t supported:

Troubleshooting

Check:
  • Exception is being caught properly in try/catch
  • Provider priority order is correct (lower number = higher priority)
  • Error is thrown (not just logged) when provider fails
Debug:
Issue: Provider claims to support feature but request failsReasons:
  • Provider configuration outdated (features changed)
  • Feature available but different API format required
  • Feature requires specific model (not all models support all features)
Solution: Add model-specific feature checks:
Problem: Estimated costs don’t match actual Lava costsExplanation:
  • Lava costs include provider base cost + merchant fee + service charge
  • Provider pricing varies by model and usage
  • Costs are only accurate AFTER request completes
Solution: Track usage from response body and calculate cost based on your pricing:
Cause: Provider uses different response format than expectedSolution: Handle multiple response formats:

Next Steps

Streaming

Handle streaming responses across different providers

Forward Proxy & Authentication

Understand URL encoding and provider routing

Supported Providers

See all supported providers and their features

Webhooks

Receive real-time notifications for usage events