Skip to main content

Use Vercel AI SDK

Use @ai-sdk/openai-compatible for Primex Chat Completions. This explicitly selects the compatible chat path; other provider packages can default to a different API. Choose an eligible model in Vercel AI SDK Setup.

Install and run​

Shell
npm install ai @ai-sdk/openai-compatible
export PRIMEX_API_KEY=YOUR_API_KEY
export PRIMEX_CHAT_MODEL=YOUR_LIVE_CHAT_MODEL

Save as example.mjs and run node example.mjs on your server:

JavaScript
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';

const apiKey = process.env.PRIMEX_API_KEY;
const modelId = process.env.PRIMEX_CHAT_MODEL;
if (!apiKey || !modelId) throw new Error('Set PRIMEX_API_KEY and PRIMEX_CHAT_MODEL');
const primex = createOpenAICompatible({
name: 'primex', apiKey,
baseURL: 'https://gateway.primexos.com/v1',
});
const { text } = await generateText({
model: primex(modelId), prompt: 'Hello from Primex', maxRetries: 0,
});
console.log(text);

The answer prints in your terminal. In Next.js, keep this provider in a server route/action; send only the answer to the browser. Never use a public-prefixed environment variable for the key or initialize this client in browser code.

Stream text​

In the same script, replace generateText with streamText in the import and replace the final call/output with:

JavaScript
const result = streamText({
model: primex(modelId), prompt: 'Hello from Primex', maxRetries: 0,
});
for await (const part of result.textStream) process.stdout.write(part);
await result.finishReason;
process.stdout.write('\n');

Confirm a complete final response; partial text is not proof of success. Usage options, structured outputs, tool execution and multi-part tool results must match the selected model/protocol. Do not enable provider capability switches solely because the SDK exposes them. SDK token metadata is not a billing receipt; inspect Usage.

Common fixes​

  • 401: set the server process environment and restart it after changes.
  • 404: use /v1, the exact offering ID, and the compatible chat provider.
  • Unsupported option: remove it and review API formats.
  • Timeout/stream interruption: inspect Usage before retrying; Errors & limits describes uncertain outcomes.

To disconnect, remove the client configuration and revoke unused keys.

See the official provider reference for additional client settings.