{"record":{"id":"717e912f441a2cc0","repo":"microsoft/autogen","slug":"missing-anthropic-api-key-environment-variable","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.cs","lineNumber":15,"sourceCode":"// Copyright (c) Microsoft Corporation. All rights reserved.\n// Create_Anthropic_Agent.cs\n\nusing AutoGen.Anthropic.Extensions;\nusing AutoGen.Anthropic.Utils;\nusing AutoGen.Core;\n\nnamespace AutoGen.Anthropic.Sample;\n\npublic static class Create_Anthropic_Agent\n{\n    public static async Task RunAsync()\n    {\n        #region create_anthropic_agent\n        var apiKey = Environment.GetEnvironmentVariable(\"ANTHROPIC_API_KEY\") ?? throw new Exception(\"Missing ANTHROPIC_API_KEY environment variable.\");\n        var anthropicClient = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, apiKey);\n        var agent = new AnthropicClientAgent(anthropicClient, \"assistant\", AnthropicConstants.Claude3Haiku);\n        #endregion\n\n        #region register_middleware\n        var agentWithConnector = agent\n            .RegisterMessageConnector()\n            .RegisterPrintMessage();\n        #endregion register_middleware\n\n        await agentWithConnector.SendAsync(new TextMessage(Role.Assistant, \"Hello\", from: \"user\"));\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Anthropic.Sample/Create_Anthropic_Agent.cs#L1-L29","documentation":"Guard exception in the 'create an Anthropic agent' documentation snippet: ANTHROPIC_API_KEY is null, so the code throws instead of building an AnthropicClient with an empty credential. Identical pattern to the other Anthropic samples; the message differs only in wording ('Missing' vs 'Please set').","triggerScenarios":"Executing Create_Anthropic_Agent.RunAsync without ANTHROPIC_API_KEY defined for the process.","commonSituations":"Fresh clone run without setting secrets; shell/IDE environment mismatch; CI job lacking the secret variable.","solutions":["Export ANTHROPIC_API_KEY before dotnet run.","For CI, add it as a masked secret variable in the pipeline.","Use launchSettings.json or .env loading so IDE runs see it too.","Verify the key with a minimal curl to the Anthropic API if the error persists after setting it."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"static string RequireEnv(string name) =>\n    Environment.GetEnvironmentVariable(name)\n    ?? throw new InvalidOperationException($\"Missing {name} environment variable.\");\n\nvar apiKey = RequireEnv(\"ANTHROPIC_API_KEY\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Centralize env-var access in one RequireEnv helper across samples.","Set keys in launchSettings.json for IDE runs so F5 always works.","Validate credentials at startup, not on first API call."],"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"}