Use the Vercel AI SDK's OpenAI-compatible provider to generate text through RedRouter. This recipe uses a custom provider instance and has not been live-tested against RedRouter.
Install and generate text
npm install ai @ai-sdk/openai-compatible
export REDROUTER_API_KEY="your-redrouter-api-key"
Save as example.mjs:
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText } from "ai";
const apiKey = process.env.REDROUTER_API_KEY;
if (!apiKey) throw new Error("Set REDROUTER_API_KEY");
const redrouter = createOpenAICompatible({
name: "redrouter",
baseURL: "https://redrouter.org/v1",
apiKey,
includeUsage: true,
});
try {
const result = await generateText({
model: redrouter("red-cyber-1"),
prompt: "Write a two-sentence explanation of a database transaction.",
maxOutputTokens: 512,
maxRetries: 0,
abortSignal: AbortSignal.timeout(120_000),
});
console.log(result.text);
console.error(result.finishReason, result.usage);
if (result.finishReason !== "stop") process.exitCode = 1;
} catch {
console.error("Generation failed; inspect the API error in your server's error handler.");
process.exitCode = 1;
}
Run node example.mjs. Expected result: text on stdout and completion metadata on stderr. The provider appends /chat/completions; retain /v1 in baseURL. This direct provider instance avoids routing a model string through a different default gateway.
Stream text
Import streamText from ai, then use the same provider:
const result = streamText({
model: redrouter("red-cyber-1"),
prompt: "Describe three properties of a reliable queue.",
maxOutputTokens: 1024,
maxRetries: 0,
abortSignal: AbortSignal.timeout(120_000),
onError: () => { process.exitCode = 1; },
});
for await (const text of result.textStream) process.stdout.write(text);
console.error(await result.finishReason, await result.usage);
if (await result.finishReason !== "stop") process.exitCode = 1;
Place this block in the same error-handling boundary as the first request. Streaming errors can arrive after text has already been shown. Preserve the incomplete state in your UI; do not turn a partial answer into a saved success. See streaming.
Use in a web application
Create the provider in server code. Keep REDROUTER_API_KEY in a server-only environment variable, without a public client prefix. Authenticate callers, bound input/output, and propagate an abort signal from the incoming request. For AI SDK UI, use the response helper supported by your installed major version; raw RedRouter SSE and an AI SDK UI stream use different framing.
red-cyber-1 supports tools and structured output but is text-only. The compatible provider also exposes SDK methods for capabilities that RedRouter does not offer, including embeddings and image generation. Choose only supported features, validate tool arguments, and follow production guidance for retries and budgets.
References: AI SDK compatible provider and streamText reference.