microsoft/autogen · error · Exception

Missing ANTHROPIC_API_KEY environment variable.

Error message

Missing ANTHROPIC_API_KEY environment variable.

What it means

Same ANTHROPIC_API_KEY guard as the other Anthropic samples, this time in the tool-calling sample (Create_Anthropic_Agent_With_Tool). Thrown before AnthropicClient construction so the agent never gets an invalid empty key. The sample demonstrates tools: [tool], which requires a working client.

Source

Thrown at dotnet/samples/AgentChat/AutoGen.Anthropic.Sample/Create_Anthropic_Agent_With_Tool.cs:66

            }
        };

        var weatherFunction = new WeatherFunction();
        var functionMiddleware = new FunctionCallMiddleware(
            functions: [
                weatherFunction.GetWeatherFunctionContract,
            ],
            functionMap: new Dictionary<string, Func<string, Task<string>>>
            {
                { weatherFunction.GetWeatherFunctionContract.Name!, weatherFunction.GetWeatherWrapper },
            });

        #endregion

        #region create_anthropic_agent

        var apiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY") ??
                     throw new Exception("Missing ANTHROPIC_API_KEY environment variable.");

        var anthropicClient = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, apiKey);
        var agent = new AnthropicClientAgent(anthropicClient, "assistant", AnthropicConstants.Claude3Haiku,
            tools: [tool]); // Define tools for AnthropicClientAgent
        #endregion

        #region register_middleware

        var agentWithConnector = agent
            .RegisterMessageConnector()
            .RegisterPrintMessage()
            .RegisterStreamingMiddleware(functionMiddleware);
        #endregion register_middleware

        #region single_turn
        var question = new TextMessage(Role.Assistant,
            "What is the weather like in San Francisco?",
            from: "user");

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set ANTHROPIC_API_KEY in the environment before running the sample.
  2. If embedding the snippet in your app, load the key from your configuration/secret store rather than raw environment.
  3. Restart terminals/IDE after setting the variable.
  4. Print Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY") != null to confirm visibility before rerunning.
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY")))
{
    Console.Error.WriteLine("ANTHROPIC_API_KEY is required for tool-calling samples.");
    return;
}

Prevention

When it happens

Trigger: Running Create_Anthropic_Agent_With_Tool.RunAsync with ANTHROPIC_API_KEY unset/null.

Common situations: Copied the #region create_anthropic_agent snippet into your own project without also copying the env-var setup; env var defined only in a CI stage that doesn't reach the test runner.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/0ff5933a98fd3705. Report an issue: GitHub.