{"record":{"id":"0ff5933a98fd3705","repo":"microsoft/autogen","slug":"missing-anthropic-api-key-environment-variable-0ff593","errorCode":null,"errorMessage":"Missing ANTHROPIC_API_KEY environment variable.","messagePattern":"Missing ANTHROPIC_API_KEY environment variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentChat/AutoGen.Anthropic.Sample/Create_Anthropic_Agent_With_Tool.cs","lineNumber":66,"sourceCode":"            }\n        };\n\n        var weatherFunction = new WeatherFunction();\n        var functionMiddleware = new FunctionCallMiddleware(\n            functions: [\n                weatherFunction.GetWeatherFunctionContract,\n            ],\n            functionMap: new Dictionary<string, Func<string, Task<string>>>\n            {\n                { weatherFunction.GetWeatherFunctionContract.Name!, weatherFunction.GetWeatherWrapper },\n            });\n\n        #endregion\n\n        #region create_anthropic_agent\n\n        var apiKey = Environment.GetEnvironmentVariable(\"ANTHROPIC_API_KEY\") ??\n                     throw new Exception(\"Missing ANTHROPIC_API_KEY environment variable.\");\n\n        var anthropicClient = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, apiKey);\n        var agent = new AnthropicClientAgent(anthropicClient, \"assistant\", AnthropicConstants.Claude3Haiku,\n            tools: [tool]); // Define tools for AnthropicClientAgent\n        #endregion\n\n        #region register_middleware\n\n        var agentWithConnector = agent\n            .RegisterMessageConnector()\n            .RegisterPrintMessage()\n            .RegisterStreamingMiddleware(functionMiddleware);\n        #endregion register_middleware\n\n        #region single_turn\n        var question = new TextMessage(Role.Assistant,\n            \"What is the weather like in San Francisco?\",\n            from: \"user\");","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Anthropic.Sample/Create_Anthropic_Agent_With_Tool.cs#L48-L84","documentation":"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.","triggerScenarios":"Running Create_Anthropic_Agent_With_Tool.RunAsync with ANTHROPIC_API_KEY unset/null.","commonSituations":"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.","solutions":["Set ANTHROPIC_API_KEY in the environment before running the sample.","If embedding the snippet in your app, load the key from your configuration/secret store rather than raw environment.","Restart terminals/IDE after setting the variable.","Print Environment.GetEnvironmentVariable(\"ANTHROPIC_API_KEY\") != null to confirm visibility before rerunning."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(\"ANTHROPIC_API_KEY\")))\n{\n    Console.Error.WriteLine(\"ANTHROPIC_API_KEY is required for tool-calling samples.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Copy the env-var setup lines together with any #region snippet you extract.","Add a pre-run credential check in the sample's Main.","Store keys in user-secrets, not in code."],"tags":["environment","api-key","dotnet","sample","anthropic","tools"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}