Skip to main content

Prerequisites

Before you begin, make sure you have:
  • .NET 6.0 or later installed
  • An OpenAI API key
  • Visual Studio, VS Code, or your preferred C# IDE

Installation

Install the AutoGen package, which includes all core features:
For more granular control, see the Installation Guide.

Your First Agent Conversation

Let’s create a simple conversation between a user and an AI assistant.
1

Create a new console application

2

Set up your OpenAI API key

Set your OpenAI API key as an environment variable:
3

Create the application

Replace the contents of Program.cs with:
Program.cs
4

Run the application

The application will start a conversation where you can interact with the AI assistant. Type your messages and press Enter. The conversation continues until you reach the maximum rounds or terminate it.

Understanding the Code

AssistantAgent

The AssistantAgent is an AI-powered agent that uses a language model to generate responses:
  • name: Unique identifier for the agent
  • systemMessage: Instructions that define the agent’s behavior
  • llmConfig: Configuration including model settings and API keys
  • Temperature: Controls randomness (0 = deterministic, 1 = creative)

UserProxyAgent

The UserProxyAgent represents a human user in the conversation:
  • HumanInputMode.ALWAYS: Prompts for user input before each message
  • HumanInputMode.NEVER: Fully autonomous (uses default replies)
  • HumanInputMode.TERMINATE: Only prompts when termination is suggested

Message Printing

The RegisterPrintMessage() middleware nicely formats and prints messages to the console:

InitiateChatAsync

Starts the conversation between agents:

Autonomous Mode Example

For a fully autonomous conversation without human input:

Next Steps

Agents

Learn more about agent types and configurations

Function Calling

Add custom functions to your agents

Group Chat

Create multi-agent conversations

Code Execution

Execute code snippets dynamically

Troubleshooting

Make sure you’ve set the OPENAI_API_KEY environment variable correctly. In Windows, you may need to restart your terminal or IDE after setting it.
Ensure you’re using .NET 6.0 or later and that you’ve restored packages:
Check your internet connection and verify that your OpenAI API key is valid and has sufficient credits.