Mintplex-Labs/anything-llm · error · Error
${e.message}
Error message
${e.message} What it means
This error re-throws the underlying message from the OpenAI SDK when the Cohere OpenAI-compatible chat completions endpoint (/compatibility/v1/chat/completions) rejects the request. The .catch handler unwraps the SDK error into a plain Error, discarding the original error type, status code, and stack trace. Any failure surfaced by the OpenAI client (auth, rate limit, invalid model, malformed messages, network timeout) collapses into this single opaque message.
Source
Thrown at server/utils/AiProviders/cohere/index.js:91
userPrompt = "",
}) {
const prompt = {
role: "system",
content: `${systemPrompt}${this.#appendContext(contextTexts)}`,
};
return [prompt, ...chatHistory, { role: "user", content: userPrompt }];
}
async getChatCompletion(messages = null, { temperature = 0.7 }) {
const result = await LLMPerformanceMonitor.measureAsyncFunction(
this.openai.chat.completions
.create({
model: this.model,
messages,
temperature,
})
.catch((e) => {
throw new Error(e.message);
})
);
if (
!result.output.hasOwnProperty("choices") ||
result.output.choices.length === 0
)
return null;
const promptTokens = result.output.usage?.prompt_tokens || 0;
const completionTokens = result.output.usage?.completion_tokens || 0;
return {
textResponse: result.output.choices[0].message.content,
metrics: {
prompt_tokens: promptTokens,
completion_tokens: completionTokens,
total_tokens: promptTokens + completionTokens,
outputTps: completionTokens / result.duration,View on GitHub (pinned to 526360e320)
Solutions
- Read the raw e.message — it usually contains the HTTP status (e.g. '401 Unauthorized', '429 Too Many Requests') which pinpoints the cause.
- Verify COHERE_API_KEY is current and has not been revoked in the Cohere dashboard.
- Confirm COHERE_MODEL_PREF is a live model id listed in Cohere's /v1/models endpoint.
- If the message indicates 429, reduce concurrency or implement request spacing before retrying.
- Patch the catch to preserve the original error: `.catch((e) => { throw e; })` so the SDK's structured error (status, headers) survives.
Example fix
// before
.catch((e) => {
throw new Error(e.message);
})
// after — preserve the original SDK error (status code, response body)
// simply rethrow, or augment context without discarding the type:
.catch((e) => {
const status = e?.status ?? e?.response?.status;
e.message = `Cohere chat completion failed${status ? ` (HTTP ${status})` : ""}: ${e.message}`;
throw e;
}) Defensive patterns
Strategy: try-catch
Validate before calling
if (!process.env.COHERE_API_KEY) throw new Error('COHERE_API_KEY is not set');
const validModels = await fetch('https://api.cohere.ai/compatibility/v1/models', {
headers: { Authorization: `Bearer ${process.env.COHERE_API_KEY}` },
}).then(r => r.json());
if (!validModels.data?.some(m => m.id === modelId))
throw new Error(`Model ${modelId} is not available on Cohere`); Type guard
/** Cohere returns an OpenAI-shaped error with a `status` field. */
function isCohereApiError(e) {
return (
e instanceof Error &&
(typeof e.status === 'number' ||
/401|403|404|429|5\d{2}/.test(e.message))
);
} Try / catch
try {
const result = await cohere.getChatCompletion(messages, { temperature });
} catch (e) {
if (/429|rate/i.test(e.message)) {
await sleep(backoffMs);
return retry();
}
if (/401|403|unauthorized/i.test(e.message))
throw new Error('Cohere API key is invalid or revoked — update COHERE_API_KEY');
throw e;
} Prevention
- Validate COHERE_API_KEY and the model id against the /v1/models endpoint before the first chat call.
- Implement request spacing to avoid 429 rate limits during bulk operations.
- Log the full SDK error (not just e.message) during development to preserve the HTTP status.
When it happens
Trigger: Calling getChatCompletion with a model string Cohere does not serve (e.g. a deprecated command model), an expired or revoked COHERE_API_KEY, exceeding the per-minute request quota, sending message objects that violate the OpenAI schema (missing role/content), or a network interruption between the server and api.cohere.ai.
Common situations: Operators who rotate API keys but forget to update the AnythingLLM env config; switching the COHERE_MODEL_PREF to a model id that was retired; transient 429 rate-limiting during bulk document processing; on-prem deployments behind a proxy that strips or mangles the Authorization header.
Related errors
AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13).
Data as JSON: /api/errors/478ccf39458b9dbb.
Report an issue: GitHub.