{"record":{"id":"ad7cd5d90108b75d","repo":"Mintplex-Labs/anything-llm","slug":"no-cometapi-api-key-was-set","errorCode":null,"errorMessage":"No CometAPI API key was set.","messagePattern":"No CometAPI API key was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"server/utils/AiProviders/cometapi/index.js","lineNumber":25,"sourceCode":"} = require(\"../../helpers/chat/responses\");\nconst fs = require(\"fs\");\nconst path = require(\"path\");\nconst { safeJsonParse } = require(\"../../http\");\nconst {\n  LLMPerformanceMonitor,\n} = require(\"../../helpers/chat/LLMPerformanceMonitor\");\nconst { COMETAPI_IGNORE_PATTERNS } = require(\"./constants\");\nconst cacheFolder = path.resolve(\n  process.env.STORAGE_DIR\n    ? path.resolve(process.env.STORAGE_DIR, \"models\", \"cometapi\")\n    : path.resolve(__dirname, `../../../storage/models/cometapi`)\n);\n\nclass CometApiLLM {\n  defaultTimeout = 3_000;\n  constructor(embedder = null, modelPreference = null) {\n    if (!process.env.COMETAPI_LLM_API_KEY)\n      throw new Error(\"No CometAPI API key was set.\");\n\n    this.className = \"CometApiLLM\";\n    const { OpenAI: OpenAIApi } = require(\"openai\");\n    this.basePath = \"https://api.cometapi.com/v1\";\n    this.openai = new OpenAIApi({\n      baseURL: this.basePath,\n      apiKey: process.env.COMETAPI_LLM_API_KEY ?? null,\n      defaultHeaders: {\n        \"HTTP-Referer\": \"https://anythingllm.com\",\n        \"X-CometAPI-Source\": \"anythingllm\",\n      },\n    });\n    this.model =\n      modelPreference || process.env.COMETAPI_LLM_MODEL_PREF || \"gpt-5-mini\";\n    this.limits = {\n      history: this.promptWindowLimit() * 0.15,\n      system: this.promptWindowLimit() * 0.15,\n      user: this.promptWindowLimit() * 0.7,","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/cometapi/index.js#L7-L43","documentation":"Constructor guard that prevents CometApiLLM instantiation when COMETAPI_LLM_API_KEY is absent from the environment. Because the key is required to build the OpenAI-compatible client (baseURL https://api.cometapi.com/v1), the class cannot function without it, so construction fails fast.","triggerScenarios":"Instantiating `new CometApiLLM()` (typically via the provider factory) when COMETAPI_LLM_API_KEY is unset, empty string, or whitespace; selecting CometAPI as the LLM provider in AnythingLLM settings without having saved an API key.","commonSituations":"Fresh deployment where the CometAPI provider was selected but the key field was left blank; .env file missing the key after a config migration; CI/test environment that does not inject the key; copying env.example to .env but forgetting to fill in the CometAPI line.","solutions":["Set COMETAPI_LLM_API_KEY in the .env file (or system environment) to a valid key from the CometAPI dashboard.","Restart the server process after updating .env so the new env var is loaded.","In the AnythingLLM UI, re-enter the API key in the CometAPI provider settings and save."],"exampleFix":"# .env\nCOMETAPI_LLM_API_KEY=sk-your-cometapi-key-here","handlingStrategy":"validation","validationCode":"const COMETAPI_KEY = process.env.COMETAPI_LLM_API_KEY;\nif (!COMETAPI_KEY || COMETAPI_KEY.trim() === '')\n  throw new Error('COMETAPI_LLM_API_KEY must be set before using the CometAPI provider');","typeGuard":null,"tryCatchPattern":"// Wrap provider instantiation so a missing key does not crash the app\ntry {\n  provider = new CometApiLLM(embedder, modelPref);\n} catch (e) {\n  if (/No CometAPI API key/i.test(e.message))\n    return respondConfigError('Set COMETAPI_LLM_API_KEY in the CometAPI provider settings.');\n  throw e;\n}","preventionTips":["Check for the key before selecting CometAPI as the provider.","Use a startup health check that validates all configured provider env vars on boot.","Store the key in .env and verify it loads with dotenv before the server starts."],"tags":["cometapi","missing-api-key","constructor-guard","env-var"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}