Mintplex-Labs/anything-llm · critical · Error

No OpenRouter API key was set.

Error message

No OpenRouter API key was set.

What it means

Thrown by the OpenRouterEmbedder constructor when process.env.OPENROUTER_API_KEY is falsy. The openai SDK is pointed at openrouter.ai/api/v1 with the key and identifying headers (HTTP-Referer, X-Title); without a key every request would 401, so construction aborts immediately. The model defaults to baai/bge-m3.

Source

Thrown at server/utils/EmbeddingEngines/openRouter/index.js:6

const { toChunks, reportEmbeddingProgress } = require("../../helpers");

class OpenRouterEmbedder {
  constructor() {
    if (!process.env.OPENROUTER_API_KEY)
      throw new Error("No OpenRouter API key was set.");
    this.className = "OpenRouterEmbedder";
    const { OpenAI: OpenAIApi } = require("openai");
    this.openai = new OpenAIApi({
      baseURL: "https://openrouter.ai/api/v1",
      apiKey: process.env.OPENROUTER_API_KEY,
      defaultHeaders: {
        "HTTP-Referer": "https://anythingllm.com",
        "X-Title": "AnythingLLM",
      },
    });
    this.model = process.env.EMBEDDING_MODEL_PREF || "baai/bge-m3";

    // Limit of how many strings we can process in a single pass to stay with resource or network limits
    this.maxConcurrentChunks = 500;

    // https://openrouter.ai/docs/api/reference/embeddings
    this.embeddingMaxChunkLength = 8_191;
  }

View on GitHub (pinned to 526360e320)

Solutions

  1. Set OPENROUTER_API_KEY in .env to a valid key from openrouter.ai/keys
  2. Confirm the key is active and the account has credits
  3. Reload AnythingLLM env after the edit

Example fix

// before
// OPENROUTER_API_KEY unset

// after
OPENROUTER_API_KEY=sk-or-...
EMBEDDING_MODEL_PREF=baai/bge-m3
Defensive patterns

Strategy: validation

Validate before calling

function hasOpenRouterKey(env = process.env) {
  return typeof env.OPENROUTER_API_KEY === 'string' &&
         env.OPENROUTER_API_KEY.trim().length > 0;
}
if (!hasOpenRouterKey()) {
  throw new Error('Missing OPENROUTER_API_KEY.');
}

Type guard

function isOpenRouterKey(v) {
  return typeof v === 'string' && /^sk-or-[A-Za-z0-9_-]{20,}$/.test(v);
}

Prevention

When it happens

Trigger: Selecting the OpenRouter embedding engine while OPENROUTER_API_KEY is unset/empty; constructing `new OpenRouterEmbedder()` before env load. The check on line 6 precedes client creation.

Common situations: Fresh OpenRouter setup; key revoked/rotated on openrouter.ai but not in .env; secret not injected into container/CI; env var typo; OpenRouter credits exhausted so the key was deactivated.

Related errors


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