Mintplex-Labs/anything-llm · error · Error

e.message

Error message

e.message

What it means

A catch-and-rethrow wrapper around the OpenAI SDK call in LiteLLM's getChatCompletion. The `.catch((e) => { throw new Error(e.message); })` discards the original SDK error's type, HTTP status, and cause, preserving only the message text. Any failure from the LiteLLM proxy (network, auth, upstream backend failure, rate limit) surfaces here.

Source

Thrown at server/utils/AiProviders/liteLLM/index.js:137

      prompt,
      ...formatChatHistory(chatHistory, this.#generateContent),
      {
        role: "user",
        content: this.#generateContent({ userPrompt, attachments }),
      },
    ];
  }

  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;

    return {
      textResponse: result.output.choices[0].message.content,
      metrics: {
        prompt_tokens: result.output.usage?.prompt_tokens || 0,
        completion_tokens: result.output.usage?.completion_tokens || 0,
        total_tokens: result.output.usage?.total_tokens || 0,
        outputTps:
          (result.output.usage?.completion_tokens || 0) / result.duration,
        duration: result.duration,

View on GitHub (pinned to 526360e320)

Solutions

  1. Check the error message text for status codes or upstream provider details (e.g. '401 Unauthorized', 'model not found').
  2. Verify the LiteLLM proxy is running and its config.yaml routes the requested model.
  3. Check LITE_LLM_API_KEY is correct if the proxy requires authentication.
  4. Look at the LiteLLM proxy logs for the upstream error, since AnythingLLM only shows the re-wrapped message.
  5. Refactor the catch to preserve the original error if structured error handling is needed upstream.

Example fix

// before — structured error data lost
.catch((e) => {
  throw new Error(e.message);
})

// after — preserve original error
.catch((e) => {
  throw e;
})
Defensive patterns

Strategy: try-catch

Validate before calling

async function checkLiteLLMHealth(basePath, apiKey) {
  const headers = apiKey ? { Authorization: `Bearer ${apiKey}` } : {};
  const res = await fetch(`${basePath}/v1/models`, { headers });
  if (!res.ok) throw new Error(`LiteLLM proxy at ${basePath} returned ${res.status}`);
  return true;
}

await checkLiteLLMHealth(process.env.LITE_LLM_BASE_PATH, process.env.LITE_LLM_API_KEY);

Try / catch

try {
  const result = await litellmProvider.getChatCompletion(messages, { temperature: 0.7 });
} catch (e) {
  // The wrapper preserves only e.message from the SDK error.
  if (e.message.includes('ECONNREFUSED') || e.message.includes('fetch failed')) {
    console.error('LiteLLM proxy unreachable at', process.env.LITE_LLM_BASE_PATH);
  } else if (e.message.includes('401') || e.message.includes('Unauthorized')) {
    console.error('LiteLLM API key invalid — check LITE_LLM_API_KEY.');
  } else {
    console.error('LiteLLM chat failed:', e.message);
    // Check LiteLLM proxy logs for upstream provider errors.
  }
}

Prevention

When it happens

Trigger: Calling `litellmProvider.getChatCompletion(messages, { temperature })` when the LiteLLM proxy returns an error. This includes: proxy unreachable, invalid API key (LITE_LLM_API_KEY), upstream model failure (e.g. the backend provider returns an error through LiteLLM), rate limiting, or a malformed model name that LiteLLM cannot route.

Common situations: The LiteLLM proxy is down or restarted. The upstream API key configured in LiteLLM expired or hit its quota. The model name in LITE_LLM_MODEL_PREF doesn't match any route configured in the LiteLLM proxy's config.yaml. Network issues between AnythingLLM and the proxy.

Related errors


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