Vercel AI SDK
Call OpenRelay models from the Vercel AI SDK with @ai-sdk/openai-compatible, including generateText, streamText, tools, structured output and embeddings.
The AI SDK calls OpenRelay through its OpenAI Compatible
provider, @ai-sdk/openai-compatible, pointed at OpenRelay's
Chat Completions and
Embeddings endpoints. There is no OpenRelay
package to install.
Install
npm install ai @ai-sdk/openai-compatible zodCreate an API key in the dashboard and export it as a server-side environment variable. Inference is prepaid, so add funds before the first request.
export OPENRELAY_API_KEY="or_your_api_key"Create the provider
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
const openrelay = createOpenAICompatible({
name: 'openrelay',
apiKey: process.env.OPENRELAY_API_KEY,
baseURL: 'https://inference.openrelay.inc/v1',
supportsStructuredOutputs: true,
});apiKey is sent as Authorization: Bearer <key>. supportsStructuredOutputs: true sends structured-output requests as response_format with type
json_schema. OpenRelay returns usage in the last chunk of every stream, so
includeUsage is not needed.
Pass an OpenRelay model id to the provider. Model ids carry the openrelay/
prefix: a bare id such as kimi-k3.1-pro returns 404 with model_not_found.
The examples below use the openrelay provider from this block.
Generate text
import { generateText } from 'ai';
const { text, usage } = await generateText({
model: openrelay('openrelay/deepseek-v4.1-flash'),
prompt: 'Explain what a context window is in two sentences.',
maxOutputTokens: 4096,
});
console.log(text);
console.log(usage);maxOutputTokens counts reasoning tokens too, so set it high enough for the
reasoning and the answer.
Stream text
import { streamText } from 'ai';
const result = streamText({
model: openrelay('openrelay/deepseek-v4.1-flash'),
prompt: 'Name three uses of a hash map.',
maxOutputTokens: 4096,
});
for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}
console.log();
console.log(await result.usage);Tool calling
import { generateText, isStepCount, tool } from 'ai';
import { z } from 'zod';
const result = await generateText({
model: openrelay('openrelay/kimi-k3.1-pro'),
tools: {
weather: tool({
description: 'Get the current temperature for a city',
inputSchema: z.object({ city: z.string() }),
execute: async ({ city }) => ({ city, temperatureCelsius: 18 }),
}),
},
stopWhen: isStepCount(3),
prompt: 'What is the temperature in Paris?',
maxOutputTokens: 4096,
});
console.log(result.text);The model calls weather, the SDK runs execute, and a second step answers
from the result. stopWhen caps the loop at three steps.
Structured output
import { generateText, Output } from 'ai';
import { z } from 'zod';
const { output } = await generateText({
model: openrelay('openrelay/deepseek-v4.1-flash'),
output: Output.object({
schema: z.object({
name: z.string(),
ingredients: z.array(z.string()),
}),
}),
prompt: 'Generate a simple pasta recipe.',
maxOutputTokens: 4096,
});
console.log(output);The request carries the schema as response_format with strict: true.
generateObject with the same schema also works, but AI SDK 7 marks it
deprecated in favor of generateText with output.
Reasoning
OpenRelay returns reasoning in reasoning_content, which the AI SDK maps to
reasoning parts, for example result.finalStep.reasoningText with
generateText. The reasoning setting is sent as reasoning_effort:
openrelay/kimi-k3.1-proandopenrelay/deepseek-v4.1-flashreason by default. Setreasoningto'none'(off),'low','high'or'max'to change it. Use one of these four values: the provider passes the setting through unchanged.openrelay/glm-5.3-flashalways reasons. Thereasoningsetting has no effect.openrelay/gemma-4-31bdoes not reason by default. Setreasoningto'low','high'or'max'to turn it on.
import { generateText } from 'ai';
const { text } = await generateText({
model: openrelay('openrelay/deepseek-v4.1-flash'),
reasoning: 'none',
prompt: 'Name three prime numbers.',
maxOutputTokens: 4096,
});
console.log(text);Embeddings
openrelay/bge-m3 returns 1,024-dimension vectors and accepts up to 8,192
input tokens.
import { embed } from 'ai';
const { embedding, usage } = await embed({
model: openrelay.embeddingModel('openrelay/bge-m3'),
value: 'sunny day at the beach',
});
console.log(embedding.length, embedding.slice(0, 3));
console.log(usage);Models
Tool calling and structured output work on openrelay/kimi-k3.1-pro,
openrelay/deepseek-v4.1-flash, openrelay/glm-5.3-flash and
openrelay/gemma-4-31b. Models lists every model id
with its context window and inputs, and Pricing has
the rates.
What to expect
- Prompt caching. The chat models cache prompt prefixes automatically, with
no request setting. Cached prompt tokens are reported in
usage.inputTokenDetails.cacheReadTokensand bill at the cached-input rate. - Penalties.
openrelay/kimi-k3.1-prorejectsfrequencyPenaltyandpresencePenaltywith a400, so leave them unset for that model.
Using OpenAI SDKs
Use the official OpenAI Python and Node libraries with OpenRelay by setting base_url and api_key, including streaming and environment variables.
Strands Agents
Run Strands Agents on OpenRelay models from Python or TypeScript through the SDK's OpenAI provider, with tools, streaming and structured output.