Mintplex-Labs/anything-llm · critical · Error

No OpenAI API key was set.

Error message

No OpenAI API key was set.

What it means

Synchronous constructor guard. OpenAiLLM cannot be instantiated unless OPEN_AI_KEY is set; the key is required to construct the OpenAI client used for both chat and the models.retrieve validation call. Thrown at 'new OpenAiLLM(...)' before any network activity.

Source

Thrown at server/utils/AiProviders/openAi/index.js:16

const { v4: uuidv4 } = require("uuid");
const { NativeEmbedder } = require("../../EmbeddingEngines/native");
const { isAbortError } = require("../../helpers/abortSignals");
const {
  formatChatHistory,
  writeResponseChunk,
  clientAbortedHandler,
} = require("../../helpers/chat/responses");
const { MODEL_MAP } = require("../modelMap");
const {
  LLMPerformanceMonitor,
} = require("../../helpers/chat/LLMPerformanceMonitor");

class OpenAiLLM {
  constructor(embedder = null, modelPreference = null) {
    if (!process.env.OPEN_AI_KEY) throw new Error("No OpenAI API key was set.");
    this.className = "OpenAiLLM";
    const { OpenAI: OpenAIApi } = require("openai");

    this.openai = new OpenAIApi({
      apiKey: process.env.OPEN_AI_KEY,
    });
    this.model =
      modelPreference || process.env.OPEN_MODEL_PREF || "gpt-4.1-nano";
    this.limits = {
      history: this.promptWindowLimit() * 0.15,
      system: this.promptWindowLimit() * 0.15,
      user: this.promptWindowLimit() * 0.7,
    };

    this.embedder = embedder ?? new NativeEmbedder();
    this.defaultTemp = 0.7;
    this.log(
      `Initialized ${this.model} with context window ${this.promptWindowLimit()}`

View on GitHub (pinned to 526360e320)

Solutions

  1. Set OPEN_AI_KEY (note the underscore naming AnythingLLM uses) to a valid OpenAI API key in .env.
  2. Restart the server so the new key is loaded.
  3. If the key was revoked, generate a new one in the OpenAI dashboard first.
  4. Confirm the key has not been committed or truncated by shell quoting.

Example fix

# before
# (OPEN_AI_KEY unset, but OPENAI_API_KEY set)

# after - .env (use AnythingLLM's expected name)
OPEN_AI_KEY=sk-...
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.OPEN_AI_KEY || !process.env.OPEN_AI_KEY.trim())
  throw new Error('OPEN_AI_KEY must be set before starting OpenAiLLM.');

Prevention

When it happens

Trigger: Instantiating OpenAiLLM when process.env.OPEN_AI_KEY is undefined or empty.

Common situations: .env missing OPEN_AI_KEY; key var typo'd (OPENAI_API_KEY vs OPEN_AI_KEY); key revoked/rotated and removed; fresh deploy without the secret; secret not injected into the container.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/38167e19aa85aab4. Report an issue: GitHub.