Mintplex-Labs/anything-llm · critical · Error

OpenAI API key must be provided to use agents.

Error message

OpenAI API key must be provided to use agents.

What it means

AgentInvocations.checkSetup() (server/utils/agents/index.js:128-133) validates required env vars per provider before any agent runs. For provider === "openai" it throws if process.env.OPEN_AI_KEY is falsy, preventing a guaranteed-401 call to the OpenAI API.

Source

Thrown at server/utils/agents/index.js:132

            from: WORKSPACE_AGENT.name,
            to: USER_AGENT.name,
            content: response?.text || "",
            state: "success",
          }
        );
      });
      return agentHistory;
    } catch (e) {
      this.log("Error loading chat history", e.message);
      return [];
    }
  }

  checkSetup() {
    switch (this.provider) {
      case "openai":
        if (!process.env.OPEN_AI_KEY)
          throw new Error("OpenAI API key must be provided to use agents.");
        break;
      case "anthropic":
        if (!process.env.ANTHROPIC_API_KEY)
          throw new Error("Anthropic API key must be provided to use agents.");
        break;
      case "lmstudio":
        if (!process.env.LMSTUDIO_BASE_PATH)
          throw new Error("LMStudio base path must be provided to use agents.");
        break;
      case "ollama":
        if (!process.env.OLLAMA_BASE_PATH)
          throw new Error("Ollama base path must be provided to use agents.");
        break;
      case "groq":
        if (!process.env.GROQ_API_KEY)
          throw new Error("Groq API key must be provided to use agents.");
        break;
      case "togetherai":

View on GitHub (pinned to 526360e320)

Solutions

  1. Set OPEN_AI_KEY in the server .env to a valid OpenAI API key.
  2. Restart the server process so the new env is loaded.
  3. If running in Docker, pass the key via -e or the compose env block.
  4. Confirm the key has not been revoked at platform.openai.com.

Example fix

# before (.env)
# OPEN_AI_KEY=

# after (.env)
OPEN_AI_KEY=sk-...validkey...
Defensive patterns

Strategy: validation

Validate before calling

function openAiAgentReady() {
  return Boolean(process.env.OPEN_AI_KEY && process.env.OPEN_AI_KEY.trim());
}
if (provider === "openai" && !openAiAgentReady())
  throw new Error("Set OPEN_AI_KEY before enabling the openai agent.");

Try / catch

try {
  agent.checkSetup();
} catch (e) {
  if (/OPEN_AI_KEY|API key must be provided/i.test(e.message)) {
    // surface a user-actionable config error, do not retry
    throw new Error("OpenAI agent is not configured: set OPEN_AI_KEY in .env and restart.");
  }
  throw e;
}

Prevention

When it happens

Trigger: Selecting the openai agent provider while OPEN_AI_KEY is unset/empty in the process environment (missing .env, env not reloaded after edit, or container missing the var).

Common situations: Fresh deployment without setting OPEN_AI_KEY, a typo in the .env key name, or a Docker container that did not receive the secret.

Related errors


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