One /v1 to rule them: designing a provider-agnostic AI gateway
Sprinkling provider SDKs across services works until the day you switch models — then the change ripples through every call site. Here's the seam I put in instead: a single OpenAI-compatible gateway, a model router behind it, and a stable public contract that never leaks a model id.
Issue
The problem, framed precisely.
Provider SDKs metastasize through your codebase
The moment you integrate more than one model provider — OpenAI, Gemini, Anthropic — the naive path is to import each provider's SDK where you need it. It works, right up until the day you want to swap a model for cost, latency, or quality. Then the change isn't a config edit; it's a diff across every call site, each with its own request shape, auth, and error semantics. The provider has leaked into your domain logic, and now your product code knows what a 'model' is.
The second-order problem is observability. With SDK calls scattered, there's no single place to attach a request id, measure latency, count tokens, or enforce a timeout. You end up with N inconsistent instrumentations and no coherent view of what your AI layer actually costs or how often it fails.
Hypothesis
What I believed was true, and why.
The provider is an implementation detail, not a domain concept
My hypothesis: if product code never names a provider or a model, then swapping either becomes a configuration change with a blast radius of zero. The way to enforce that is a single choke point speaking one shape — an OpenAI-compatible /v1 interface — with a model router behind it that owns the actual selection. Product code calls ai.chat / ai.extract / ai.generate; it does not know, and cannot know, what served the request.
“If a single grep for a provider name returns a hit outside the gateway, the abstraction has already failed.”
Strategy
The approach and the trade-offs weighed.
A choke point, a router, and a stable label
The design has three parts. First, an AI Gateway that every AI call funnels through — the single trust and telemetry boundary. Second, a Model Router inside it that maps a task type (chat, extract, generate, embed) to a concrete model + parameters via hot-reloadable policy, so routing changes without a deploy. Third, a stable public label: the response meta always reports model: "gateway-managed", so the outward contract tracks JSON shape, never the model behind it.
- Swappability: change a model in one policy, nothing downstream moves.
- Central telemetry: every call carries a request id, latency, and token count through one place.
- Safety in one location: retries, timeouts, and structured-output validation live at the boundary, not scattered across services.
The cost I accepted: one extra network hop, and the discipline of never leaking a raw model id past the boundary. For a portfolio-scale system that hop is single-digit milliseconds against multi-second inference — a rounding error against the flexibility it buys.
Implementation
How it was actually built.
The SDK surface and the routing seam
Callers touch a four-method SDK. Note what's absent: no model, no temperature, no provider. Those are the router's job.
ai.chat({ messages, stream? }) // conversational, optional SSE
ai.extract({ input, schema }) // structured extraction \u2192 validated JSON
ai.generate({ prompt, schema? }) // generation, optionally structured
ai.embed({ input }) // embeddings
// every call carries a ULID requestId propagated end-to-endThe router is a pure function from task type to decision. Because it's data, not code, the policy can hot-reload — and because the model id never escapes, I can point 'chat' at a different backend tomorrow with zero product changes:
// Model Router \u2014 task \u2192 concrete model + params. Data, not branches.
const POLICY = {
chat: { model: CHAT_MODEL, params: { temperature: 0.7, maxTokens: 3072 } },
extract: { model: CHAT_MODEL, params: { temperature: 0, maxTokens: 4096 } },
generate: { model: CHAT_MODEL, params: { temperature: 0.3, maxTokens: 4096 } },
embed: { model: EMBED_MODEL, params: { temperature: 0, maxTokens: 0 } },
};
export const route = (task: TaskType) => POLICY[task];
// Response meta always reports model: 'gateway-managed'.One real-world wrinkle I hit building this: some OpenAI-compatible endpoints return an SSE stream even for non-stream requests. The gateway's parser has to accept either raw JSON or the last non-[DONE] data frame — otherwise the abstraction is brittle exactly where it promised to be robust. That defensive parse is the difference between 'works with my one provider' and 'works with any /v1'.
Summary
Outcome, evidence, and what I'd change.
The payoff and the honest limits
The gateway delivered what the hypothesis promised: I swapped the entire inference backend by editing two environment variables, and not one line of product code moved. Every AI request now emits a request id, latency, and token count through a single point, so cost and failure are measurable instead of inferred.
The honest limits: a single gateway is a single point of failure and a potential bottleneck, so it has to be stateless and horizontally scalable, and its own health has to be watched harder than any downstream. And 'gateway-managed' as a label means callers can't pin a specific model even when they legitimately want to (an eval harness, a reproducibility need) — so the design keeps an internal escape hatch that never crosses the public boundary. Abstractions earn their keep by what they refuse to expose; the discipline is the point.
Written by Ridha Fahmi Junaidi · AI Implementation Lead · AI Integration Engineer