Mintplex-Labs/anything-llm · critical · Error

No OpenAI API key was set.

Error message

No OpenAI API key was set.

What it means

Thrown by the OpenAiEmbedder constructor when process.env.OPEN_AI_KEY is falsy. The openai SDK client is built with only the apiKey (no custom baseURL), so an empty key means every request would 401; construction aborts immediately. The default model falls back to text-embedding-ada-002 but the key has no default.

Source

Thrown at server/utils/EmbeddingEngines/openAi/index.js:5

const { toChunks, reportEmbeddingProgress } = require("../../helpers");

class OpenAiEmbedder {
  constructor() {
    if (!process.env.OPEN_AI_KEY) throw new Error("No OpenAI API key was set.");
    this.className = "OpenAiEmbedder";
    const { OpenAI: OpenAIApi } = require("openai");
    this.openai = new OpenAIApi({
      apiKey: process.env.OPEN_AI_KEY,
    });
    this.model = process.env.EMBEDDING_MODEL_PREF || "text-embedding-ada-002";

    // Limit of how many strings we can process in a single pass to stay with resource or network limits
    this.maxConcurrentChunks = 500;

    // https://platform.openai.com/docs/guides/embeddings/embedding-models
    this.embeddingMaxChunkLength = 8_191;
  }

  log(text, ...args) {
    console.log(`\x1b[36m[${this.className}]\x1b[0m ${text}`, ...args);
  }

View on GitHub (pinned to 526360e320)

Solutions

  1. Set OPEN_AI_KEY (note the underscore form AnythingLLM expects) in .env to a valid key
  2. Confirm the key is active and funded at platform.openai.com
  3. Reload AnythingLLM env after the edit

Example fix

// before
// OPEN_AI_KEY unset (common mistake: using OPENAI_API_KEY instead)

// after
OPEN_AI_KEY=sk-...
EMBEDDING_MODEL_PREF=text-embedding-3-small
Defensive patterns

Strategy: validation

Validate before calling

function hasOpenAiKey(env = process.env) {
  return typeof env.OPEN_AI_KEY === 'string' &&
         env.OPEN_AI_KEY.trim().length > 0;
}
if (!hasOpenAiKey()) {
  throw new Error('Missing OPEN_AI_KEY (note the underscore form AnythingLLM uses).');
}

Type guard

function isOpenAiKey(v) {
  return typeof v === 'string' && /^sk-[A-Za-z0-9_-]{20,}$/.test(v);
}

Prevention

When it happens

Trigger: Selecting the OpenAI embedding engine while OPEN_AI_KEY is unset/empty; constructing `new OpenAiEmbedder()` before env load. The check on line 5 precedes client creation.

Common situations: Fresh OpenAI setup; key revoked/rotated on platform.openai.com but not in .env; secret not injected into the container/CI; env var typo (OPENAI_API_KEY vs OPEN_AI_KEY — AnythingLLM uses the underscore form); billing paused so the key was deactivated.

Related errors


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