microsoft/autogen · error · Exception
Missing MISTRAL_API_KEY environment variable.
Error message
Missing MISTRAL_API_KEY environment variable.
What it means
Startup guard in the Mistral token-count sample: it reads MISTRAL_API_KEY and throws a plain Exception if absent, because MistralClient cannot authenticate without it. The throw happens in RunAsync before the MistralClientAgent or any middleware (token counter, message connector) is constructed; it is sample code, not the Mistral/AutoGen library.
Source
Thrown at dotnet/samples/AgentChat/AutoGen.Basic.Sample/Example14_MistralClientAgent_TokenCount.cs:42
if (reply is IMessage<ChatCompletionResponse> message)
{
responses.Add(message.Content);
}
return reply;
}
public int GetCompletionTokenCount()
{
return responses.Sum(r => r.Usage.CompletionTokens);
}
}
#endregion token_counter_middleware
public static async Task RunAsync()
{
#region create_mistral_client_agent
var apiKey = Environment.GetEnvironmentVariable("MISTRAL_API_KEY") ?? throw new Exception("Missing MISTRAL_API_KEY environment variable.");
var mistralClient = new MistralClient(apiKey);
var agent = new MistralClientAgent(
client: mistralClient,
name: "assistant",
model: MistralAIModelID.OPEN_MISTRAL_7B);
#endregion create_mistral_client_agent
#region register_middleware
var tokenCounterMiddleware = new MistralAITokenCounterMiddleware();
var mistralMessageConnector = new MistralChatMessageConnector();
var agentWithTokenCounter = agent
.RegisterMiddleware(tokenCounterMiddleware)
.RegisterMiddleware(mistralMessageConnector)
.RegisterPrintMessage();
#endregion register_middleware
#region chat_with_agent
await agentWithTokenCounter.SendAsync("write a long, tedious story");View on GitHub (pinned to 027ecf0a37)
Solutions
- Create an API key at console.mistral.ai and export MISTRAL_API_KEY in the launching shell before running the sample.
- Add MISTRAL_API_KEY to the IDE run profile / CI secret store if you launch or build the samples there.
- Confirm the variable name matches exactly — the sample reads only MISTRAL_API_KEY.
- Verify the key has access to OPEN_MISTRAL_7B (the model used by this sample) to avoid a follow-up auth/model error.
Example fix
// before
var apiKey = Environment.GetEnvironmentVariable("MISTRAL_API_KEY") ?? throw new Exception("Missing MISTRAL_API_KEY environment variable.");
// after
var apiKey = Environment.GetEnvironmentVariable("MISTRAL_API_KEY")
?? throw new InvalidOperationException("MISTRAL_API_KEY is not set. Create a key at console.mistral.ai and export it before running."); Defensive patterns
Strategy: validation
Validate before calling
var apiKey = Environment.GetEnvironmentVariable("MISTRAL_API_KEY");
if (string.IsNullOrWhiteSpace(apiKey))
{
Console.Error.WriteLine("MISTRAL_API_KEY is not set. Create a key at console.mistral.ai and export it.");
return;
} Try / catch
try
{
await Example14_MistralClientAgent_TokenCount.RunAsync();
}
catch (Exception ex) when (ex.Message.Contains("MISTRAL_API_KEY"))
{
Console.Error.WriteLine($"Missing Mistral credential: {ex.Message}");
} Prevention
- Per-provider key checklist: OpenAI, Azure, Mistral and Bing samples read different variables — set the one the sample actually uses.
- Use exact variable names (MISTRAL_API_KEY, not MISTRAL_KEY).
- Configure keys via IDE run profiles or CI secrets for reproducible runs.
When it happens
Trigger: Invoking Example14_MistralClientAgent_TokenCount.RunAsync() when MISTRAL_API_KEY is unset/empty in the process. Distinct from the OpenAI samples: this key comes from console.mistral.ai, not OpenAI.
Common situations: Having only OPENAI_API_KEY set (most AutoGen samples need OpenAI) and running the whole sample project; no Mistral account/key provisioned; key exported under a different variable name (e.g. MISTRAL_API_KEY vs MISTRAL_KEY).
Related errors
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- BING_API_KEY environment variable is not set
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/6034f2a3bbcf06c4.
Report an issue: GitHub.