{"record":{"id":"82f5c2a0b30a0058","repo":"Mintplex-Labs/anything-llm","slug":"e-message","errorCode":null,"errorMessage":"e.message","messagePattern":"e\\.message","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/apipie/index.js","lineNumber":204,"sourceCode":"      },\n    ];\n  }\n\n  async getChatCompletion(messages = null, { temperature = 0.7 }) {\n    if (!(await this.isValidChatCompletionModel(this.model)))\n      throw new Error(\n        `ApiPie chat: ${this.model} is not valid for chat completion!`\n      );\n\n    const result = await LLMPerformanceMonitor.measureAsyncFunction(\n      this.openai.chat.completions\n        .create({\n          model: this.model,\n          messages,\n          temperature,\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:\n          (result.output.usage?.completion_tokens || 0) / result.duration,\n        duration: result.duration,","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/3aec848f2885144aa8f1e53b9731a04310d5d558/server/utils/AiProviders/apipie/index.js#L186-L222","documentation":"Not a fixed message: this is the .catch attached to openai.chat.completions.create re-throwing the upstream ApiPie (OpenAI-compatible) error's message verbatim. Whatever the endpoint returned - 401 incorrect APIPIE_LLM_API_KEY, 429 quota, 404 model not found, socket timeouts, oversized prompts - becomes the Error text. Because the promise rejects before completion, LLMPerformanceMonitor never records metrics for the call.","triggerScenarios":"Invalid or expired ApiPie key (401); model removed between validation and the call (404); rate/quota limits (429); network failure reaching apipie.ai; prompt exceeding the upstream model's context window.","commonSituations":"Key rotation without updating provider settings; aggregator or upstream provider outages; long workspace context pushed into the prompt; regional or firewall blocks on apipie.ai.","solutions":["Match the error text to a status: 401 -> fix the key, 429 -> back off, 404 -> repick the model, network -> check egress","Verify APIPIE_LLM_API_KEY with a direct curl to https://apipie.ai/v1/models","Retry with exponential backoff on 429/5xx","Trim workspace context if context-length errors appear"],"exampleFix":"// before\nconst result = await apipie.getChatCompletion(messages);\n\n// after\ntry {\n  const result = await apipie.getChatCompletion(messages);\n} catch (e) {\n  if (/429|rate/i.test(e.message)) {\n    await new Promise((r) => setTimeout(r, 5000));\n    return apipie.getChatCompletion(messages);\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"// Cheap liveness + auth probe before a long chat call\ntry {\n  await apipie.openai.models.list();\n} catch (e) {\n  return res.status(502).json({ error: `ApiPie unreachable or key invalid: ${e.message}` });\n}","typeGuard":null,"tryCatchPattern":"try {\n  const result = await apipie.getChatCompletion(messages);\n} catch (e) {\n  if (/401|api key/i.test(e.message)) handleBadKey();\n  else if (/429|quota|rate/i.test(e.message)) return retryWithBackoff(fn);\n  else if (/404|model/i.test(e.message)) handleBadModel();\n  else throw e;\n}","preventionTips":["Probe the key with models.list before starting long conversations","Classify upstream errors by inspecting the message text (it is verbatim from the API)","Bound prompt size to avoid context-length rejections","Retry transient statuses only, with backoff"],"tags":["apipie","openai-compatible","api","network"],"backgroundTag":"llm-api-request-failed","analyzedSha":"3aec848f2885144aa8f1e53b9731a04310d5d558","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}