{"record":{"id":"3742ab0d21bab956","repo":"Mintplex-Labs/anything-llm","slug":"e-message-3742ab","errorCode":null,"errorMessage":"e.message","messagePattern":"e\\.message","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/foundry/index.js","lineNumber":324,"sourceCode":"    if (!this.model)\n      throw new Error(\n        `Foundry chat: ${this.model} is not valid or defined model for chat completion!`\n      );\n\n    // max_completion_tokens is required by Foundry (it caps output at 1024\n    // otherwise), so the window has to be resolved before the request is built.\n    await this.assertModelContextLimits();\n    await this.assertModelLoaded();\n    const result = await LLMPerformanceMonitor.measureAsyncFunction(\n      this.openai.chat.completions\n        .create({\n          model: this.model,\n          messages,\n          temperature,\n          max_completion_tokens: this.promptWindowLimit(),\n        })\n        .catch((e) => {\n          throw new Error(e.message);\n        })\n    );\n\n    if (\n      !result.output.hasOwnProperty(\"choices\") ||\n      result.output.choices.length === 0\n    )\n      return null;\n\n    return {\n      textResponse: result.output.choices[0].message.content,\n      metrics: {\n        prompt_tokens: result.output.usage.prompt_tokens || 0,\n        completion_tokens: result.output.usage.completion_tokens || 0,\n        total_tokens: result.output.usage.total_tokens || 0,\n        outputTps: result.output.usage.completion_tokens / result.duration,\n        duration: result.duration,\n        model: this.model,","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/foundry/index.js#L306-L342","documentation":"Not a distinct error type — the literal message is whatever e.message the underlying OpenAI SDK (Foundry's OpenAI-compatible endpoint) threw. The `.catch((e) => { throw new Error(e.message); })` wrapper strips the original error class and stack, re-throwing only the human-readable text. So 'e.message' covers every failure the SDK can raise during a chat completion: rate limits, auth, model-not-found, network/premature-close, malformed request, etc.","triggerScenarios":"this.openai.chat.completions.create({ model, messages, temperature, max_completion_tokens }) rejecting for any reason — Foundry Local returned a non-2xx, the socket dropped mid-response (the 'Premature close' case the file comments on), max_completion_tokens exceeded the window, or the model was evicted after loading.","commonSituations":"Model evicted by Foundry's idle timeout right after the load check (the documented 'Premature close'); max_completion_tokens larger than the resolved promptWindowLimit; Foundry Local crashed/OOM during generation; transient network blip to localhost endpoint.","solutions":["Read the captured e.message text — it is the real cause (e.g. 'Premature close', 'context_length_exceeded', 401)","If it says the model is gone, the file's recovery path is to clear it from #loadedModels so the next call reloads it","Lower max_completion_tokens / the resolved context window if the message mentions context length","Check Foundry Local process health and VRAM; restart Foundry if it OOMed"],"exampleFix":"// before (loses stack + class)\n.catch((e) => { throw new Error(e.message); })\n\n// after (preserve original error)\n.catch((e) => { throw e; })","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  return await llm.getChatCompletion(messages, { temperature });\n} catch (e) {\n  // e.message is the OpenAI SDK's text; match on it since the class is lost\n  if (/Premature close/i.test(e.message)) {\n    FoundryLLM.forgetModel(this.model); // clear cache so next call reloads\n    return retryOnce();\n  }\n  if (/context_length_exceeded/i.test(e.message)) {\n    throw new ContextTooLongError(e.message);\n  }\n  throw e;\n}","preventionTips":["Prefer re-throwing the original SDK error (throw e) so the class/stack survive","Implement the file's documented 'forget model on Premature close' recovery explicitly at the caller","Keep max_completion_tokens comfortably under promptWindowLimit()"],"tags":["foundry","openai-sdk","error-wrapping","chat-completion","network"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}