Guides

How to use the OpenAI SDK with LLMBase

Connect an OpenAI SDK chat-completions client to the LLMBase Inference API with the correct base URL, key, model ID, and production safeguards.

Guides

If an application already uses the OpenAI SDK for chat completions, connecting it to LLMBase is a small configuration change: use the LLMBase base URL, an LLMBase inference API key, and a model ID from the LLMBase catalog. The important part is being precise about which compatibility surface and which key type you are using.

LLMBase documents an OpenAI-compatible chat-completions and models API. It does not claim compatibility with every vendor-specific endpoint or field, so treat the documented request surface as the contract for an integration.

1. Install the SDK and set the key on your server

Install the official JavaScript SDK in your server-side application:

npm install openai

Create an LLMBase inference API key in the dashboard and store it in a server-side environment variable. Inference keys use the llmbase_... prefix and spend prepaid inference credits. Do not put an API key in browser code, a mobile bundle, a public repository, or a client-side environment variable.

LLMBASE_API_KEY=llmbase_...

The key for direct inference is different from a chat-agent key. Use the direct Inference API documentation when your application sends requests to https://api.llmbase.ai/v1.

2. Create an OpenAI client with the LLMBase base URL

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.llmbase.ai/v1",
  apiKey: process.env.LLMBASE_API_KEY,
});

The /v1 suffix is part of the SDK base URL. The client sends the normal Bearer authorization header using the key you provide.

3. Use a current LLMBase model ID

Ask the API for the current model list instead of copying an unverified identifier from a blog post or an old example:

const models = await client.models.list();

for (const model of models.data) {
  console.log(model.id);
}

Pick the ID that matches the capabilities your request needs, then make a normal chat-completions request:

const response = await client.chat.completions.create({
  model: "deepseek/deepseek-v4-flash",
  messages: [
    {
      role: "system",
      content: "You are a concise product-support assistant.",
    },
    {
      role: "user",
      content: "Explain how to reset a password in three steps.",
    },
  ],
  max_tokens: 250,
});

console.log(response.choices[0]?.message?.content);

Replace the example model ID with the model your application has evaluated. For a production integration, read the current model metadata before using advanced capabilities such as tool calls, structured outputs, reasoning controls, image inputs, or prompt caching.

4. Stream only when the product needs it

Streaming is useful for an interactive interface because a person can begin reading before the complete answer is ready. It is not automatically the best choice for a background job or a strict JSON pipeline.

const stream = await client.chat.completions.create({
  model: "deepseek/deepseek-v4-flash",
  messages: [{ role: "user", content: "Write a haiku about clean APIs." }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

Keep the server responsible for the stream and pass only the permitted result to the client. Log request identifiers and non-sensitive error context, not prompts or keys indiscriminately.

5. Build for the unhappy path

A working demo is not yet a reliable integration. Set a sensible output limit, handle timeouts and documented errors, and show a clear retry option where retrying is appropriate. If an API request depends on a feature, select a model that explicitly advertises that feature rather than hoping it will be accepted.

Keep a small integration test that performs one non-streaming completion and, if your product uses it, one streaming completion. Use a model ID returned by the catalog and validate the response shape your application depends on.

The three migration changes to remember

For most existing OpenAI SDK chat-completions clients, the essentials are:

  1. Set baseURL to https://api.llmbase.ai/v1.
  2. Use an llmbase_... inference API key in apiKey.
  3. Select a current LLMBase model ID and use only documented request fields.

For endpoint details, supported parameters, and troubleshooting, use the Quickstart, OpenAI compatibility, and Chat completions reference. They are the source of truth for an implementation, while this guide is the shortest path to a clean first integration.

EUMade in Europe

Chat with 20+ AI Models in one App.

Use Claude, ChatGPT, Gemini alongside EU-hosted models and many more.

Get the App:

Download on the App StoreGet it on Google Play