Skip to main content
This is the simplest possible AutoGen example. It creates a single assistant agent that responds to a task.

What You’ll Learn

  • How to create an OpenAI model client
  • How to create an AssistantAgent
  • How to run a simple task

Prerequisites

1

Install AutoGen

pip install -U "autogen-agentchat" "autogen-ext[openai]"
2

Set your OpenAI API key

export OPENAI_API_KEY="sk-..."

Code Example

Create a file called hello_world.py with the following code:
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient

async def main() -> None:
    # Create the model client
    model_client = OpenAIChatCompletionClient(model="gpt-4o")
    
    # Create the assistant agent
    agent = AssistantAgent("assistant", model_client=model_client)
    
    # Run a simple task
    result = await agent.run(task="Say 'Hello World!'")
    print(result)
    
    # Clean up
    await model_client.close()

asyncio.run(main())

Run the Example

python hello_world.py

Expected Output

You should see output similar to:
TaskResult(messages=[TextMessage(source='assistant', content='Hello World!', ...)])

How It Works

  1. Model Client: Creates an OpenAIChatCompletionClient configured to use GPT-4o
  2. Assistant Agent: Creates an AssistantAgent that uses the model client to generate responses
  3. Run Task: Executes the task and returns a TaskResult containing the agent’s response
  4. Cleanup: Closes the model client to release resources

Key Concepts

AssistantAgent

A general-purpose agent that uses an LLM to respond to tasks and messages.

Model Client

Handles communication with the LLM provider (OpenAI, Azure, etc.).

Task

A string prompt that tells the agent what to do.

TaskResult

Contains the messages generated during task execution.

Next Steps

Two Agent Chat

Learn how to create conversations between multiple agents

Tool Calling

Add tools to give your agents new capabilities

Build docs developers (and LLMs) love