{"record":{"id":"95fcad62541b2350","repo":"Mintplex-Labs/anything-llm","slug":"no-xai-api-key-was-set","errorCode":null,"errorMessage":"No xAI API key was set.","messagePattern":"No xAI API key was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"server/utils/AiProviders/xai/index.js","lineNumber":14,"sourceCode":"const { NativeEmbedder } = require(\"../../EmbeddingEngines/native\");\nconst {\n  LLMPerformanceMonitor,\n} = require(\"../../helpers/chat/LLMPerformanceMonitor\");\nconst {\n  handleDefaultStreamResponseV2,\n  formatChatHistory,\n} = require(\"../../helpers/chat/responses\");\nconst { MODEL_MAP } = require(\"../modelMap\");\n\nclass XAiLLM {\n  constructor(embedder = null, modelPreference = null) {\n    if (!process.env.XAI_LLM_API_KEY)\n      throw new Error(\"No xAI API key was set.\");\n    this.className = \"XAiLLM\";\n    const { OpenAI: OpenAIApi } = require(\"openai\");\n\n    this.openai = new OpenAIApi({\n      baseURL: \"https://api.x.ai/v1\",\n      apiKey: process.env.XAI_LLM_API_KEY,\n    });\n    this.model =\n      modelPreference || process.env.XAI_LLM_MODEL_PREF || \"grok-beta\";\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(","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/xai/index.js#L1-L32","documentation":"Thrown in the XAiLLM constructor when process.env.XAI_LLM_API_KEY is falsy. The provider cannot be instantiated at all; no grok request will be attempted. It is a hard startup gate set before the OpenAI SDK client is built against https://api.x.ai/v1.","triggerScenarios":"Instantiating new XAiLLM(...) while XAI_LLM_API_KEY is unset, empty string, or only set in a different environment than the running process; selecting the xAI provider in the UI before the key is provisioned; .env edited but the process was not restarted so the var is not loaded.","commonSituations":"Key stored under a different name (e.g. XAI_API_KEY) than the expected XAI_LLM_API_KEY; container orchestration that strips underscores; dotenv file not loaded in the test/worker process; rotating a revoked key and forgetting to redeploy.","solutions":["Set XAI_LLM_API_KEY in the environment that runs the server (export or .env), then restart the process.","Confirm the exact variable name is XAI_LLM_API_KEY (note the _LLM_ segment), not XAI_API_KEY.","If using a process manager or Docker, verify the var is present in that process's env via a startup log, not just your shell.","Obtain a valid key from https://console.x.ai and replace any expired/placeholder value."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"function assertXaiEnv() {\n  if (!process.env.XAI_LLM_API_KEY) {\n    throw new Error('Missing env XAI_LLM_API_KEY — set it before constructing XAiLLM');\n  }\n}\nassertXaiEnv();","typeGuard":null,"tryCatchPattern":"try {\n  new XAiLLM(embedder, modelPref);\n} catch (e) {\n  if (e.message === 'No xAI API key was set.') { /* surface config UI / 500 to caller */ }\n  throw e;\n}","preventionTips":["Use the exact env name XAI_LLM_API_KEY (with the _LLM_ segment).","Log process.env.XAI_LLM_API_KEY !== undefined at startup (boolean only).","Restart the process after editing .env."],"tags":["xai","grok","config","env","auth","startup"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}