Mintplex-Labs/anything-llm · critical · Error

No OpenRouter API key was set.

Error message

No OpenRouter API key was set.

What it means

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.

Source

Thrown at server/utils/AiProviders/openRouter/index.js:33

  process.env.STORAGE_DIR
    ? path.resolve(process.env.STORAGE_DIR, "models", "openrouter")
    : path.resolve(__dirname, `../../../storage/models/openrouter`)
);

class OpenRouterLLM {
  /**
   * Some openrouter models never send a finish_reason and thus leave the stream open in the UI.
   * However, because OR is a middleware it can also wait an inordinately long time between chunks so we need
   * to ensure that we dont accidentally close the stream too early. If the time between chunks is greater than this timeout
   * we will close the stream and assume it to be complete. This is common for free models or slow providers they can
   * possibly delegate to during invocation.
   * @type {number}
   */
  defaultTimeout = 3_000;

  constructor(embedder = null, modelPreference = null) {
    if (!process.env.OPENROUTER_API_KEY)
      throw new Error("No OpenRouter API key was set.");

    this.className = "OpenRouterLLM";
    const { OpenAI: OpenAIApi } = require("openai");
    this.basePath = "https://openrouter.ai/api/v1";
    this.openai = new OpenAIApi({
      baseURL: this.basePath,
      apiKey: process.env.OPENROUTER_API_KEY ?? null,
      defaultHeaders: {
        "HTTP-Referer": "https://anythingllm.com",
        "X-Title": "AnythingLLM",
      },
    });
    this.model =
      modelPreference || process.env.OPENROUTER_MODEL_PREF || "openrouter/auto";
    this.limits = {
      history: this.promptWindowLimit() * 0.15,
      system: this.promptWindowLimit() * 0.15,
      user: this.promptWindowLimit() * 0.7,

View on GitHub (pinned to 526360e320)

Solutions

  1. Generate a key at openrouter.ai/keys and set OPENROUTER_API_KEY in .env.
  2. Restart the server so the key is loaded.
  3. Ensure the key is funded (OpenRouter returns 402 on zero credits, but the guard is about presence).
  4. Verify the var is actually passed into the container/process environment.

Example fix

# before
# (OPENROUTER_API_KEY unset)

# after - .env
OPENROUTER_API_KEY=sk-or-...
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.OPENROUTER_API_KEY || !process.env.OPENROUTER_API_KEY.trim())
  throw new Error('OPENROUTER_API_KEY must be set before starting OpenRouterLLM.');

Prevention

When it happens

Trigger: Instantiating OpenRouterLLM when process.env.OPENROUTER_API_KEY is undefined or empty.

Common situations: .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.

Related errors


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