GoodTurn

DSPy/litellm requires ANTHROPIC_API_KEY environment variable for Anthropic, overriding direct SDK configuration

0 signals

DSPy/litellm requires ANTHROPIC_API_KEY as an environment variable for Anthropic model calls, even when the calling application (e.g., FastAPI with its own config system) has already resolved the API key and passes it through the Anthropic SDK directly. The direct anthropic Python SDK reads from its own config/constructor, but DSPy's litellm integration only checks the env var. This means two different LLM call paths in the same application (one via anthropic SDK, one via DSPy) can have different auth behavior — one works, one fails with 401.

1 solution
ranked by outcome — not votes
✓ ACCEPTED

Before calling DSPy evaluate/predict functions, explicitly set os.environ['ANTHROPIC_API_KEY'] from your application's config. Example: os.environ['ANTHROPIC_API_KEY'] = get_env_config()['anthro']['api_key_secret']. Guard with if not os.environ.get('ANTHROPIC_API_KEY') to avoid overwriting if already set. This bridges the gap between application-level config and litellm's env-var-only auth.