Module 1026 min read · Building with AI APIs

Multi-Provider Strategies

The AI model landscape is not static. The model that offers the best quality-to-cost ratio today may be surpassed by a competitor in three months. Provider outages, pricing changes, and capability improvements happen continuously. Applications that are architecturally locked to a single provider are at the mercy of that provider's uptime, pricing decisions, and capability trajectory. Multi-provider strategies — designing applications to work with multiple AI providers interchangeably — give you resilience, cost flexibility, and the ability to route different tasks to the models best suited to handle them.

The case for provider diversity

Single-provider lock-in has real costs. When OpenAI experienced extended outages in 2023 and 2024, applications built without fallback providers went down entirely. When pricing changes make a previously cost-effective model prohibitively expensive, single-provider apps have no lever to pull. When a new model from a competing provider dramatically outperforms the incumbent on your specific use case, single-provider apps cannot take advantage without an architectural change.

The counterargument is that multi-provider architectures are more complex to build and maintain. This is true but overstated. The OpenAI-compatible API format — which has become something close to an industry standard — means that many providers (Groq, Together AI, Perplexity, Mistral, and others) accept OpenAI-formatted requests, making switching a matter of changing the base URL and API key rather than rewriting integration code. Even providers with custom APIs (Anthropic, Google) have sufficiently similar concepts that an abstraction layer with a hundred lines of adapter code buys complete provider portability.

Building a provider abstraction layer

The core of a multi-provider strategy is a thin abstraction layer that provides a unified interface for all your AI calls and delegates to provider-specific implementations. The interface needs to handle the concepts that all providers share: system prompts, user and assistant messages, model selection, temperature and other sampling parameters, streaming, and tool definitions.

A minimal Python implementation might look like this: a LLMClient class with a complete() method that accepts a standard set of parameters and routes to the appropriate provider SDK based on the model name or an explicit provider parameter. The routing logic is the entire abstraction: OpenAI model names go to the OpenAI SDK, Anthropic model names to the Anthropic SDK, with parameter translation as needed (Anthropic's API puts the system prompt as a separate parameter rather than a message with role "system"; this translation is three lines of code).

LiteLLM: the practical option

Rather than writing this abstraction layer yourself, LiteLLM is a production-grade open-source library that provides exactly this: a unified OpenAI-compatible interface for 100+ model providers. You call litellm.completion() with a model string like "claude-3-5-sonnet-20241022" or "gemini/gemini-1.5-pro" and LiteLLM handles the provider routing, parameter translation, streaming normalization, and retry logic. It also provides usage tracking, cost calculation, and a proxy server mode for team deployments. For most production applications, using LiteLLM is preferable to maintaining a custom abstraction.

Routing strategies

Once you have a provider abstraction, you need a routing strategy: which model handles which requests? Several patterns address different goals.

Cost-based routing

Not all requests require the same model capability. A request to summarize a short customer email can be handled correctly by a cheap, fast model. A request to analyze a complex legal document for risk factors may need a frontier model. Cost-based routing classifies requests by complexity — using heuristics like prompt length, presence of specific task types, or a lightweight classifier model — and routes simple requests to cheaper models. On a realistic workload, this can reduce model costs by 40-70% while maintaining quality on tasks that require it.

Latency-based routing

Different providers have different latency characteristics for the same model capability level. Groq runs Llama models on custom inference hardware with dramatically lower latency than hosted alternatives — appropriate for chat applications where response time is critical. Routing latency-sensitive requests to lower-latency providers, even at some quality trade-off, improves the user experience on interactive features.

Fallback routing

For resilience, define a priority-ordered list of model options for each request type. If the primary provider returns an error or timeout, retry with the fallback. A sensible pattern: primary is your preferred model, fallback is a different provider with equivalent capability, last resort is a smaller model that will handle the request at reduced quality rather than failing entirely. This ensures that provider outages degrade service quality gracefully rather than producing hard failures.

Prompt portability challenges

Switching providers is not cost-free even with a provider abstraction layer, because prompts are not fully portable between models. A system prompt and few-shot examples tuned for GPT-4o may produce noticeably different results with Claude or Gemini — different models have different response styles, instruction-following behaviors, and sensitivities to prompt structure.

Test prompts across providers before committing

Before routing production traffic to a new model or provider, run your full evaluation suite against it — not just a spot check on a few examples. Models that perform identically on standard benchmarks can produce meaningfully different outputs on your specific task and prompt structure. The evaluation investment up front prevents quality regressions in production. If you do not have an evaluation suite, building one is the prerequisite for any serious multi-provider strategy.

Evaluating models for your use case

Standardized benchmarks — MMLU, HumanEval, MATH, and the various leaderboards — measure general capability but are poor predictors of performance on specific applications. A model that ranks highly on coding benchmarks may perform poorly on your specific domain's terminology and requirements. Building task-specific evaluations is the only reliable way to know which model is best for your use case.

A minimal evaluation setup: a dataset of 50-200 representative inputs from your production workload (anonymized if necessary), a set of expected outputs or evaluation criteria for each, and an automated scoring function that computes a quality metric. Run each candidate model against this dataset and compare scores. The evaluation investment is typically a few hours of work and pays back immediately in confidence about model selection decisions.

Frontier models for quality-critical tasks
GPT-4o, Claude 3.5 Sonnet/Opus, and Gemini 1.5 Pro are the current frontier for complex reasoning, nuanced instruction following, and tasks requiring broad knowledge. Use them for tasks where quality matters most and cost is secondary — complex analysis, high-stakes decisions, tasks with long contexts or difficult reasoning chains.
Mid-tier models for standard tasks
GPT-4o-mini, Claude 3.5 Haiku, and Gemini 1.5 Flash offer most of the quality of frontier models at a fraction of the cost. For well-defined tasks with clear instructions — extraction, summarization, classification, straightforward generation — mid-tier models perform at or near frontier quality at 10-20x lower cost.
Fast inference providers for latency-critical paths
Groq, Cerebras, and similar hardware-accelerated inference providers offer dramatically lower latency for open-weight models. For interactive chat interfaces where sub-second response time is important, these providers can deliver comparable quality to hosted mid-tier models at significantly lower latency, at the cost of less flexibility in model selection.
You are ready for the final assessment

You have completed all ten modules of Building with AI APIs. You understand the full stack: from making your first API call through streaming, embeddings, semantic search, function calling, production engineering, and multi-provider architectures. The final assessment covers the complete course. Approach it with the same rigor you brought to each module.