In this lesson, we'll build a simple console-based chatbot that:
Chat with the User
Connects to an AI model hosted by Groq.
Displays the AI response.
Maintains conversation history, so the bot remembers earlier messages in the current session.
We'll use Microsoft.Extensions.AI with the OpenAI-compatible client.
An AI provider is a company or platform that hosts, runs, and delivers access to AI models over the internet. Instead of buying expensive supercomputers to run massive AI programs on your own device, you connect to a provider's servers to send requests and get answers back.
First-Party Model Creators: These companies invent, train, and host their own proprietary AI models (for example, OpenAI with GPT and Google with Gemini).
Inference & Hosting Platforms: These companies do not build their own AI models. Instead, they take existing open-source models and run them on specialized, ultra-fast hardware (for example, Groq, Together AI, and Fireworks AI).
Cloud Aggregators: Tech giants that offer a single storefront where businesses can access many different AI models from multiple creators under one roof (for example, Amazon Bedrock and Microsoft Azure).
A simple analogy:
Model: The AI brain that generates the response.
Provider: The service that makes the model available.
API: The communication interface your application uses.
API key: The credential that identifies and authorizes your application.
Your application sends a request to Groq, Groq runs the selected model, and the response comes back to your application.
A model is the actual language model that processes your prompt and generates an answer. Think of a model as the brain that generates the response.
For example:
llama-3.3-70b-versatile is a model name you can use with Groq, subject to current availability.
GPT models are available through OpenAI.
Gemini models are available through Google.
Your application sends a request to a provider, the provider runs the selected model, and your application receives the response.
Different models have different:
Capabilities: reasoning, coding, summarization, etc.
Context windows: how much input and conversation history they can process.
Output limits: how many tokens they can generate in one response.
Speed and cost: how quickly and at what price they respond.
Groq's model catalogue currently lists openai/gpt-oss-20b with a 131,072-token context window and a maximum completion of 65,536 tokens. Those are model capabilities, not a promise that your account can use the entire allowance in every request.
Open the official Groq developer console:
Sign up or sign in.
Open the API Keys section.
Select Create API Key.
Give it a name, such as CSharpConsoleChatbot.
Copy the generated key and store it securely.
The API key is your application's credential. Never share it, commit it to Git, or put it in Angular/browser code. If a real key is accidentally exposed, revoke it and create a replacement.
Groq offers a Free plan and paid Developer options. Free access is subject to model-specific limits.
Some terms you'll see:
Term Meaning
RPM Requests per minute
RPD Requests per day
TPM Tokens per minute
TPD Tokens per day
Context window Maximum tokens the model can process in a request, including conversation input and generated output
Max output tokens Maximum tokens the model can generate in one response
For example, a model might support a 131,072-token context window but have a much smaller output limit or account-level rate limits.
Where to check:
Limits can change and differ by model and account. Always check your own console rather than relying on a fixed number from a tutorial.
For our first chatbot, we'll use a model that is currently listed in Groq's supported model catalogue.
One option is: openai/gpt-oss-20b
This is an open-weight model served by Groq. Despite the openai prefix, it is not the same thing as calling OpenAI's hosted GPT API.
Another option is: llama-3.3-70b-versatile
Check the model catalogue before using either, because model availability and lifecycle can change.
Suppose you have this conversation:
You: What is AI?
AI: AI is...
You: What is machine learning?
AI: Machine learning is...
You: Explain neural networks.
AI: Neural networks are...
The chatbot sends the conversation history along with the latest question. The model uses that history to understand the conversation.
The context window is the maximum amount of tokens the model can process for that request, including the relevant input and generated output.
A larger context window can accommodate more conversation history, but it doesn't mean the model remembers your conversations forever. Our console application will keep history only in memory while it is running.
Hands-on implementation
Open Visual Studio or VS Code terminal and run following commands.
dotnet new console -n GroqChatBot
cd GroqChatBot
Run:
dotnet add package Microsoft.Extensions.AI.OpenAI
dotnet add package Microsoft.Extensions.Hosting
Why these packages?
Package Purpose
Microsoft.Extensions.AI.OpenAI Provides the OpenAI client adapter for IChatClient
Microsoft.Extensions.Hosting Provides configuration and User Secrets integration
The IChatClient abstraction allows us to work with a common .NET chat interface.
Create or update appsettings.json in your project.
Notice that we haven't added the API key here.
In the project directory, execute:
dotnet user-secrets init
This creates a User Secrets identifier in your project file.
Now execute:
dotnet user-secrets set "Groq:ApiKey" "YOUR_GROQ_API_KEY"
Replace YOUR_GROQ_API_KEY with the key you generated.
Your API key is stored outside your project directory and won't be included in your Git commits.
You can also manage it through Visual Studio:
Right-click the project.
Select Manage User Secrets.
Add:
{
"Groq:ApiKey": "YOUR_GROQ_API_KEY"
}
dotnet run
Let's connect the code to the AI concepts we've already studied.
Code What it means
IChatClient Common .NET interface for communicating with a chat model
ChatRole.System Instructions that guide the assistant
ChatRole.User User's message
ChatRole.Assistant Model's previous response
ChatOptions Controls output length and generation settings
GetResponseAsync() Sends the conversation and waits for the response
memory Keeps the conversation history in application memory
Temperature = 0.7f Controls response variation
MaxOutputTokens = 300 Limits generated output tokens
Look at this line:
ChatResponse response = await chatClient.GetResponseAsync(memory, options);
We send the entire memory list, not just the latest question.
The model receives this conversation and can use the earlier messages to answer the latest question.
Important: The memory list is not the AI model's permanent memory. It is just a C# list stored in your application's RAM.
If you close the application, the list disappears.
Also, the list grows with every exchange. Eventually, the conversation can exceed the model's context limit. For a learning application, this is fine; in a production application, you would need history management, such as limiting old messages or summarizing earlier conversation.