{"record":{"id":"8fe914fe8d7dd046","repo":"rohitg00/agentmemory","slug":"cohere-api-key-is-required","errorCode":null,"errorMessage":"COHERE_API_KEY is required","messagePattern":"COHERE_API_KEY is required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/providers/embedding/cohere.ts","lineNumber":14,"sourceCode":"import type { EmbeddingProvider } from \"../../types.js\";\nimport { getEnvVar } from \"../../config.js\";\nimport { fetchWithTimeout } from \"../_fetch.js\";\n\nconst API_URL = \"https://api.cohere.ai/v1/embed\";\n\nexport class CohereEmbeddingProvider implements EmbeddingProvider {\n  readonly name = \"cohere\";\n  readonly dimensions = 1024;\n  private apiKey: string;\n\n  constructor(apiKey?: string) {\n    this.apiKey = apiKey || getEnvVar(\"COHERE_API_KEY\") || \"\";\n    if (!this.apiKey) throw new Error(\"COHERE_API_KEY is required\");\n  }\n\n  async embed(text: string): Promise<Float32Array> {\n    const [result] = await this.embedBatch([text]);\n    return result;\n  }\n\n  async embedBatch(texts: string[]): Promise<Float32Array[]> {\n    const response = await fetchWithTimeout(API_URL, {\n      method: \"POST\",\n      headers: {\n        Authorization: `Bearer ${this.apiKey}`,\n        \"Content-Type\": \"application/json\",\n      },\n      body: JSON.stringify({\n        model: \"embed-english-v3.0\",\n        texts,\n        input_type: \"search_document\",","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/rohitg00/agentmemory/blob/e04ba88819c365c9acf9d6661ea802143e728bd6/src/providers/embedding/cohere.ts#L1-L32","documentation":"The Cohere embedding provider's constructor requires an API key, taken from the explicit `apiKey` argument or the COHERE_API_KEY env var; if both are empty/undefined it throws immediately at construction time. This fail-fast validation prevents later 401s against Cohere's embed API.","triggerScenarios":"Calling `new CohereEmbeddingProvider()` (or whatever the exported constructor is) with no argument while COHERE_API_KEY is unset, empty string, or whitespace in the environment.","commonSituations":".env file not loaded (forgot dotenv / wrong working directory); variable named COHEREAPIKEY or COHERE_KEY instead of COHERE_API_KEY; shell export didn't propagate to the spawned MCP server; passing an empty string explicitly which is falsy and still triggers the throw.","solutions":["Export the key: `export COHERE_API_KEY=your-key-here` (get one at dashboard.cohere.com).","Pass it explicitly: `new CohereProvider(process.env.COHERE_API_KEY)` after confirming the env var is non-empty.","Verify the exact variable name COHERE_API_KEY and that your .env loader actually runs before construction.","If you meant a different provider, switch to OpenAI/Gemini/local embeddings instead of Cohere."],"exampleFix":"// before\nconst provider = new CohereProvider(); // throws if env missing\n// after\nconst apiKey = process.env.COHERE_API_KEY;\nif (!apiKey) throw new Error(\"Set COHERE_API_KEY first\");\nconst provider = new CohereProvider(apiKey);","handlingStrategy":"validation","validationCode":"// Run before constructing the Cohere provider\nfunction requireEnv(name: string): string {\n  const v = process.env[name];\n  if (!v || v.trim() === \"\") throw new Error(`${name} is not set`);\n  return v.trim();\n}\nconst key = requireEnv(\"COHERE_API_KEY\");","typeGuard":"function hasApiKey(v: unknown): v is string {\n  return typeof v === \"string\" && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  provider = new CohereProvider();\n} catch (e) {\n  if (e instanceof Error && e.message === \"COHERE_API_KEY is required\") {\n    console.error(\"Set COHERE_API_KEY in your environment (see .env.example)\");\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["Keep a .env.example listing COHERE_API_KEY and load .env before importing provider modules.","Fail fast at startup with a clear message if required keys are missing.","Never pass an explicit empty string — it is falsy and still triggers the error.","In CI/containers, mount the secret under the exact name COHERE_API_KEY."],"tags":["missing-env-var","api-key","configuration","embeddings"],"backgroundTag":"missing-env-var","analyzedSha":"e04ba88819c365c9acf9d6661ea802143e728bd6","analyzedAt":"2026-08-30T01:07:40.754Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}