rohitg00/agentmemory · error · Error

COHERE_API_KEY is required

Error message

COHERE_API_KEY is required

What it means

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.

Source

Thrown at src/providers/embedding/cohere.ts:14

import type { EmbeddingProvider } from "../../types.js";
import { getEnvVar } from "../../config.js";
import { fetchWithTimeout } from "../_fetch.js";

const API_URL = "https://api.cohere.ai/v1/embed";

export class CohereEmbeddingProvider implements EmbeddingProvider {
  readonly name = "cohere";
  readonly dimensions = 1024;
  private apiKey: string;

  constructor(apiKey?: string) {
    this.apiKey = apiKey || getEnvVar("COHERE_API_KEY") || "";
    if (!this.apiKey) throw new Error("COHERE_API_KEY is required");
  }

  async embed(text: string): Promise<Float32Array> {
    const [result] = await this.embedBatch([text]);
    return result;
  }

  async embedBatch(texts: string[]): Promise<Float32Array[]> {
    const response = await fetchWithTimeout(API_URL, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${this.apiKey}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        model: "embed-english-v3.0",
        texts,
        input_type: "search_document",

View on GitHub (pinned to e04ba88819)

Solutions

  1. Export the key: `export COHERE_API_KEY=your-key-here` (get one at dashboard.cohere.com).
  2. Pass it explicitly: `new CohereProvider(process.env.COHERE_API_KEY)` after confirming the env var is non-empty.
  3. Verify the exact variable name COHERE_API_KEY and that your .env loader actually runs before construction.
  4. If you meant a different provider, switch to OpenAI/Gemini/local embeddings instead of Cohere.

Example fix

// before
const provider = new CohereProvider(); // throws if env missing
// after
const apiKey = process.env.COHERE_API_KEY;
if (!apiKey) throw new Error("Set COHERE_API_KEY first");
const provider = new CohereProvider(apiKey);
Defensive patterns

Strategy: validation

Validate before calling

// Run before constructing the Cohere provider
function requireEnv(name: string): string {
  const v = process.env[name];
  if (!v || v.trim() === "") throw new Error(`${name} is not set`);
  return v.trim();
}
const key = requireEnv("COHERE_API_KEY");

Type guard

function hasApiKey(v: unknown): v is string {
  return typeof v === "string" && v.trim().length > 0;
}

Try / catch

try {
  provider = new CohereProvider();
} catch (e) {
  if (e instanceof Error && e.message === "COHERE_API_KEY is required") {
    console.error("Set COHERE_API_KEY in your environment (see .env.example)");
    process.exit(1);
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: .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.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30). Data as JSON: /api/errors/8fe914fe8d7dd046. Report an issue: GitHub.