Mintplex-Labs/anything-llm · error · Error

xAI API Key must be provided to use agents.

Error message

xAI API Key must be provided to use agents.

What it means

Thrown by AgentHandler.checkSetup() (server/utils/agents/index.js:218) when the workspace agent provider is 'xai' but XAI_LLM_API_KEY is unset/empty. xAI (Grok) exposes its API behind this single key, which the guard validates before the agent session is created.

Source

Thrown at server/utils/agents/index.js:219

          );
        break;
      case "deepseek":
        if (!process.env.DEEPSEEK_API_KEY)
          throw new Error("DeepSeek API Key must be provided to use agents.");
        break;
      case "litellm":
        if (!process.env.LITE_LLM_BASE_PATH)
          throw new Error(
            "LiteLLM API base path and key must be provided to use agents."
          );
        break;
      case "apipie":
        if (!process.env.APIPIE_LLM_API_KEY)
          throw new Error("ApiPie API Key must be provided to use agents.");
        break;
      case "xai":
        if (!process.env.XAI_LLM_API_KEY)
          throw new Error("xAI API Key must be provided to use agents.");
        break;
      case "zai":
        if (!process.env.ZAI_API_KEY)
          throw new Error("Z.AI API Key must be provided to use agents.");
        break;
      case "novita":
        if (!process.env.NOVITA_LLM_API_KEY)
          throw new Error("Novita API Key must be provided to use agents.");
        break;
      case "nvidia-nim":
        if (!process.env.NVIDIA_NIM_LLM_BASE_PATH)
          throw new Error(
            "NVIDIA NIM base path must be provided to use agents."
          );
        break;
      case "ppio":
        if (!process.env.PPIO_API_KEY)
          throw new Error("PPIO API Key must be provided to use agents.");

View on GitHub (pinned to 526360e320)

Solutions

  1. Create an API key at https://console.x.ai and set XAI_LLM_API_KEY in .env.
  2. Restart AnythingLLM to reload process.env.
  3. Validate with curl -H 'Authorization: Bearer $XAI_LLM_API_KEY' https://api.x.ai/v1/models.
  4. Confirm the agent model is a valid xAI model id (e.g. grok-beta).

Example fix

// before (.env)
// XAI_LLM_API_KEY=

// after (.env)
XAI_LLM_API_KEY=xai-...your-key
Defensive patterns

Strategy: validation

Validate before calling

const key = process.env.XAI_LLM_API_KEY;
if (!key) {
  throw new Error(
    'Cannot start xAI agent: set XAI_LLM_API_KEY (from console.x.ai) in .env.'
  );
}

Try / catch

try {
  await agentHandler.start();
} catch (e) {
  if (/xAI API Key must be provided/.test(e.message)) {
    return respondConfigError('xai', ['XAI_LLM_API_KEY']);
  }
  throw e;
}

Prevention

When it happens

Trigger: An agent invocation runs with workspace.agentProvider === 'xai' while XAI_LLM_API_KEY is absent from process.env. Path: #providerSetupAndCheck() -> checkSetup() at index.js:476.

Common situations: xAI selected in System > LLM Preference without a key; key from console.x.ai not added to .env; .env edited but the server not restarted; key revoked after rotation.

Related errors


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