Semantic Kernel Prompt Created: 18 Jan 2026 Updated: 01 Feb 2026

A Guide to OpenAI and Semantic Kernel in .NET

In modern software development, integrating Large Language Models (LLMs) into daily workflows is becoming a standard. However, a common pitfall is hardcoding sensitive API keys directly into the source code. This article demonstrates how to securely handle your OpenAI API Key and use it within a .NET 9 console application to break down complex human tasks into actionable steps.

1. The Security Layer: Windows Environment Variables

Before writing code, you must store your API key in a location that is not tracked by version control systems like Git. On Windows, the most effective way is using Environment Variables.

How to set the variable:

  1. Open PowerShell or Command Prompt.
  2. Run the following command (replace the placeholder with your actual key):
  3. PowerShell
setx OPEN_AI_KEY "your_actual_openai_key_here"
  1. Crucial Step: Restart your IDE (JetBrains Rider, Visual Studio, or VS Code) after running this command. Applications only "see" new environment variables when they are first launched.

2. The Implementation: Task Decomposition with Semantic Kernel

In this example, we use Microsoft Semantic Kernel to act as a "Task Organizer." The goal is to take a complex request—like preparing breakfast—and have the AI generate a sequence of simple instructions.

The .NET Code

using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;

// 1. Securely retrieving the key from the OS
var apiKey = Environment.GetEnvironmentVariable("OPEN_AI_KEY");

if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("Please set the OPEN_AI_KEY environment variable.");
return;
}

// 2. Initializing the OpenAI Chat Service
var textGeneration = new OpenAIChatCompletionService(modelId: "gpt-4o", apiKey: apiKey);

// 3. Defining the "System Prompt" for task orchestration
var prompt = """
You are an AI assistant helping to organize daily tasks.
Your task is to break down complex activities into simple,
actionable steps.
Provide only the step-by-step breakdown without any additional
explanations.
## Complex task:
"Prepare a healthy breakfast with coffee and clean up the kitchen
afterwards."
""";

// 4. Executing the request
var response = await textGeneration.GetChatMessageContentAsync(prompt);

Console.WriteLine("--- Daily Task Breakdown ---");
Console.WriteLine(response);

3. Troubleshooting: Why is the Key Returning Null?

If you run the code and see the "Please set the environment variable" message, check these three common issues:

  1. The Typo Trap: Ensure the string in Environment.GetEnvironmentVariable("OPEN_AI_KEY") matches your Windows variable name exactly. If you saved it as OPENAI_API_KEY in Windows but called it OPEN_AI_KEY in C#, it will return null.
  2. The Restart Requirement: If you added the variable while your IDE was open, the IDE is still running on the "old" environment state. Restart the IDE completely.
  3. Scope Issues: Ensure you added it to User Variables (specific to you) rather than System Variables if you don't have Administrator rights.

4. Why Use Semantic Kernel?

While you could use a standard HttpClient to call OpenAI, Semantic Kernel provides several professional advantages:

  1. Abstraction: Easily switch between OpenAI, Azure OpenAI, or Hugging Face without changing your core logic.
  2. Middleware: Built-in support for logging, telemetry, and retry policies.
  3. Future-Proofing: It is designed to handle "Plugins" and "Functions," allowing the AI to eventually execute the tasks it generates.
Share this lesson: