{"record":{"id":"38167e19aa85aab4","repo":"Mintplex-Labs/anything-llm","slug":"no-openai-api-key-was-set-38167e","errorCode":null,"errorMessage":"No OpenAI API key was set.","messagePattern":"No OpenAI API key was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"server/utils/AiProviders/openAi/index.js","lineNumber":16,"sourceCode":"const { v4: uuidv4 } = require(\"uuid\");\nconst { NativeEmbedder } = require(\"../../EmbeddingEngines/native\");\nconst { isAbortError } = require(\"../../helpers/abortSignals\");\nconst {\n  formatChatHistory,\n  writeResponseChunk,\n  clientAbortedHandler,\n} = require(\"../../helpers/chat/responses\");\nconst { MODEL_MAP } = require(\"../modelMap\");\nconst {\n  LLMPerformanceMonitor,\n} = require(\"../../helpers/chat/LLMPerformanceMonitor\");\n\nclass OpenAiLLM {\n  constructor(embedder = null, modelPreference = null) {\n    if (!process.env.OPEN_AI_KEY) throw new Error(\"No OpenAI API key was set.\");\n    this.className = \"OpenAiLLM\";\n    const { OpenAI: OpenAIApi } = require(\"openai\");\n\n    this.openai = new OpenAIApi({\n      apiKey: process.env.OPEN_AI_KEY,\n    });\n    this.model =\n      modelPreference || process.env.OPEN_MODEL_PREF || \"gpt-4.1-nano\";\n    this.limits = {\n      history: this.promptWindowLimit() * 0.15,\n      system: this.promptWindowLimit() * 0.15,\n      user: this.promptWindowLimit() * 0.7,\n    };\n\n    this.embedder = embedder ?? new NativeEmbedder();\n    this.defaultTemp = 0.7;\n    this.log(\n      `Initialized ${this.model} with context window ${this.promptWindowLimit()}`","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/openAi/index.js#L1-L34","documentation":"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.","triggerScenarios":"Instantiating OpenAiLLM when process.env.OPEN_AI_KEY is undefined or empty.","commonSituations":".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.","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."],"exampleFix":"# before\n# (OPEN_AI_KEY unset, but OPENAI_API_KEY set)\n\n# after - .env (use AnythingLLM's expected name)\nOPEN_AI_KEY=sk-...","handlingStrategy":"validation","validationCode":"if (!process.env.OPEN_AI_KEY || !process.env.OPEN_AI_KEY.trim())\n  throw new Error('OPEN_AI_KEY must be set before starting OpenAiLLM.');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["openai","configuration","env","authentication"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}