In today’s rapidly evolving AI landscape, there has never been a better time to be a .NET developer exploring artificial intelligence. With the latest libraries and APIs, integrating large language models (LLMs) into your .NET applications has become straightforward, efficient, and consistent.

One of the key tools enabling this seamless experience is Microsoft.Extensions.AI — a library designed to provide clean abstractions for working with various AI providers. In this article, we’ll walk through how to use GitHub Models with .NET 9, set up the OpenAI adapter, configure a chat client, and get your application talking directly to a model in just a few lines of code.

1. Setting Up the .NET 9 Console App

We’ll start with a basic .NET 9 console application. Once it’s ready, the first step is to add the required libraries.

Installing the Required Libraries

  1. Open the NuGet Package Manager in your project.

  2. In the search box, type Microsoft.Extensions.AI.

  3. Instead of installing this package directly, install Microsoft.Extensions.AI.OpenAI — this is the package required to connect to GitHub Models.

  4. The OpenAI package automatically includes the core Microsoft.Extensions.AI library as a dependency, so there’s no need to install it separately.

After installation, you should see the library added to your project. Once it’s confirmed, clean up your Program.cs file to prepare for the next steps.

2. Creating and Configuring the Chat Client

The IChatClient interface is the main abstraction provided by Microsoft.Extensions.AI that we’ll use to communicate with large language models in .NET.

Defining the Chat Client

Start by creating a variable of type IChatClient and assigning it to a new chat client instance.

  • Model Name:
    This parameter defines which large language model you want to use from GitHub Models. We’ll fill it in later.

  • API Key:
    The API key authenticates your application with GitHub Models. Without it, requests won’t work. Leave it blank for now — we’ll add the real key later.

  • Endpoint:
    Inside the OpenAIClientOptions, there’s an Endpoint property. Since we’re connecting to GitHub Models, use the following URL:

    https://api.github.com/models

    This ensures your client sends all requests directly to GitHub Models.

Finally, call .AsChatClient() to convert your provider-specific chat client into the standard IChatClient interface.
This step is important — it keeps your code clean, maintainable, and provider-agnostic. If you decide to switch from GitHub Models to another AI provider in the future, your application logic won’t need to change.

3. Generating the GitHub API Key

Next, let’s create a GitHub API key to enable access to the models.

  1. Visit the GitHub Models Marketplace. (A link is usually available in the model documentation or the official GitHub page.)

  2. In the model selection dropdown, browse through the available models.

  3. For this example, we’ll use GPT-4.1 Mini.

  4. Click “Use this model”.

  5. If you’re using the free tier, click “Create personal access token” to generate your API key.

Creating the Token

  • Set the expiration (default is 30 days).

  • Give your token a name, for example: YouTube GitHub Model Token.

  • Click “Generate token” to create it.

Once the token appears, copy it immediately — you’ll only see it once. Keep it somewhere safe and never hard-code it in a real application. For demonstration purposes, we’ll paste it directly into the code here, but remember to use environment variables or a secrets manager in production.

After pasting the token, add your model name ("gpt-4.1-mini") to complete the chat client configuration.

4. Building the Chat Application Logic

Now that the chat client is ready, let’s add the actual chat functionality.

Displaying a Welcome Message

When the application starts, display a short welcome message and instructions on how to exit the chat.

Maintaining Conversation History

To enable contextual responses, create a list that stores all messages exchanged between the user and the model.
This list will hold both user messages and assistant responses, allowing the model to “remember” context throughout the conversation.

Running the Chat Loop

The chat application should run continuously until the user types exit.
Here’s the basic loop logic:

  1. Use a while (true) loop to keep the chat running.

  2. Display a console prompt so the user knows when to type input.

  3. Read the user’s message.

  4. If the user presses Enter without typing anything, skip and ask again.

  5. If the user types exit, break the loop and end the chat.

Each valid user message is added to the chat history, and the entire conversation is passed to the model for the next response.


5. Streaming Model Responses in Real Time

Before printing the model’s reply, display a small label like “Assistant:” to make it clear which messages come from the model.

Use the GetStreamingResponseAsync() method to stream the model’s response token by token.
This approach lets you see the output appear in real time, just like chatting with a live AI assistant.

Use Console.Write() to print each token as it arrives, creating an interactive and dynamic chat experience.
Meanwhile, store the full response in a variable (e.g., assistantResponse) by appending each token as it streams in.
Once the model finishes, add the complete response to the chat history for contextual memory.

6. Enhancing the User Experience

Right now, both user input and model responses appear in the same console color, which can be confusing.
Let’s improve that with color formatting:

  • Welcome message: Yellow

  • User input: White

  • Assistant response: Green

This small change makes the chat more readable and visually pleasant.

After updating the colors, run the app again — you’ll see the difference immediately.
Messages are now clearly separated, and the assistant’s replies stream in smoothly in real time.

7. Testing the Chat Application

Let’s test our new chat app:

  • Type: What’s .NET?
    → The assistant instantly responds, explaining what .NET is.

  • Next, try: Write a story about a superhero.
    → The model begins streaming a creative story live in the console — just like an AI chat assistant.

  • Finally, test context retention:

    1. Type Add 10 and 5.
      → The model responds 10 + 5 = 15.

    2. Then type Minus 4.
      → The model replies 15 - 4 = 11.

Notice how it remembered the previous number without repeating the full prompt.
That’s because we’re passing the complete conversation history with every new request.

8. Wrapping Up

And that’s it!
You’ve just seen how easily .NET can integrate with AI and large language models using the Microsoft.Extensions.AI library.

With just a few lines of code, we:

  • Connected a .NET 9 console app to GitHub Models

  • Streamed real-time responses

  • Maintained conversation history

  • Built a simple, interactive chat system

This demonstrates how powerful and flexible the new .NET AI ecosystem has become. It’s simple, fast, and clean to implement — ideal for developers looking to build intelligent, conversational applications.