{"record":{"id":"c4731493f883798c","repo":"Mintplex-Labs/anything-llm","slug":"no-openrouter-api-key-was-set","errorCode":null,"errorMessage":"No OpenRouter API key was set.","messagePattern":"No OpenRouter API key was set\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"server/utils/AiProviders/openRouter/index.js","lineNumber":33,"sourceCode":"  process.env.STORAGE_DIR\n    ? path.resolve(process.env.STORAGE_DIR, \"models\", \"openrouter\")\n    : path.resolve(__dirname, `../../../storage/models/openrouter`)\n);\n\nclass OpenRouterLLM {\n  /**\n   * Some openrouter models never send a finish_reason and thus leave the stream open in the UI.\n   * However, because OR is a middleware it can also wait an inordinately long time between chunks so we need\n   * to ensure that we dont accidentally close the stream too early. If the time between chunks is greater than this timeout\n   * we will close the stream and assume it to be complete. This is common for free models or slow providers they can\n   * possibly delegate to during invocation.\n   * @type {number}\n   */\n  defaultTimeout = 3_000;\n\n  constructor(embedder = null, modelPreference = null) {\n    if (!process.env.OPENROUTER_API_KEY)\n      throw new Error(\"No OpenRouter API key was set.\");\n\n    this.className = \"OpenRouterLLM\";\n    const { OpenAI: OpenAIApi } = require(\"openai\");\n    this.basePath = \"https://openrouter.ai/api/v1\";\n    this.openai = new OpenAIApi({\n      baseURL: this.basePath,\n      apiKey: process.env.OPENROUTER_API_KEY ?? null,\n      defaultHeaders: {\n        \"HTTP-Referer\": \"https://anythingllm.com\",\n        \"X-Title\": \"AnythingLLM\",\n      },\n    });\n    this.model =\n      modelPreference || process.env.OPENROUTER_MODEL_PREF || \"openrouter/auto\";\n    this.limits = {\n      history: this.promptWindowLimit() * 0.15,\n      system: this.promptWindowLimit() * 0.15,\n      user: this.promptWindowLimit() * 0.7,","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/openRouter/index.js#L15-L51","documentation":"Synchronous constructor guard. OpenRouterLLM cannot be instantiated unless OPENROUTER_API_KEY is set; the key is passed to the OpenAI-compatible client targeting https://openrouter.ai/api/v1. Thrown at 'new OpenRouterLLM(...)' before any network activity.","triggerScenarios":"Instantiating OpenRouterLLM when process.env.OPENROUTER_API_KEY is undefined or empty.","commonSituations":".env missing the var; key var typo'd (OPENROUTER_APIKEY); key removed after rotation; fresh deploy; account credit issue led someone to delete the key.","solutions":["Generate a key at openrouter.ai/keys and set OPENROUTER_API_KEY in .env.","Restart the server so the key is loaded.","Ensure the key is funded (OpenRouter returns 402 on zero credits, but the guard is about presence).","Verify the var is actually passed into the container/process environment."],"exampleFix":"# before\n# (OPENROUTER_API_KEY unset)\n\n# after - .env\nOPENROUTER_API_KEY=sk-or-...","handlingStrategy":"validation","validationCode":"if (!process.env.OPENROUTER_API_KEY || !process.env.OPENROUTER_API_KEY.trim())\n  throw new Error('OPENROUTER_API_KEY must be set before starting OpenRouterLLM.');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate a key at openrouter.ai/keys and fund the account.","Use the exact env name OPENROUTER_API_KEY.","Inject the secret via the container environment, never commit it."],"tags":["openrouter","configuration","env","authentication"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}