abi/screenshot-to-code · critical · Exception

Anthropic API key is missing.

Error message

Anthropic API key is missing.

What it means

Raised by create_provider_session when the selected model is in ANTHROPIC_MODELS but anthropic_api_key (explicit arg) and the ANTHROPIC_API_KEY env var are both empty. Fail-fast guard before the AsyncAnthropic client is built; same pattern as the OpenAI/Gemini guards in the same factory.

Source

Thrown at backend/agent/providers/factory.py:56

        screenshot_enabled=is_screenshot_preview_available(),
    )

    if model in OPENAI_MODELS:
        if not openai_api_key:
            raise Exception("OpenAI API key is missing.")

        client = AsyncOpenAI(api_key=openai_api_key, base_url=openai_base_url)
        return OpenAIProviderSession(
            client=client,
            model=model,
            prompt_messages=prompt_messages,
            tools=serialize_openai_tools(canonical_tools),
            recorder=recorder,
        )

    if model in ANTHROPIC_MODELS:
        if not anthropic_api_key:
            raise Exception("Anthropic API key is missing.")

        client = AsyncAnthropic(api_key=anthropic_api_key)
        return AnthropicProviderSession(
            client=client,
            model=model,
            prompt_messages=prompt_messages,
            tools=serialize_anthropic_tools(canonical_tools),
            recorder=recorder,
        )

    if model in GEMINI_MODELS:
        if not gemini_api_key:
            raise Exception("Gemini API key is missing.")

        client = genai.Client(api_key=gemini_api_key)
        return GeminiProviderSession(
            client=client,
            model=model,

View on GitHub (pinned to d026163f58)

Solutions

  1. Set ANTHROPIC_API_KEY in backend/.env and restart uvicorn.
  2. Or supply the key via the in-app Settings dialog.
  3. Confirm the env var reaches the process (printenv ANTHROPIC_API_KEY in the server's shell).
  4. Pick a model matching a key you already have.

Example fix

# backend/.env
ANTHROPIC_API_KEY=sk-ant-...
Defensive patterns

Strategy: validation

Validate before calling

import os
if model in ANTHROPIC_MODELS and not os.environ.get("ANTHROPIC_API_KEY"):
    raise SystemExit("Set ANTHROPIC_API_KEY in backend/.env before using Claude models")

Try / catch

try:
    session = create_provider_session(model=model, ...)
except Exception as e:
    if "API key is missing" in str(e):
        return {"error": "missing_key", "provider": "anthropic"}

Prevention

When it happens

Trigger: Generation requested with a Claude-family model while ANTHROPIC_API_KEY is unset in both the process environment and the request parameters.

Common situations: backend/.env missing or not loaded (load_dotenv path wrong), server started before the key was added, key provided only for a different provider, or CI environment without secrets.

Related errors


AI-assisted analysis of abi/screenshot-to-code@d026163f58 (2026-08-14). Data as JSON: /api/errors/8f907ff0d8d2b5e2. Report an issue: GitHub.