{"record":{"id":"6f793c94a279a299","repo":"Mintplex-Labs/anything-llm","slug":"no-anthropic-api-key-was-set","errorCode":null,"errorMessage":"No Anthropic API key was set.","messagePattern":"No Anthropic API key was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"server/utils/AiProviders/anthropic/index.js","lineNumber":29,"sourceCode":"} = require(\"../../helpers/chat/LLMPerformanceMonitor\");\nconst { getAnythingLLMUserAgent } = require(\"../../../endpoints/utils\");\n\nclass AnthropicLLM {\n  /**\n   * List of Anthropic models that do not support the `temperature` inference parameter.\n   * These models reject `temperature`/`top_p`/`top_k` with a 400 error.\n   * @type {string[]}\n   */\n  noTemperatureModels = [\n    \"claude-opus-4-7\",\n    \"claude-opus-4-8\",\n    \"claude-sonnet-5\",\n    // Add other models here if identified\n  ];\n\n  constructor(embedder = null, modelPreference = null) {\n    if (!process.env.ANTHROPIC_API_KEY)\n      throw new Error(\"No Anthropic API key was set.\");\n\n    this.className = \"AnthropicLLM\";\n    // Docs: https://www.npmjs.com/package/@anthropic-ai/sdk\n    const AnthropicAI = require(\"@anthropic-ai/sdk\");\n    const anthropic = new AnthropicAI({\n      apiKey: process.env.ANTHROPIC_API_KEY,\n      defaultHeaders: {\n        \"User-Agent\": getAnythingLLMUserAgent(),\n      },\n    });\n    this.anthropic = anthropic;\n    this.model =\n      modelPreference ||\n      process.env.ANTHROPIC_MODEL_PREF ||\n      \"claude-sonnet-4-6\";\n    this.limits = {\n      history: this.promptWindowLimit() * 0.15,\n      system: this.promptWindowLimit() * 0.15,","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/anthropic/index.js#L11-L47","documentation":"Thrown by the AnthropicLLM constructor when process.env.ANTHROPIC_API_KEY is falsy at instantiation time. The provider refuses to construct because every downstream call (chat, stream, embed) needs a valid key. It is a hard precondition gate, not a retryable network failure.","triggerScenarios":"Instantiating `new AnthropicLLM(embedder, model)` (e.g. when a user picks the Anthropic provider in system setup or sends the first chat message) while ANTHROPIC_API_KEY is unset, empty string, or whitespace. AnythingLLM loads providers lazily, so this typically fires on the first request after selecting Anthropic, not at server boot.","commonSituations":"Fresh install where .env was never populated; .env edited but server not restarted; key put under a wrong name (ANTHROPIC_API_KEY vs ANTHROPIC_KEY); Docker container missing the -e flag or env_file; copied .env.example to .env but left the value blank.","solutions":["Add `ANTHROPIC_API_KEY=sk-ant-...` to the server's .env file and restart the AnythingLLM process/container so dotenv reloads.","In the UI, re-open System > LLM Provider and confirm the key field is saved; AnythingLLM writes provider keys to .env on save.","Verify the variable is actually exported in the running process: `node -e \"console.log(Boolean(process.env.ANTHROPIC_API_KEY))\"` from the same shell/container.","Check for typos or trailing whitespace in the key name and value; an empty or quoted-empty string still fails the truthiness check."],"exampleFix":"// before\n// .env\nANTHROPIC_API_KEY=\n\n// after\n// .env\nANTHROPIC_API_KEY=sk-ant-api03-xxxxxxxxxxxxxxxxxxxxxxxx","handlingStrategy":"validation","validationCode":"// Before constructing AnthropicLLM\nif (!process.env.ANTHROPIC_API_KEY) {\n  throw new Error(\n    \"ANTHROPIC_API_KEY is missing. Set it in .env or the provider UI before selecting Anthropic.\"\n  );\n}\nconst llm = new AnthropicLLM(embedder, modelPref);","typeGuard":"/**\n * @param {unknown} v\n * @returns {boolean} v is a non-empty api key string\n */\nfunction isNonEmptyKey(v) {\n  return typeof v === \"string\" && v.trim().length > 0;\n}\nisNonEmptyKey(process.env.ANTHROPIC_API_KEY);","tryCatchPattern":"// Construction-time guards are not retryable; catch only to surface a friendly message.\ntry {\n  const llm = new AnthropicLLM(embedder, modelPref);\n} catch (e) {\n  if (/No Anthropic API key/.test(e.message)) {\n    return { ok: false, reason: \"config\", message: \"Set ANTHROPIC_API_KEY.\" };\n  }\n  throw e;\n}","preventionTips":["Validate required provider env vars at app boot and fail with a consolidated error before serving traffic.","Use a single source of truth for provider config (AnythingLLM's provider settings) rather than ad-hoc .env edits.","Add a smoke-test endpoint that constructs the selected provider on save and reports success/failure.","Document each provider's required env var names in onboarding so users don't mistype them."],"tags":["config","env","authentication","anthropic","constructor"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}