MemPalace/mempalace · error · LLMError

Anthropic provider requires ANTHROPIC_API_KEY env or --llm-a

Error message

Anthropic provider requires ANTHROPIC_API_KEY env or --llm-api-key

What it means

LLMError raised by AnthropicProvider.classify() when no API key is configured. The provider is constructed lazily-valid (check_available deliberately skips probing to avoid paid calls), so the missing key surfaces only at the first real classify() request via the X-API-Key header requirement.

Source

Thrown at mempalace/llm_client.py:410

            api_key_source=source,
        )

    def check_available(self) -> tuple[bool, str]:
        if not self.api_key:
            return False, "ANTHROPIC_API_KEY not set (use --llm-api-key or env)"
        # Don't probe — a live request would cost money. First real call will
        # surface auth errors if the key is invalid.
        return True, "ok"

    def classify(
        self,
        system: str,
        user: str,
        json_mode: bool = True,
        think: Optional[bool] = None,  # noqa: ARG002 — accepted for interface compat; Anthropic extended thinking is configured separately
    ) -> LLMResponse:
        if not self.api_key:
            raise LLMError("Anthropic provider requires ANTHROPIC_API_KEY env or --llm-api-key")
        sys_prompt = system
        if json_mode:
            sys_prompt += "\n\nRespond with valid JSON only, no prose."
        body = {
            "model": self.model,
            "max_tokens": 2048,
            "temperature": 0.1,
            "system": sys_prompt,
            "messages": [{"role": "user", "content": user}],
        }
        headers = {
            "X-API-Key": self.api_key,
            "anthropic-version": self.API_VERSION,
        }
        data = _http_post_json(
            f"{self.endpoint}/v1/messages", body, headers=headers, timeout=self.timeout
        )
        try:

View on GitHub (pinned to 06cb6987f0)

Solutions

  1. Export ANTHROPIC_API_KEY in the environment the process actually runs in
  2. Or pass --llm-api-key / api_key= explicitly when building the provider
  3. For local-only use, switch to the ollama provider instead — no key needed
  4. Add a startup check: provider.api_key is truthy before the first classify

Example fix

# before
provider = build_provider("anthropic", model="claude-sonnet-4-20250514")
provider.classify(s, u)  # LLMError: Anthropic provider requires ANTHROPIC_API_KEY env or --llm-api-key

# after
export ANTHROPIC_API_KEY=sk-ant-...
provider = build_provider("anthropic", model="claude-sonnet-4-20250514")
provider.classify(s, u)
Defensive patterns

Strategy: validation

Validate before calling

import os

if os.environ.get("MP_LLM_PROVIDER") == "anthropic" and not os.environ.get("ANTHROPIC_API_KEY"):
    raise RuntimeError("ANTHROPIC_API_KEY not set — refusing to start BYOK path")

Try / catch

from mempalace.llm_client import LLMError

try:
    provider.classify(s, u)
except LLMError as e:
    if "ANTHROPIC_API_KEY" in str(e):
        raise SystemExit("Set ANTHROPIC_API_KEY or switch to the local ollama provider")
    raise

Prevention

When it happens

Trigger: build_provider("anthropic", model=...) without ANTHROPIC_API_KEY in the environment and without --llm-api-key; the key env var set in a different shell/session than the one running the pipeline.

Common situations: Enabling the external BYOK Anthropic path but forgetting the env var; running under systemd/cron where the interactive shell's exports are absent; CI lacking the secret.

Related errors


AI-assisted analysis of MemPalace/mempalace@06cb6987f0 (2026-08-15). Data as JSON: /api/errors/7e0d292708a33e06. Report an issue: GitHub.