The Vercel AI SDK's best design decision is easy to overlook: a model is a provider object you pass in, not a client you construct inside your handlers. That seam is exactly where a gateway fits — and fitting one in takes one file.

The file

Install the packages:

npm install ai @ai-sdk/openai zod 

Keep a virtual key in .env.local, then define the provider once:

// lib/ai.ts import { createOpenAI } from "@ai-sdk/openai"; export const nrouter = createOpenAI({ apiKey: process.env.NROUTER_API_KEY, baseURL: "https://api.nrouter.ai/v1", }); 

The OpenAI provider accepts a base URL, and the gateway answers at that address in the OpenAI wire format. The SDK's view of the world doesn't move.

What doesn't change

The route handler is the standard one:

import { streamText } from "ai"; import { nrouter } from "@/lib/ai"; export async function POST(req: Request) { const { messages } = await req.json(); const result = streamText({ model: nrouter("gpt-5.4-mini"), messages }); return result.toDataStreamResponse(); } 

Streaming, the client hooks and the data protocol are untouched, on the Edge Runtime or Node.js.

What the gateway carries

Three things that otherwise become application code:

  • Fallbacks as data. nrouter_fallbacks in the request body names the chain to try when a model errors. Or call nrouter/auto and set nrouter_optimize to quality, balanced or cost, with a cost band when cost wins.
  • Telemetry without plumbing. Every response carries x-nr-request-id, x-nr-model, x-nr-cost-status and x-nr-request-cost. The per-call record exists because the gateway wrote it.
  • Checks before generation. Guardrails run server-side in the request path, so a blocked call stops before tokens are spent on it.

Error handling stays idiomatic: an onError callback on streamText sees gateway stream errors, and HTTP errors follow the gateway's documented shape.

Nothing in the app becomes gateway-shaped. You keep the SDK you chose; the routing, fallback and accounting concerns move to the layer whose job they are.

Full walkthrough: Vercel AI SDK guide