Skip to content
Guides

Use CheapLLM with LangChain

Point LangChain.js ChatOpenAI at the CheapLLM base URL.

1. Install the packages

For JavaScript/TypeScript install the OpenAI integration and the LangChain core package. 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 @langchain/openai @langchain/core

2. Provide the API key

Export the key before starting the app and read it from the environment. Do not place the secret directly in code or in configuration that ends up under version control.

Bash or Zsh
export CHEAPLLM_API_KEY="YOUR_CHEAPLLM_API_KEY"

3. Configure ChatOpenAI

Pass the model identifier, the key and the CheapLLM address to the ChatOpenAI constructor. The configuration.baseURL field takes the base URL including /v1.

model.ts
import { ChatOpenAI } from "@langchain/openai";

const model = new ChatOpenAI({
  model: "MODEL_ID",
  apiKey: process.env.CHEAPLLM_API_KEY,
  configuration: {
    baseURL: "https://api.cheapllm.com/v1",
  },
});

4. Send a request

Call the model with invoke. ChatOpenAI sends OpenAI Chat Completions requests; extra features depend on what the API and the chosen model support.

model.ts
const answer = await model.invoke("Explain in one sentence what an API is.");
console.log(answer.content);
All guides