Mintplex-Labs/anything-llm · error · Error

AnthropicLLM::getChatCompletion failed to communicate with A

Error message

AnthropicLLM::getChatCompletion failed to communicate with Anthropic. ${error.message}

What it means

Wraps any exception thrown during the Anthropic chat-completion request and re-throws it with an `AnthropicLLM::getChatCompletion failed to communicate with Anthropic.` prefix plus the original `error.message`. The original error is also console.error'd before re-throw. It is a catch-all for upstream SDK / network / auth failures, not a single specific condition.

Source

Thrown at server/utils/AiProviders/anthropic/index.js:255

      const promptTokens = result.output.usage.input_tokens;
      const completionTokens = result.output.usage.output_tokens;

      return {
        textResponse: result.output.content[0].text,
        metrics: {
          prompt_tokens: promptTokens,
          completion_tokens: completionTokens,
          total_tokens: promptTokens + completionTokens,
          outputTps: completionTokens / result.duration,
          duration: result.duration,
          model: this.model,
          provider: this.className,
          timestamp: new Date(),
        },
      };
    } catch (error) {
      console.error(error);
      throw new Error(
        `AnthropicLLM::getChatCompletion failed to communicate with Anthropic. ${error.message}`
      );
    }
  }

  async streamGetChatCompletion(messages = null, { temperature = 0.7 }) {
    await this.assertModelMaxTokens();
    const systemContent = messages[0].content;
    const measuredStreamRequest = await LLMPerformanceMonitor.measureStream({
      func: this.anthropic.messages.stream({
        model: this.model,
        max_tokens: this.maxTokens,
        system: this.#buildSystemPrompt(systemContent),
        messages: messages.slice(1), // Pop off the system message
        temperature: this.temperatureParam(temperature),
      }),
      messages,
      runPromptTokenCalculation: false,

View on GitHub (pinned to 526360e320)

Solutions

  1. Read the inner `error.message` (already logged via console.error) — the Anthropic SDK returns a descriptive status+body; fix the root cause it names (auth, model, params).
  2. If the model is in noTemperatureModels, ensure the request path omits temperature/top_p/top_k for that model.
  3. Validate the key with a one-off curl: `curl https://api.anthropic.com/v1/messages -H "x-api-key: $ANTHROPIC_API_KEY" ...` to confirm 401 vs network.
  4. On 429, add backoff/retry at the caller or reduce concurrency; on ECONNRESET check network/proxy/SDK version.
  5. Rotate the key in the Anthropic dashboard and update .env if the message indicates an invalid key.

Example fix

// before
const result = await this.anthropic.messages.create({
  model: this.model,
  temperature,
  // ...
});

// after - drop sampling params for noTemperatureModels
const params = {
  model: this.model,
  ...(this.noTemperatureModels.includes(this.model)
    ? {}
    : { temperature }),
};
const result = await this.anthropic.messages.create(params);
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-flight: cheap validity checks before the paid call.
if (!this.model) throw new Error("No Anthropic model selected.");
if (this.noTemperatureModels.includes(this.model) && temperature !== undefined) {
  // strip sampling params to avoid a 400
  temperature = undefined;
}
// (key/model reachability can only truly be tested by the call itself)

Type guard

/**
 * @param {unknown} e
 * @returns {boolean} e is an Anthropic SDK error with status
 */
function isAnthropicSdkError(e) {
  return (
    e != null &&
    typeof e === "object" &&
    typeof e.message === "string" &&
    (typeof e.status === "number" || typeof e.error === "object")
  );
}

Try / catch

try {
  const result = await llm.getChatCompletion(messages, { temperature });
} catch (e) {
  const msg = e.message ?? "";
  if (/401|invalid api key/i.test(msg)) await rotateKey();
  else if (/429|rate limit/i.test(msg)) await backoffRetry(() => llm.getChatCompletion(messages, { temperature }));
  else if (/400|temperature|top_p|top_k/i.test(msg)) stripSamplingAndRetry();
  else throw e;
}

Prevention

When it happens

Trigger: Calling `getChatCompletion(messages, {temperature})` and the underlying `this.anthropic.messages.create(...)` rejects. Concrete causes: 401 invalid key, 429 rate limit, 400 from passing temperature/top_p/top_k to a model in `noTemperatureModels` (claude-opus-4-7, claude-opus-4-8, claude-sonnet-5), 404 unknown model id, DNS/TLS/ECONNRESET to api.anthropic.com, or SDK timeout.

Common situations: Key revoked or rotated but .env still holds the old value; selected a deprecated/renamed model id; hitting Anthropic rate limits under load; corporate proxy or firewall blocking outbound HTTPS; passing sampling params to a reasoning model that rejects them; SDK version mismatch after `npm install`.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/364bd2b9dcd9ebd4. Report an issue: GitHub.