Skip to main content
Model clients provide the interface between AutoGen agents and large language models. AutoGen supports multiple LLM providers through the autogen-ext package.

Installation

Install the extension for your chosen provider:

OpenAI

The OpenAIChatCompletionClient supports GPT-4, GPT-3.5, o1, and o3 models.

Basic Usage

Configuration Options

model
string
required
The model name (e.g., gpt-4o, gpt-4-turbo, gpt-3.5-turbo)
api_key
string
OpenAI API key. If not provided, reads from OPENAI_API_KEY environment variable
temperature
float
default:"1.0"
Sampling temperature between 0 and 2
top_p
float
default:"1.0"
Nucleus sampling parameter
max_tokens
int
Maximum tokens to generate
timeout
float
default:"60.0"
Request timeout in seconds
base_url
string
Override the default OpenAI API endpoint

Advanced Example

Azure OpenAI

The AzureOpenAIChatCompletionClient connects to Azure OpenAI Service.

Basic Usage

Configuration Options

azure_endpoint
string
required
The Azure OpenAI endpoint URL
api_version
string
required
Azure OpenAI API version (e.g., 2024-02-01)
azure_deployment
string
required
Your deployment name in Azure
api_key
string
Azure OpenAI API key
azure_ad_token
string
Azure Active Directory token for authentication

Azure AD Authentication

Anthropic

The AnthropicChatCompletionClient supports Claude models.

Basic Usage

Configuration Options

model
string
required
Claude model name:
  • claude-3-5-sonnet-20241022 - Most capable
  • claude-3-opus-20240229 - Previous flagship
  • claude-3-sonnet-20240229 - Balanced
  • claude-3-haiku-20240307 - Fast and compact
api_key
string
Anthropic API key. Falls back to ANTHROPIC_API_KEY environment variable
max_tokens
int
required
Maximum tokens to generate. Required for Anthropic models
temperature
float
default:"1.0"
Sampling temperature between 0 and 1
top_p
float
Nucleus sampling parameter
top_k
int
Only sample from top K options

Extended Thinking (Claude 3.5 Sonnet)

Claude 3.5 Sonnet supports extended thinking mode:

AWS Bedrock

Use Claude models through AWS Bedrock:
aws_region
string
AWS region (e.g., us-west-2, us-east-1)
aws_access_key
string
AWS access key ID
aws_secret_key
string
AWS secret access key
aws_session_token
string
AWS session token for temporary credentials

Ollama

The OllamaChatCompletionClient connects to local Ollama instances.

Basic Usage

Configuration Options

model
string
required
Ollama model name (e.g., llama3.2, mistral, qwen2.5)
host
string
default:"http://localhost:11434"
Ollama server URL
temperature
float
Sampling temperature
top_p
float
Nucleus sampling parameter
top_k
int
Top-K sampling parameter
num_ctx
int
Context window size
num_predict
int
Maximum tokens to generate

Advanced Configuration

Llama.cpp

Run GGUF models locally with llama.cpp:

Installation

Basic Usage

Configuration Options

model_path
string
required
Path to the GGUF model file
n_ctx
int
default:"2048"
Context window size
n_gpu_layers
int
default:"0"
Number of layers to offload to GPU
temperature
float
default:"0.8"
Sampling temperature
top_p
float
default:"0.95"
Nucleus sampling
top_k
int
default:"40"
Top-K sampling
max_tokens
int
default:"512"
Maximum tokens to generate

Azure AI

Connect to Azure AI model deployments:

Streaming Responses

All model clients support streaming:

Model Capabilities

Query model capabilities:

Token Counting

Count tokens before sending requests:

Usage Tracking

Track token usage from responses:

Error Handling

Handle common errors:

Environment Variables

Model clients respect standard environment variables:

Best Practices

Use Environment Variables

Store API keys in environment variables instead of hardcoding:

Set Timeouts

Always configure appropriate timeouts:

Monitor Usage

Track token usage to manage costs:

Next Steps

Code Executors

Set up code execution environments

Tools

Add tools and capabilities to agents