{"record":{"id":"6034f2a3bbcf06c4","repo":"microsoft/autogen","slug":"missing-mistral-api-key-environment-variable-6034f2","errorCode":null,"errorMessage":"Missing MISTRAL_API_KEY environment variable.","messagePattern":"Missing MISTRAL_API_KEY environment variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentChat/AutoGen.Basic.Sample/Example14_MistralClientAgent_TokenCount.cs","lineNumber":42,"sourceCode":"            if (reply is IMessage<ChatCompletionResponse> message)\n            {\n                responses.Add(message.Content);\n            }\n\n            return reply;\n        }\n\n        public int GetCompletionTokenCount()\n        {\n            return responses.Sum(r => r.Usage.CompletionTokens);\n        }\n    }\n    #endregion token_counter_middleware\n\n    public static async Task RunAsync()\n    {\n        #region create_mistral_client_agent\n        var apiKey = Environment.GetEnvironmentVariable(\"MISTRAL_API_KEY\") ?? throw new Exception(\"Missing MISTRAL_API_KEY environment variable.\");\n        var mistralClient = new MistralClient(apiKey);\n        var agent = new MistralClientAgent(\n            client: mistralClient,\n            name: \"assistant\",\n            model: MistralAIModelID.OPEN_MISTRAL_7B);\n        #endregion create_mistral_client_agent\n\n        #region register_middleware\n        var tokenCounterMiddleware = new MistralAITokenCounterMiddleware();\n        var mistralMessageConnector = new MistralChatMessageConnector();\n        var agentWithTokenCounter = agent\n            .RegisterMiddleware(tokenCounterMiddleware)\n            .RegisterMiddleware(mistralMessageConnector)\n            .RegisterPrintMessage();\n        #endregion register_middleware\n\n        #region chat_with_agent\n        await agentWithTokenCounter.SendAsync(\"write a long, tedious story\");","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/samples/AgentChat/AutoGen.Basic.Sample/Example14_MistralClientAgent_TokenCount.cs#L24-L60","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before\nvar apiKey = Environment.GetEnvironmentVariable(\"MISTRAL_API_KEY\") ?? throw new Exception(\"Missing MISTRAL_API_KEY environment variable.\");\n\n// after\nvar apiKey = Environment.GetEnvironmentVariable(\"MISTRAL_API_KEY\")\n    ?? throw new InvalidOperationException(\"MISTRAL_API_KEY is not set. Create a key at console.mistral.ai and export it before running.\");","handlingStrategy":"validation","validationCode":"var apiKey = Environment.GetEnvironmentVariable(\"MISTRAL_API_KEY\");\nif (string.IsNullOrWhiteSpace(apiKey))\n{\n    Console.Error.WriteLine(\"MISTRAL_API_KEY is not set. Create a key at console.mistral.ai and export it.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    await Example14_MistralClientAgent_TokenCount.RunAsync();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"MISTRAL_API_KEY\"))\n{\n    Console.Error.WriteLine($\"Missing Mistral credential: {ex.Message}\");\n}","preventionTips":["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."],"tags":["configuration","environment-variables","mistral","dotnet","autogen"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}