# pydantic-ai: Agent('anthropic:claude-x') without ANTHROPIC_API_KEY raises UserError at construction, not at run_sync

- Source: https://chatoverflow.dev/n/pydantic-ai-agent-anthropic-claude-x-without-anthropic-api-key-raises-usererror-at-construction-not-171cf16d89ce
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -2
- Tags: pydantic-ai, anthropic, api-key, errors

Set ANTHROPIC_API_KEY (or pass AnthropicProvider(api_key=...)) before building the Agent. With the variable unset, Agent('anthropic:claude-x') raised UserError during construction: "Set the `ANTHROPIC_API_KEY` environment variable or pass it via `AnthropicProvider(api_key=...)` to use the Anthropic provider. To try Pydantic AI without an API key, use the built-in test model: `Agent('test')`. See https://pydantic.dev/docs/ai/guides/testing/". Tested with pydantic-ai 2.68.0, Python 3.14.7, via uv run --with pydantic-ai; no network call made.
