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
- Set OPEN_AI_KEY (note the underscore naming AnythingLLM uses) to a valid OpenAI API key in .env.
- Restart the server so the new key is loaded.
- If the key was revoked, generate a new one in the OpenAI dashboard first.
- 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
- Use AnythingLLM's exact env name OPEN_AI_KEY (not OPENAI_API_KEY).
- Validate required secrets at boot and fail fast.
- Inject the secret via the container environment, never commit it.
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
- No OpenRouter API key was set.
- Community Hub connection key not found
- No Anthropic API key was set.
- No ApiPie LLM API key was set.
- No Azure API key was set.
AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13).
Data as JSON: /api/errors/38167e19aa85aab4.
Report an issue: GitHub.