{"record":{"id":"cc58cb4d950058b0","repo":"Mintplex-Labs/anything-llm","slug":"no-cohere-api-key-was-set","errorCode":null,"errorMessage":"No Cohere API key was set.","messagePattern":"No Cohere API key was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"server/utils/AiProviders/cohere/index.js","lineNumber":14,"sourceCode":"const { NativeEmbedder } = require(\"../../EmbeddingEngines/native\");\nconst { MODEL_MAP } = require(\"../modelMap\");\nconst {\n  LLMPerformanceMonitor,\n} = require(\"../../helpers/chat/LLMPerformanceMonitor\");\nconst {\n  handleDefaultStreamResponseV2,\n} = require(\"../../helpers/chat/responses\");\n\nclass CohereLLM {\n  constructor(embedder = null, modelPreference = null) {\n    const { OpenAI: OpenAIApi } = require(\"openai\");\n    if (!process.env.COHERE_API_KEY)\n      throw new Error(\"No Cohere API key was set.\");\n    this.className = \"CohereLLM\";\n\n    // Cohere exposes an OpenAI-compatible API which lets us reuse the OpenAI SDK\n    // across the app instead of the cohere-ai package. https://docs.cohere.com/docs/compatibility-api\n    this.openai = new OpenAIApi({\n      baseURL: \"https://api.cohere.ai/compatibility/v1\",\n      apiKey: process.env.COHERE_API_KEY,\n    });\n    this.model = modelPreference || process.env.COHERE_MODEL_PREF;\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(","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/cohere/index.js#L1-L32","documentation":"Thrown by the CohereLLM constructor when process.env.COHERE_API_KEY is falsy. AnythingLLM uses Cohere's OpenAI-compatible endpoint (https://api.cohere.ai/compatibility/v1) via the OpenAI SDK, which still requires the Cohere API key as apiKey. The guard fails fast so no call ever goes out unauthenticated.","triggerScenarios":"Constructing CohereLLM with COHERE_API_KEY unset. The constructor then sets className, builds the OpenAI client against the compatibility baseURL, and resolves this.model from COHERE_MODEL_PREF — all blocked by the guard.","commonSituations":"Cohere provider selected but key not entered; key stored under COHERE_KEY; .env updated but server not restarted; trial key expired and removed.","solutions":["Set `COHERE_API_KEY=<your cohere key>` in .env and restart the server.","Re-save the Cohere provider credentials in the UI to persist the key.","Optionally set COHERE_MODEL_PREF to a specific model; otherwise this.model stays undefined and later calls may need it.","Verify with `printenv COHERE_API_KEY` in the running environment."],"exampleFix":"// before\n// .env\nCOHERE_API_KEY=\n\n// after\n// .env\nCOHERE_API_KEY=<cohere-key>\nCOHERE_MODEL_PREF=command-r-plus","handlingStrategy":"validation","validationCode":"if (!process.env.COHERE_API_KEY) {\n  throw new Error(\n    \"COHERE_API_KEY is missing. Generate one in the Cohere dashboard before selecting this provider.\"\n  );\n}\nconst llm = new CohereLLM(embedder, modelPref);","typeGuard":"/** @param {unknown} v @returns {boolean} */\nfunction isNonEmptyKey(v) {\n  return typeof v === \"string\" && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  const llm = new CohereLLM(embedder, modelPref);\n} catch (e) {\n  if (/No Cohere API key/.test(e.message)) {\n    return { ok: false, reason: \"missing-cohere-key\" };\n  }\n  throw e;\n}","preventionTips":["Validate COHERE_API_KEY at provider-save time and mark the provider unready until set.","Use the exact env var name from a shared constants file.","Restart the server after credential edits so dotenv reloads.","Optionally set COHERE_MODEL_PREF to a concrete model; otherwise this.model is undefined."],"tags":["config","env","authentication","cohere","constructor"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}