Skip to content
Guides

Use CheapLLM with the OpenAI SDK

Connect the official OpenAI SDK to the CheapLLM-compatible API.

1. Install the SDK

In a Node.js project install the official openai package if it is not there yet. The examples use the JavaScript/TypeScript client. Note that the CheapLLM site is still a demonstration and the API does not accept requests yet; the configuration below is the target setup.

Terminal
pnpm add openai

2. Provide the key

Export the key before starting the app. The client below reads it from CHEAPLLM_API_KEY, so the value never appears in the source code.

Bash or Zsh
export CHEAPLLM_API_KEY="YOUR_CHEAPLLM_API_KEY"

3. Configure the client

Pass the CheapLLM base URL to the SDK and pick a model from the catalog. The SDK appends the Chat Completions path to that address.

client.ts
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.CHEAPLLM_API_KEY,
  baseURL: "https://api.cheapllm.com/v1",
});

4. Send a request

Call chat.completions.create for the OpenAI-compatible Chat Completions API. Replace MODEL_ID with an identifier that your key can use.

client.ts
const response = await client.chat.completions.create({
  model: "MODEL_ID",
  messages: [{ role: "user", content: "Hello!" }],
});

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