{"record":{"id":"0fb08cec9236e454","repo":"Mintplex-Labs/anything-llm","slug":"openai-api-key-must-be-provided-to-use-agents","errorCode":null,"errorMessage":"OpenAI API key must be provided to use agents.","messagePattern":"OpenAI API key must be provided to use agents\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"server/utils/agents/index.js","lineNumber":132,"sourceCode":"            from: WORKSPACE_AGENT.name,\n            to: USER_AGENT.name,\n            content: response?.text || \"\",\n            state: \"success\",\n          }\n        );\n      });\n      return agentHistory;\n    } catch (e) {\n      this.log(\"Error loading chat history\", e.message);\n      return [];\n    }\n  }\n\n  checkSetup() {\n    switch (this.provider) {\n      case \"openai\":\n        if (!process.env.OPEN_AI_KEY)\n          throw new Error(\"OpenAI API key must be provided to use agents.\");\n        break;\n      case \"anthropic\":\n        if (!process.env.ANTHROPIC_API_KEY)\n          throw new Error(\"Anthropic API key must be provided to use agents.\");\n        break;\n      case \"lmstudio\":\n        if (!process.env.LMSTUDIO_BASE_PATH)\n          throw new Error(\"LMStudio base path must be provided to use agents.\");\n        break;\n      case \"ollama\":\n        if (!process.env.OLLAMA_BASE_PATH)\n          throw new Error(\"Ollama base path must be provided to use agents.\");\n        break;\n      case \"groq\":\n        if (!process.env.GROQ_API_KEY)\n          throw new Error(\"Groq API key must be provided to use agents.\");\n        break;\n      case \"togetherai\":","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/agents/index.js#L114-L150","documentation":"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.","triggerScenarios":"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).","commonSituations":"Fresh deployment without setting OPEN_AI_KEY, a typo in the .env key name, or a Docker container that did not receive the secret.","solutions":["Set OPEN_AI_KEY in the server .env to a valid OpenAI API key.","Restart the server process so the new env is loaded.","If running in Docker, pass the key via -e or the compose env block.","Confirm the key has not been revoked at platform.openai.com."],"exampleFix":"# before (.env)\n# OPEN_AI_KEY=\n\n# after (.env)\nOPEN_AI_KEY=sk-...validkey...","handlingStrategy":"validation","validationCode":"function openAiAgentReady() {\n  return Boolean(process.env.OPEN_AI_KEY && process.env.OPEN_AI_KEY.trim());\n}\nif (provider === \"openai\" && !openAiAgentReady())\n  throw new Error(\"Set OPEN_AI_KEY before enabling the openai agent.\");","typeGuard":null,"tryCatchPattern":"try {\n  agent.checkSetup();\n} catch (e) {\n  if (/OPEN_AI_KEY|API key must be provided/i.test(e.message)) {\n    // surface a user-actionable config error, do not retry\n    throw new Error(\"OpenAI agent is not configured: set OPEN_AI_KEY in .env and restart.\");\n  }\n  throw e;\n}","preventionTips":["Run checkSetup() at server boot so missing keys fail fast with a clear message.","Restart the process after editing .env; env is read once at startup.","Pass secrets via compose env / -e in Docker rather than baking them in.","Validate keys are non-empty (not just present) before enabling a provider."],"tags":["openai","config","env","auth","agents"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}