Mintplex-Labs/anything-llm · critical · Error

No Z.AI API key was set.

Error message

No Z.AI API key was set.

What it means

Thrown in the ZAiLLM constructor when process.env.ZAI_API_KEY is falsy. The provider cannot be built; no request to https://api.z.ai/api/paas/v4 is attempted. It is a hard startup gate before the OpenAI SDK client is constructed.

Source

Thrown at server/utils/AiProviders/zai/index.js:12

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

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

    this.openai = new OpenAIApi({
      baseURL: "https://api.z.ai/api/paas/v4",
      apiKey: process.env.ZAI_API_KEY,
    });
    this.model = modelPreference || process.env.ZAI_MODEL_PREF || "glm-4.5";
    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(
      `Initialized ${this.model} with context window ${this.promptWindowLimit()}`

View on GitHub (pinned to 526360e320)

Solutions

  1. Set ZAI_API_KEY in the running environment and restart the server.
  2. Use the exact name ZAI_API_KEY (no LLM substring, unlike xAI), and also set ZAI_MODEL_PREF (defaults to glm-4.5).
  3. Verify the var reaches the process by logging process.env.ZAI_API_KEY !== undefined at startup (log the boolean, never the value).
  4. Obtain a valid key from the Z.AI (Zhipu) PaaS console.
Defensive patterns

Strategy: validation

Validate before calling

function assertZaiEnv() {
  if (!process.env.ZAI_API_KEY) {
    throw new Error('Missing env ZAI_API_KEY — set it before constructing ZAiLLM');
  }
}
assertZaiEnv();

Try / catch

try {
  new ZAiLLM(embedder, modelPref);
} catch (e) {
  if (e.message === 'No Z.AI API key was set.') { /* surface config error */ }
  throw e;
}

Prevention

When it happens

Trigger: Constructing ZAiLLM while ZAI_API_KEY is unset/empty; selecting the Z.AI provider before provisioning the key; env var present in a different process (e.g. shell) than the server worker.

Common situations: Confusing ZAI_API_KEY with ZAI_MODEL_PREF or with a generic OPENAI_API_KEY; .env saved but process not restarted; CI/test runner that does not load dotenv; key removed during cleanup.

Related errors


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