microsoft/autogen · error · Exception

Please set ANTHROPIC_API_KEY environment variable.

Error message

Please set ANTHROPIC_API_KEY environment variable.

What it means

Guard exception in the Anthropic prompt-caching sample: Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY") returned null, so the sample throws before constructing AnthropicClient. The sample then builds French/German translator agents, all of which need this key.

Source

Thrown at dotnet/samples/AgentChat/AutoGen.Anthropic.Sample/Anthropic_Agent_With_Prompt_Caching.cs:97

                                    Bob looked at her, a sad smile on his face. “I have to make sure it’s over. This is the only way.”

                                    Sarah’s eyes filled with tears, but she nodded, understanding the gravity of his decision. “Thank you, Bob. For everything.”

                                    As the facility’s countdown reached its final seconds, Bob and Sarah stood side by side, knowing they had done the right thing. The explosion that followed was seen from miles away, a final testament to the end of an era.

                                    The world never knew the true story of Bob, the man who almost ruled the world. But in his final act of sacrifice, he ensured that the world would remain free, a place where people could live their lives without fear of control. Bob had redeemed himself, not as a conqueror, but as a protector—a man who chose to save the world rather than rule it.

                                    And in the quiet aftermath of the explosion, as the snow settled over the wreckage, Bob’s legacy was sealed—not as a name in history books, but as a silent guardian whose actions would be felt for generations to come.
                                    """;
    #endregion

    public static async Task RunAsync()
    {
        #region init translator agents & register middlewares

        var apiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY") ??
                     throw new Exception("Please set ANTHROPIC_API_KEY environment variable.");
        var anthropicClient = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, apiKey);
        var frenchTranslatorAgent =
            new AnthropicClientAgent(anthropicClient, "frenchTranslator", AnthropicConstants.Claude35Sonnet,
                    systemMessage: "You are a French translator")
                .RegisterMessageConnector()
                .RegisterPrintMessage();

        var germanTranslatorAgent = new AnthropicClientAgent(anthropicClient, "germanTranslator",
                AnthropicConstants.Claude35Sonnet, systemMessage: "You are a German translator")
            .RegisterMessageConnector()
            .RegisterPrintMessage();

        #endregion

        var userProxyAgent = new UserProxyAgent(
                name: "user",
                humanInputMode: HumanInputMode.ALWAYS)
            .RegisterPrintMessage();

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set ANTHROPIC_API_KEY in the environment the sample runs under: export ANTHROPIC_API_KEY=sk-ant-...
  2. If the repo supports DotNetEnv, ensure the .env file exists at repo root with the key and is loaded before this line.
  3. In an IDE, add the variable to the run configuration/launch profile.
  4. Confirm the key is valid for the Claude 3.5 Sonnet model used by AnthropicConstants.Claude35Sonnet.
Defensive patterns

Strategy: validation

Validate before calling

var apiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
    Console.Error.WriteLine("Missing ANTHROPIC_API_KEY. Get one at console.anthropic.com.");
    return;
}

Try / catch

try { RunAsync().Wait(); }
catch (Exception e) when (e.InnerException?.Message.Contains("ANTHROPIC_API_KEY") == true)
{
    Console.Error.WriteLine("Set ANTHROPIC_API_KEY before running this sample.");
}

Prevention

When it happens

Trigger: Running AutoGen.Anthropic.Sample's Anthropic_Agent_With_Prompt_Caching.RunAsync without ANTHROPIC_API_KEY in the environment.

Common situations: Running the sample project directly without a .env loader; CI pipeline missing the secret; key set under a different name (e.g. CLAUDE_API_KEY or OPENAI_API_KEY) by mistake.

Related errors


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