rohitg00/agentmemory · error · Error

Cohere embedding failed (${response.status}): ${err}

Error message

Cohere embedding failed (${response.status}): ${err}

What it means

`embedBatch` calls Cohere's embedding HTTP API and throws this error when the response status is not ok, embedding the HTTP status code plus the raw response body. Unlike the constructor error, this is a runtime/network failure: the key existed but the API rejected the request (or the server returned 5xx).

Source

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

  }

  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",
      }),
    });

    if (!response.ok) {
      const err = await response.text();
      throw new Error(`Cohere embedding failed (${response.status}): ${err}`);
    }

    const data = (await response.json()) as {
      embeddings: number[][];
    };

    return data.embeddings.map((e) => new Float32Array(e));
  }
}

View on GitHub (pinned to e04ba88819)

Solutions

  1. Read the embedded response body in the message — it contains Cohere's exact reason (invalid key, model not found, quota).
  2. If 401/403: rotate to a fresh COHERE_API_KEY and restart the process.
  3. If 429: add exponential backoff/retry with jitter around embedBatch calls, and reduce batch size/frequency.
  4. If 400: check the `model` and `texts` payload — non-empty strings within length limits — and pin a model your key can access.
  5. If 5xx: retry after a delay; if persistent, check Cohere's status page or switch providers.

Example fix

// before
const vec = await provider.embed(text); // throws on 429/401 with raw body
// after
try {
  const vec = await provider.embed(text);
} catch (e) {
  if (/\(429\)/.test(e.message)) await sleep(backoff()); // then retry
  else if (/\((401|403)\)/.test(e.message)) rotateCohereKey();
  else throw e;
}
Defensive patterns

Strategy: retry

Validate before calling

// Preflight: cheap check that the key is at least shaped correctly
if (!/^\S{20,}$/.test(process.env.COHERE_API_KEY ?? "")) {
  console.warn("COHERE_API_KEY looks invalid; API calls will likely 401");
}

Try / catch

async function embedWithRetry(text: string, tries = 3) {
  for (let i = 0; i < tries; i++) {
    try { return await provider.embed(text); }
    catch (e) {
      const m = e instanceof Error ? e.message : String(e);
      const status = Number(/Cohere embedding failed \((\d+)\)/.exec(m)?.[1]);
      if (status === 401 || status === 403 || (status >= 400 && status < 500 && status !== 429)) throw e;
      await new Promise(r => setTimeout(r, 2 ** i * 500 + Math.random() * 250));
    }
  }
  throw new Error("Cohere embed: retries exhausted");
}

Prevention

When it happens

Trigger: Any `embed()`/`embedBatch()` call where Cohere returns a non-2xx: invalid or revoked API key (401/403), model name not entitled (400/404), rate limit or quota exceeded (429), malformed input (empty texts, too-long input), or 5xx service errors.

Common situations: Key rotated/revoked in the Cohere dashboard while the process still holds the old one; free-trial trial-key limits on the embed model; sending batches exceeding Cohere's size limits; regional endpoint/network errors surfacing as 5xx; test key used in prod tenant.

Related errors


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