Technical document

Design for Swappable AI Providers in RAG Infrastructure

In RAG infrastructure utilizing OpenAI API and Gemini, we organize boundary design that separates responsibilities for generation, embedding, and reranking, making it resilient to AI provider changes.
AI Development / RAG~10 minPublication Date September 22, 2026Update date September 22, 2026
Design for Swappable AI Providers in RAG Infrastructure

Summary

Key points of this document

  • Where are the design challenges?
  • How to define boundaries?
  • Details that matter in implementation

Overview

When integrating RAG into business systems, implementations tend to become tightly coupled with the initially chosen AI provider. Without separately considering generation models, embedding models, reranking, streaming, and error formats, subsequent changes can lead to major refactoring. The interesting aspect of the design is not to hide provider differences, but to pre-determine where these differences will manifest in terms of quality, cost, latency, and audit logs.

Where are the design challenges?

In RAG, not only the process of passing search results to the prompt, but also document splitting, embedding, searching, reranking, answer generation, and citation display are chained. If AI provider-specific response formats and exception handling are written directly into each layer, a wide range of areas will need to be fixed every time the model changes.

On the other hand, if it's too generalized, you can't leverage the strengths of each provider. Streaming, JSON output, tool calling, embedding dimensions, pricing, and rate limits differ. The purpose of creating boundaries is not to eliminate differences, but to control where those differences manifest.

What becomes effective later in a RAG infrastructure is not switching model names, but the difference in behavior for the same question. To handle models strong in JSON output, models strong in long citations, and models with low latency but rough handling of evidence, all through the same interface, the business side needs to define success and failure.

How to define boundaries?

In design, generation, embedding, and reranking are defined as separate ports. The business side only sees the meaning of "get answer candidates from questions and context," "vectorize text," and "reorder candidate documents," while specific OpenAI API and Gemini calls are encapsulated within adapters.

Even if the generation, embedding, and reranking ports look similar, they are contracts with different lifespans. The generation port is prone to change with streaming and tool calling, while the embedding port remains in the data structure due to dimensions and distance functions. Being aware of this difference, the granularity of replacement should be divided by data asset units, not API call units.

Details that matter in implementation

In TypeScript and Python, input and output types are determined first. Generation results include body, citation ID, tokens used, and completion reason, while exceptions are categorized into authentication failure, rate limit, timeout, and quality failure. For embeddings, dimension count and normalization status are stored as metadata to prevent mixing vectors from different models in the same index.

In implementation,ModelInvocation...standardize call records like "ModelInvocation" across all providers, saving input tokens, output tokens, temperature, search result ID, and retry count. More than the answer body, knowing which context was passed and which model terminated for what reason is effective for future quality improvements.

  • When changing embedding models, do not mix old and new vectors in the same index; separate them by index_version.
  • Streaming responses should not be in a UI-specific format; express chunks, completion, and failure as events.
  • Retain model identifiers in fallback answers to track quality degradation in logs.

Observe how it breaks

In validation, the same question set is run across multiple providers, comparing not only accuracy but also citation validity, unnecessary assertions, response time, and failure rate. It's important not to rely solely on automated evaluation, but to retain questions where incorrect business answers pose significant problems as a golden set.

In validation, the same evaluation set is run per provider, comparing not only the correctness of answers but also missing citations, out-of-context completions, and responses to questions that should be rejected. Instead of just checking if it's not broken after changing models, observing how the breakage changes for different types of questions makes operational decisions easier.

Discarded options and tradeoffs

If abstraction is too thick, the interface needs to be extended every time model-specific features are used. Conversely, if it's too thin, it affects business code during replacement. In the initial stage, keeping the scope of commonality small and just separating generation and embedding can be sufficiently effective.

Strong abstraction delays the use of new features, while thin abstraction increases the impact during switching. Realistically, it's easier to maintain by separating the layer hidden from business logic from the layer that explicitly uses model-specific features, and making it possible to later search for where specific features were used.

Decision criteria to retain on site

A design that allows switching AI providers does not mean a design where any model produces the same answer. Limiting where differences emerge and making those differences visible through evaluation is a realistic criterion for long-term operation of a RAG infrastructure.

The term "provider-agnostic" alone does not constitute a design. Differentiating which differences to absorb, which to expose, and which to monitor through evaluation becomes the technical core for long-term RAG usage.

Technical documents

We will continue to increase technical documentation.

We will continue to organize our thoughts on AI, cloud, business app development, requirements definition, and operational design into documents.

Go to technical documentation list