{"record":{"id":"dc78f14ae61df0f9","repo":"mem0ai/mem0","slug":"anthropic-api-key-is-required","errorCode":null,"errorMessage":"Anthropic API key is required","messagePattern":"Anthropic API key is required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/llms/anthropic.ts","lineNumber":17,"sourceCode":"import type Anthropic from \"@anthropic-ai/sdk\";\nimport { LLM, LLMResponse } from \"./base\";\nimport { LLMConfig, Message } from \"../types\";\nimport { loadPeer } from \"../utils/load_peer\";\n\nexport class AnthropicLLM implements LLM {\n  private client!: Anthropic;\n  private readonly clientArgs: { apiKey: string; baseURL?: string };\n  private model: string;\n  private maxTokens: number;\n  private temperature?: number;\n  private topP?: number;\n\n  constructor(config: LLMConfig) {\n    const apiKey = config.apiKey || process.env.ANTHROPIC_API_KEY;\n    if (!apiKey) {\n      throw new Error(\"Anthropic API key is required\");\n    }\n    // Forward baseURL to the client when set so proxy/gateway users are\n    // honored (parity with the OpenAI provider and the Python fix in #5626).\n    const clientArgs: { apiKey: string; baseURL?: string } = { apiKey };\n    if (config.baseURL) {\n      clientArgs.baseURL = config.baseURL;\n    }\n    this.clientArgs = clientArgs;\n    this.model = config.model || \"claude-sonnet-4-6\";\n    // Defaults mirror the Python provider's AnthropicConfig\n    // (max_tokens=2000, temperature=0.1, top_p omitted).\n    this.maxTokens = config.maxTokens ?? 2000;\n    this.temperature = config.temperature ?? 0.1;\n    this.topP = config.topP;\n  }\n\n  private async ensureClient(): Promise<void> {\n    if (this.client) return;","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/llms/anthropic.ts#L1-L35","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set ANTHROPIC_API_KEY in the environment or pass apiKey inside the LLM config","Ensure dotenv loads before constructing Memory (import 'dotenv/config' at the top of the entry file)","Verify the variable is injected in your deploy target (docker run -e, Kubernetes secrets, CI variables)","Obtain a key from console.anthropic.com if none exists"],"exampleFix":"// before\nnew Memory({ llm: { provider: \"anthropic\" } });\n\n// after\nnew Memory({\n  llm: { provider: \"anthropic\", config: { apiKey: process.env.ANTHROPIC_API_KEY! } },\n});","handlingStrategy":"validation","validationCode":"const apiKey = config.apiKey ?? process.env.ANTHROPIC_API_KEY;\nif (!apiKey) throw new Error(\"ANTHROPIC_API_KEY missing - set it before constructing the LLM\");","typeGuard":"function isMissingAnthropicKey(err: unknown): boolean {\n  return err instanceof Error && err.message === \"Anthropic API key is required\";\n}","tryCatchPattern":"try {\n  new AnthropicLLM(cfg);\n} catch (err) {\n  if (err instanceof Error && err.message === \"Anthropic API key is required\") {\n    throw new Error(\"Config error: set ANTHROPIC_API_KEY or pass llm.config.apiKey\");\n  }\n  throw err;\n}","preventionTips":["Assert required secrets during app bootstrap with a clear failure message","Import dotenv/config at the very top of the entry file","Add deployment checklists verifying ANTHROPIC_API_KEY is injected in CI/Docker"],"tags":["anthropic","api-key","configuration","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}