microsoft/autogen · error · Exception
Missing ANTHROPIC_API_KEY environment variable.
Error message
Missing ANTHROPIC_API_KEY environment variable.
What it means
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').
Source
Thrown at dotnet/samples/AgentChat/AutoGen.Anthropic.Sample/Create_Anthropic_Agent.cs:15
// Copyright (c) Microsoft Corporation. All rights reserved.
// Create_Anthropic_Agent.cs
using AutoGen.Anthropic.Extensions;
using AutoGen.Anthropic.Utils;
using AutoGen.Core;
namespace AutoGen.Anthropic.Sample;
public static class Create_Anthropic_Agent
{
public static async Task RunAsync()
{
#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);
#endregion
#region register_middleware
var agentWithConnector = agent
.RegisterMessageConnector()
.RegisterPrintMessage();
#endregion register_middleware
await agentWithConnector.SendAsync(new TextMessage(Role.Assistant, "Hello", from: "user"));
}
}
View on GitHub (pinned to 027ecf0a37)
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.
Defensive patterns
Strategy: validation
Validate before calling
static string RequireEnv(string name) =>
Environment.GetEnvironmentVariable(name)
?? throw new InvalidOperationException($"Missing {name} environment variable.");
var apiKey = RequireEnv("ANTHROPIC_API_KEY"); Prevention
- 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.
When it happens
Trigger: Executing Create_Anthropic_Agent.RunAsync without ANTHROPIC_API_KEY defined for the process.
Common situations: Fresh clone run without setting secrets; shell/IDE environment mismatch; CI job lacking the secret variable.
Related errors
- Please set ANTHROPIC_API_KEY environment variable.
- Missing ANTHROPIC_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- AZURE_OPENAI_API_KEY is not set
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/717e912f441a2cc0.
Report an issue: GitHub.