mem0ai/mem0 · error · Error

Anthropic API key is required

Error message

Anthropic API key is required

What it means

AnthropicLLM requires an API key at construction: config.apiKey first, then the ANTHROPIC_API_KEY environment variable; if neither exists it throws before any request is made. This is fail-fast configuration validation so that missing credentials surface at startup, not mid-conversation.

Source

Thrown at mem0-ts/src/oss/src/llms/anthropic.ts:17

import type Anthropic from "@anthropic-ai/sdk";
import { LLM, LLMResponse } from "./base";
import { LLMConfig, Message } from "../types";
import { loadPeer } from "../utils/load_peer";

export class AnthropicLLM implements LLM {
  private client!: Anthropic;
  private readonly clientArgs: { apiKey: string; baseURL?: string };
  private model: string;
  private maxTokens: number;
  private temperature?: number;
  private topP?: number;

  constructor(config: LLMConfig) {
    const apiKey = config.apiKey || process.env.ANTHROPIC_API_KEY;
    if (!apiKey) {
      throw new Error("Anthropic API key is required");
    }
    // Forward baseURL to the client when set so proxy/gateway users are
    // honored (parity with the OpenAI provider and the Python fix in #5626).
    const clientArgs: { apiKey: string; baseURL?: string } = { apiKey };
    if (config.baseURL) {
      clientArgs.baseURL = config.baseURL;
    }
    this.clientArgs = clientArgs;
    this.model = config.model || "claude-sonnet-4-6";
    // Defaults mirror the Python provider's AnthropicConfig
    // (max_tokens=2000, temperature=0.1, top_p omitted).
    this.maxTokens = config.maxTokens ?? 2000;
    this.temperature = config.temperature ?? 0.1;
    this.topP = config.topP;
  }

  private async ensureClient(): Promise<void> {
    if (this.client) return;

View on GitHub (pinned to 001c235229)

Solutions

  1. Set ANTHROPIC_API_KEY in the environment or pass apiKey inside the LLM config
  2. Ensure dotenv loads before constructing Memory (import 'dotenv/config' at the top of the entry file)
  3. Verify the variable is injected in your deploy target (docker run -e, Kubernetes secrets, CI variables)
  4. Obtain a key from console.anthropic.com if none exists

Example fix

// before
new Memory({ llm: { provider: "anthropic" } });

// after
new Memory({
  llm: { provider: "anthropic", config: { apiKey: process.env.ANTHROPIC_API_KEY! } },
});
Defensive patterns

Strategy: validation

Validate before calling

const apiKey = config.apiKey ?? process.env.ANTHROPIC_API_KEY;
if (!apiKey) throw new Error("ANTHROPIC_API_KEY missing - set it before constructing the LLM");

Type guard

function isMissingAnthropicKey(err: unknown): boolean {
  return err instanceof Error && err.message === "Anthropic API key is required";
}

Try / catch

try {
  new AnthropicLLM(cfg);
} catch (err) {
  if (err instanceof Error && err.message === "Anthropic API key is required") {
    throw new Error("Config error: set ANTHROPIC_API_KEY or pass llm.config.apiKey");
  }
  throw err;
}

Prevention

When it happens

Trigger: Configuring Memory with LLM provider 'anthropic' without an apiKey while ANTHROPIC_API_KEY is unset; env var typo (ANTHROPIC_APIKEY, ANTHROPIC_KEY); dotenv loaded after the Memory instance is constructed; secrets missing in CI/Docker/serverless.

Common situations: Works locally (key in shell) but fails in deployment where the env var was never injected; .env not committed and not configured in the platform; mixing up the OpenAI and Anthropic key names.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/dc78f14ae61df0f9. Report an issue: GitHub.