{"record":{"id":"86ba6377e34cdb23","repo":"microsoft/autogen","slug":"please-set-anthropic-api-key-environment-variable","errorCode":null,"errorMessage":"Please set ANTHROPIC_API_KEY environment variable.","messagePattern":"Please set ANTHROPIC_API_KEY environment variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentChat/AutoGen.Anthropic.Sample/Anthropic_Agent_With_Prompt_Caching.cs","lineNumber":97,"sourceCode":"\n                                    Bob looked at her, a sad smile on his face. “I have to make sure it’s over. This is the only way.”\n\n                                    Sarah’s eyes filled with tears, but she nodded, understanding the gravity of his decision. “Thank you, Bob. For everything.”\n\n                                    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.\n\n                                    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.\n\n                                    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.\n                                    \"\"\";\n    #endregion\n\n    public static async Task RunAsync()\n    {\n        #region init translator agents & register middlewares\n\n        var apiKey = Environment.GetEnvironmentVariable(\"ANTHROPIC_API_KEY\") ??\n                     throw new Exception(\"Please set ANTHROPIC_API_KEY environment variable.\");\n        var anthropicClient = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, apiKey);\n        var frenchTranslatorAgent =\n            new AnthropicClientAgent(anthropicClient, \"frenchTranslator\", AnthropicConstants.Claude35Sonnet,\n                    systemMessage: \"You are a French translator\")\n                .RegisterMessageConnector()\n                .RegisterPrintMessage();\n\n        var germanTranslatorAgent = new AnthropicClientAgent(anthropicClient, \"germanTranslator\",\n                AnthropicConstants.Claude35Sonnet, systemMessage: \"You are a German translator\")\n            .RegisterMessageConnector()\n            .RegisterPrintMessage();\n\n        #endregion\n\n        var userProxyAgent = new UserProxyAgent(\n                name: \"user\",\n                humanInputMode: HumanInputMode.ALWAYS)\n            .RegisterPrintMessage();","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Anthropic.Sample/Anthropic_Agent_With_Prompt_Caching.cs#L79-L115","documentation":"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.","triggerScenarios":"Running AutoGen.Anthropic.Sample's Anthropic_Agent_With_Prompt_Caching.RunAsync without ANTHROPIC_API_KEY in the environment.","commonSituations":"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.","solutions":["Set ANTHROPIC_API_KEY in the environment the sample runs under: export ANTHROPIC_API_KEY=sk-ant-...","If the repo supports DotNetEnv, ensure the .env file exists at repo root with the key and is loaded before this line.","In an IDE, add the variable to the run configuration/launch profile.","Confirm the key is valid for the Claude 3.5 Sonnet model used by AnthropicConstants.Claude35Sonnet."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var apiKey = Environment.GetEnvironmentVariable(\"ANTHROPIC_API_KEY\");\nif (string.IsNullOrWhiteSpace(apiKey))\n{\n    Console.Error.WriteLine(\"Missing ANTHROPIC_API_KEY. Get one at console.anthropic.com.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try { RunAsync().Wait(); }\ncatch (Exception e) when (e.InnerException?.Message.Contains(\"ANTHROPIC_API_KEY\") == true)\n{\n    Console.Error.WriteLine(\"Set ANTHROPIC_API_KEY before running this sample.\");\n}","preventionTips":["Load a .env file (DotNetEnv) at sample startup for all API keys.","Keep a single helper that validates all required env vars before building agents.","Document required variables in each sample's README."],"tags":["environment","api-key","dotnet","sample","anthropic"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}