Vercel AI SDK
Integrate nRouter with Vercel AI SDK in Next.js and React apps. Stream completions, utilize guardrails, and track costs while routing to any major AI model.
Integrate nRouter with the Vercel AI SDK to stream completions, enforce server-side guardrails, and track list-price costs in Next.js, React, and Node.js applications. Point @ai-sdk/openai to https://api.nrouter.ai/v1 to add configured model fallbacks, exact token pricing, and edge moderation with zero client refactoring.
For framework details, see the official Vercel AI SDK documentation.
Installation & Setup
Install the Vercel AI SDK, OpenAI provider, and Zod:
npm install ai @ai-sdk/openai zodStore your virtual API key in .env.local and initialize the provider:
// 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",
});Real-Time Streaming (streamText)
Stream completions to web clients using Next.js route handlers:
// app/api/chat/route.ts
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();
}Routing & Observability
Configure model selection, caching, and client error handling:
- Routing & fallbacks: Target specific models or use
nrouter/auto(see Router Settings). For per-call fallbacks, passnrouter_fallbacksinsidebody(see Per-Request Options). - FinOps telemetry: Headers include
x-nr-request-id,x-nr-model,x-nr-cost-status, andx-nr-request-cost. - Runtime support: Deploy to Edge Runtime (
export const runtime = "edge") or standard Node.js. - Error handling: Handle HTTP errors from the gateway (see Errors & Rate Limits):
// app/api/chat/route.ts
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: nrouter("gpt-5.4-mini"),
messages,
onError({ error }) {
console.error("Gateway stream error:", error);
},
});
return result.toDataStreamResponse();
}Next Steps
- Node.js SDK Guide — Official native TypeScript SDK
- Python & LangChain — Multi-agent and chain workflows in Python
- Chat Completions API — Direct HTTP wire specifications