{"record":{"id":"9136c828aae889dd","repo":"Mintplex-Labs/anything-llm","slug":"no-z-ai-api-key-was-set","errorCode":null,"errorMessage":"No Z.AI API key was set.","messagePattern":"No Z\\.AI API key was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"server/utils/AiProviders/zai/index.js","lineNumber":12,"sourceCode":"const { NativeEmbedder } = require(\"../../EmbeddingEngines/native\");\nconst {\n  LLMPerformanceMonitor,\n} = require(\"../../helpers/chat/LLMPerformanceMonitor\");\nconst {\n  handleDefaultStreamResponseV2,\n} = require(\"../../helpers/chat/responses\");\nconst { MODEL_MAP } = require(\"../modelMap\");\n\nclass ZAiLLM {\n  constructor(embedder = null, modelPreference = null) {\n    if (!process.env.ZAI_API_KEY) throw new Error(\"No Z.AI API key was set.\");\n    this.className = \"ZAiLLM\";\n    const { OpenAI: OpenAIApi } = require(\"openai\");\n\n    this.openai = new OpenAIApi({\n      baseURL: \"https://api.z.ai/api/paas/v4\",\n      apiKey: process.env.ZAI_API_KEY,\n    });\n    this.model = modelPreference || process.env.ZAI_MODEL_PREF || \"glm-4.5\";\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(\n      `Initialized ${this.model} with context window ${this.promptWindowLimit()}`","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/zai/index.js#L1-L30","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set ZAI_API_KEY in the running environment and restart the server.","Use the exact name ZAI_API_KEY (no LLM substring, unlike xAI), and also set ZAI_MODEL_PREF (defaults to glm-4.5).","Verify the var reaches the process by logging process.env.ZAI_API_KEY !== undefined at startup (log the boolean, never the value).","Obtain a valid key from the Z.AI (Zhipu) PaaS console."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"function assertZaiEnv() {\n  if (!process.env.ZAI_API_KEY) {\n    throw new Error('Missing env ZAI_API_KEY — set it before constructing ZAiLLM');\n  }\n}\nassertZaiEnv();","typeGuard":null,"tryCatchPattern":"try {\n  new ZAiLLM(embedder, modelPref);\n} catch (e) {\n  if (e.message === 'No Z.AI API key was set.') { /* surface config error */ }\n  throw e;\n}","preventionTips":["Use the exact env name ZAI_API_KEY (no _LLM_ segment, unlike xAI).","Also set ZAI_MODEL_PREF (defaults to glm-4.5).","Restart after editing .env; verify the boolean presence at startup."],"tags":["zai","zhipu","glm","config","env","auth","startup"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}