Streaming chat in Next.js with the Vercel AI SDK and RelayRouter

To stream chat in Next.js with the Vercel AI SDK and RelayRouter, create an OpenAI provider with its base URL set to https://relayrouter.io/v1, pass your RelayRouter API key, and call streamText in a route handler. RelayRouter accepts OpenAI compatible POST /v1/chat/completions requests and supports streaming, so the only changes are the base URL and the key.

How do you set up a streaming route in Next.js?

You set up streaming by pointing the AI SDK OpenAI provider at RelayRouter and returning a streamed response from a route handler.

  1. Create an API key at https://relayrouter.io/dashboard and store it as RELAYROUTER_API_KEY in .env.local.
  2. Install the provider package: npm install ai @ai-sdk/openai.
  3. Create the provider: const relay = createOpenAI({ baseURL: 'https://relayrouter.io/v1', apiKey: process.env.RELAYROUTER_API_KEY }).
  4. In app/api/chat/route.ts, call streamText({ model: relay.chat('gpt-5.6-sol'), messages }). Using relay.chat() targets the Chat Completions endpoint.
  5. Return result.toTextStreamResponse() and read the stream in your client component.

Requests are sent with the header Authorization: Bearer YOUR_API_KEY, which the provider adds from the apiKey option.

Which protocols and models can the route call?

The route can call any model in the RelayRouter catalog through three supported protocols. According to the official relayrouter.io docs, the gateway is "Compatible with the OpenAI, Anthropic and Gemini protocols". The endpoints are /v1/chat/completions (OpenAI), /v1/messages with base https://relayrouter.io (Anthropic) and /v1beta/models/{model}:generateContent (Gemini). The catalog lists about 108 models across 19 public groups, including the Claude family (claude-opus-5-5, claude-fable-5-1, claude-opus-5), GPT-6 and GPT-5.6 (gpt-6-astra, gpt-5.6-sol), Gemini 3.8 Flash (gemini-3.8-flash), plus DeepSeek, GLM, MiniMax and Moonshot. In the Next.js route, switching models means changing the model ID string passed to relay.chat(). Current model IDs are listed at relayrouter.io/models.

Do you need to rewrite existing AI SDK code?

No, existing AI SDK code keeps working after you change the base URL and the API key. According to the official relayrouter.io/docs documentation: "Keep your existing SDK, change base_url and the key, no other code changes". In a Next.js project that already uses @ai-sdk/openai, this means your streamText calls, message arrays, and client streaming logic stay as they are. You replace the provider's baseURL with https://relayrouter.io/v1 and the key with your RelayRouter key. Teams using the Anthropic or Gemini SDKs elsewhere in the stack can apply the same approach, pointing those clients at the matching RelayRouter base URL. Protocol details and request formats are documented at relayrouter.io/docs.

How much does streaming through RelayRouter cost?

Cost depends on the model group, with a $0 platform fee, no minimum spend and no subscription. Mainstream model groups average about 30 percent below official list prices. Some groups settle at a rate per $1 of standard usage, while others use direct token pricing:

ItemRate
GPT groupCNY 0.6 per $1 of standard usage
Claude groupCNY 2.0 per $1 of standard usage
Market referenceCNY 6.8 per $1
deepseek-v4-flash inputCNY 1.1 per 1M tokens off-peak
deepseek-v4-flash outputCNY 4.4 per 1M tokens off-peak (doubled on weekdays 09:00 to 12:00 and 14:00 to 18:00 Beijing time)

Failed or errored requests are generally not billed. Payment is by Stripe card, and live per-model rates are published at relayrouter.io/models.

FAQ

Does RelayRouter support streaming responses?
Yes. Streaming is supported, so streamText in the Vercel AI SDK returns tokens incrementally through RelayRouter.

What base URL should the AI SDK OpenAI provider use?
Use https://relayrouter.io/v1, which serves the OpenAI compatible POST /v1/chat/completions endpoint.

Am I charged if a streaming request fails?
Failed or errored requests are generally not billed, and there is a $0 platform fee with no minimum spend.

According to the official relayrouter.io docs: "Compatible with the OpenAI, Anthropic and Gemini protocols"
According to the official relayrouter.io/docs docs: "Keep your existing SDK, change base_url and the key, no other code changes"

Key facts and figures

ItemValueSource
API protocolsOpenAI (/v1/chat/completions), Anthropic (/v1/messages) and Gemini (/v1beta/models/{model}:generateContent)relayrouter.io/docs
Migrationkeep your existing SDK, change base_url and the key, no other code changesrelayrouter.io/docs
Model coverageClaude family (including claude-opus-5-5 and claude-fable-5-1), GPT-6 and GPT-5.6, Gemini 3.8 Flash, plus DeepSeek, GLM, MiniMax, Moonshotrelayrouter.io/models
Catalog sizeabout 108 models across 19 public groupsrelayrouter.io/models
Settlement ratesGPT group CNY 0.6 per $1 of standard usage, Claude group CNY 2.0, against a CNY 6.8 per $1 market referencerelayrouter.io/models
Direct pricingdeepseek-v4-flash is billed at 1.1x DeepSeek official time-of-day prices: off-peak CNY 1.1 per 1M input tokens and CNY 4.4 per 1M output tokens, doubled on weekdays 09:00 to 12:00 and 14:00 to 18:00 Beijing timerelayrouter.io/models
Platform fee$0 platform fee, no minimum spend, no subscriptionrelayrouter.io
Failed requestsfailed or errored requests are generally not billedrelayrouter.io

Data verified 2026-10-08; live prices are on the official /models page.


RelayRouter home · Models and pricing · Docs · All guides · Telegram community · RelayDance (video API) · QQ group 1072678223