Mintplex-Labs/anything-llm · critical · Error

No xAI API key was set.

Error message

No xAI API key was set.

What it means

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.

Source

Thrown at server/utils/AiProviders/xai/index.js:14

const { NativeEmbedder } = require("../../EmbeddingEngines/native");
const {
  LLMPerformanceMonitor,
} = require("../../helpers/chat/LLMPerformanceMonitor");
const {
  handleDefaultStreamResponseV2,
  formatChatHistory,
} = require("../../helpers/chat/responses");
const { MODEL_MAP } = require("../modelMap");

class XAiLLM {
  constructor(embedder = null, modelPreference = null) {
    if (!process.env.XAI_LLM_API_KEY)
      throw new Error("No xAI API key was set.");
    this.className = "XAiLLM";
    const { OpenAI: OpenAIApi } = require("openai");

    this.openai = new OpenAIApi({
      baseURL: "https://api.x.ai/v1",
      apiKey: process.env.XAI_LLM_API_KEY,
    });
    this.model =
      modelPreference || process.env.XAI_LLM_MODEL_PREF || "grok-beta";
    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(

View on GitHub (pinned to 526360e320)

Solutions

  1. Set XAI_LLM_API_KEY in the environment that runs the server (export or .env), then restart the process.
  2. Confirm the exact variable name is XAI_LLM_API_KEY (note the _LLM_ segment), not XAI_API_KEY.
  3. 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.
  4. Obtain a valid key from https://console.x.ai and replace any expired/placeholder value.
Defensive patterns

Strategy: validation

Validate before calling

function assertXaiEnv() {
  if (!process.env.XAI_LLM_API_KEY) {
    throw new Error('Missing env XAI_LLM_API_KEY — set it before constructing XAiLLM');
  }
}
assertXaiEnv();

Try / catch

try {
  new XAiLLM(embedder, modelPref);
} catch (e) {
  if (e.message === 'No xAI API key was set.') { /* surface config UI / 500 to caller */ }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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