Mintplex-Labs/anything-llm · error

No Moonshot AI API key was set.

Error message

No Moonshot AI API key was set.

What it means

MoonshotAiLLM's constructor throws when process.env.MOONSHOT_AI_API_KEY is falsy, before the OpenAI-compatible client for https://api.moonshot.ai/v1 is created. Any code path that instantiates the provider (selecting Moonshot as the LLM, sending a chat) fails until the key is set.

Solutions

  1. Add MOONSHOT_AI_API_KEY=<key from platform.moonshot.ai> to the server's .env
  2. Restart the AnythingLLM server or container
  3. Verify the variable name reaches the container (name check only)
  4. Remove quotes/whitespace around the value in compose/.env files

Example fix

# before
# (MOONSHOT_AI_API_KEY not set)
# after
MOONSHOT_AI_API_KEY=sk-...your-key
Defensive patterns

Strategy: validation

Validate before calling

if (!process.env.MOONSHOT_AI_API_KEY)
  throw new Error('Moonshot provider selected but MOONSHOT_AI_API_KEY is not configured');
const llm = new MoonshotAiLLM(embedder);

Try / catch

try {
  const llm = new MoonshotAiLLM(embedder);
} catch (e) {
  if (e.message === 'No Moonshot AI API key was set.')
    return respondConfigError(e); // configure key, restart, do not retry
  throw e;
}

Prevention

When it happens

Trigger: Selecting Moonshot/Kimi as the provider with MOONSHOT_AI_API_KEY unset, misspelled, or empty; editing .env without restarting; container deployments missing the variable.

Common situations: Typo'd variable name (MOONSHOT_API_KEY, MOONSHOT_KEY); forgetting to restart after .env edits; docker-compose without the env entry.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@f92433b4ea (2026-08-18). Data as JSON: /api/errors/c81c64920ee94ab0. Report an issue: GitHub.

Appendix: source

Thrown at server/utils/AiProviders/moonshotAi/index.js:14

const { NativeEmbedder } = require("../../EmbeddingEngines/native");
const {
  LLMPerformanceMonitor,
} = require("../../helpers/chat/LLMPerformanceMonitor");
const {
  handleDefaultStreamResponseV2,
  formatChatHistory,
} = require("../../helpers/chat/responses");
const { MODEL_MAP } = require("../modelMap");

class MoonshotAiLLM {
  constructor(embedder = null, modelPreference = null) {
    if (!process.env.MOONSHOT_AI_API_KEY)
      throw new Error("No Moonshot AI API key was set.");
    this.className = "MoonshotAiLLM";
    const { OpenAI: OpenAIApi } = require("openai");

    this.openai = new OpenAIApi({
      baseURL: "https://api.moonshot.ai/v1",
      apiKey: process.env.MOONSHOT_AI_API_KEY,
    });
    this.model =
      modelPreference ||
      process.env.MOONSHOT_AI_MODEL_PREF ||
      "moonshot-v1-32k";
    this.limits = {
      history: this.promptWindowLimit() * 0.15,
      system: this.promptWindowLimit() * 0.15,
      user: this.promptWindowLimit() * 0.7,
    };

    this.embedder = embedder ?? new NativeEmbedder();

View on GitHub (pinned to f92433b4ea)