Model Configuration - AG2

Model Configuration

The AG2 framework provides an explicit, predictable, and type-safe way to configure Large Language Models (LLMs) for your agents. The configuration API is designed to provide a consistent developer experience across different model providers while maintaining strong typing support.

Supported Providers

AG2 supports multiple LLM providers through dedicated configuration classes. Each provider requires its respective optional dependencies to be installed.

Provider Configuration Class Installation Command
OpenAI Responses OpenAIResponsesConfig pip install "ag2[openai]"
OpenAI OpenAIConfig pip install "ag2[openai]"
Anthropic AnthropicConfig pip install "ag2[anthropic]"
Gemini GeminiConfig pip install "ag2[gemini]"
Gemini on Vertex AI VertexAIConfig pip install "ag2[gemini]"
Ollama OllamaConfig pip install "ag2[ollama]"
DashScope DashScopeConfig pip install "ag2[dashscope]"

(Note: OpenAIConfig is also available for OpenAI-compatible endpoints).

How to Configure a Model

Basic Configuration

To configure a model, import the specific provider's configuration class and initialize it with your desired parameters. The most common parameters are model, api_key, and base_url.

<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br> <br>from autogen.beta.config import OpenAIResponsesConfig<br># Configure an OpenAI Responses API model<br>config = OpenAIResponsesConfig(<br> model="gpt-4.1-nano",<br> api_key="sk-...",<br> streaming=True<br>)<br>
<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br> <br>from autogen.beta.config import OpenAIConfig<br># Configure an OpenAI model<br>config = OpenAIConfig(<br> model="gpt-4o-mini",<br> api_key="sk-...",<br> temperature=0.2,<br> streaming=True<br>)<br>
<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br> <br>from autogen.beta.config import AnthropicConfig<br># Configure an Anthropic model<br>config = AnthropicConfig(<br> model="claude-haiku-4-5-20251001",<br> api_key="sk-ant-...",<br> streaming=True<br>)<br>
<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br> <br>from autogen.beta.config import GeminiConfig<br># Configure a Gemini model<br>config = GeminiConfig(<br> model="gemini-3-flash-preview",<br> api_key="...",<br> streaming=True<br>)<br>
<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br> <br>from autogen.beta.config import OllamaConfig<br># Configure an Ollama model<br>config = OllamaConfig(<br> model="qwen3.5:latest",<br> streaming=True<br>)<br>
<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br> <br>from autogen.beta.config import DashScopeConfig<br># Configure a DashScope model<br>config = DashScopeConfig(<br> model="qwen-plus",<br> api_key="...",<br> streaming=True<br>)<br>

Using Environment Variables

For security and convenience, you don't need to hardcode your API keys. If api_key is not explicitly provided, the configuration will automatically attempt to load it from your environment variables.

The system looks for provider-specific keys (e.g., OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY).

<br>1<br>2<br>3<br>4<br> <br>from autogen.beta.config import OpenAIConfig<br># Automatically falls back to OPENAI_API_KEY from the environment<br>config = OpenAIConfig(model="gpt-5")<br>

Google Vertex AI (Gemini)

For Gemini on Vertex AI (Google Cloud), use the dedicated VertexAIConfig class. GeminiConfig covers the public Developer API (api_key); VertexAIConfig covers the Vertex path (GCP project, location, and Google-issued credentials).

Authentication accepts any of the following:

Service account key fileApplication Default CredentialsPre-built Credentials object

<br> 1<br> 2<br> 3<br> 4<br> 5<br> 6<br> 7<br> 8<br> 9<br>10<br> <br>from autogen.beta.config import VertexAIConfig<br>config = VertexAIConfig(<br> model="gemini-3-flash-preview",<br> project="my-gcp-project",<br> location="us-central1",<br> credentials="/path/to/service-account-key.json",<br> # Path to a service-account JSON key file downloaded from<br> # GCP Console -> IAM & Admin -> Service Accounts -> Keys.<br>)<br>

The service account needs the Vertex AI User (roles/aiplatform.user) IAM role on the project.

<br> 1<br> 2<br> 3<br> 4<br> 5<br> 6<br> 7<br> 8<br> 9<br>10<br> <br>from autogen.beta.config import VertexAIConfig<br># Run `gcloud auth application-default login` first, or ensure<br># GOOGLE_APPLICATION_CREDENTIALS points to a key file. With nothing<br># passed to `credentials`, google-genai resolves ADC automatically.<br>config = VertexAIConfig(<br> model="gemini-3-flash-preview",<br> project="my-gcp-project",<br> location="us-central1",<br>
<br> 1<br> 2<br> 3<br> 4<br> 5<br> 6<br> 7<br> 8<br> 9<br>10<br>11<br>12<br>13<br> <br>import google.auth<br>from autogen.beta.config import VertexAIConfig<br>creds, _ = google.auth.default(<br> scopes=["https://www.googleapis.com/auth/cloud-platform"],<br>})<br>config = VertexAIConfig(<br> model="gemini-3-flash-preview",<br> project="my-gcp-project",<br> location="us-central1",<br> credentials=creds,<br>})<br>

Environment variables

Instead of passing parameters explicitly, the underlying google-genai SDK resolves any field left unset from the following environment variables:

Environment variable Used by Equivalent parameter Notes
GOOGLE_API_KEY GeminiConfig api_key Takes precedence over GEMINI_API_KEY if both are set.
GEMINI_API_KEY GeminiConfig api_key Developer API key.
GOOGLE_CLOUD_PROJECT VertexAIConfig project GCP project ID.
GOOGLE_CLOUD_LOCATION VertexAIConfig location GCP region (or global).
GOOGLE_APPLICATION_CREDENTIALS VertexAIConfig credentials Path to a service-account JSON key file, read via ADC.

With the three Vertex variables set in the environment, configuration collapses to just the model name:

export GOOGLE_CLOUD_PROJECT=my-gcp-project
export GOOGLE_CLOUD_LOCATION=us-central1
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json
<br>1<br>2<br>3<br>4<br> <br>from autogen.beta.config import VertexAIConfig<br># All Vertex auth parameters resolved from the environment.<br>config = VertexAIConfig(model="gemini-3-flash-preview")<br>

Self-Hosted and OpenAI-Compatible Models (vLLM, LM Studio, etc.)

If you are using a self-hosted model or an API that is compatible with the OpenAI format (such as vLLM, LM Studio, FastChat, or Together AI), you can use the OpenAIConfig class and specify a custom base_url.

<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br> <br>from autogen.beta.config import OpenAIConfig<br># Configure a vLLM or other OpenAI-compatible endpoint<br>config = OpenAIConfig(<br> model="qwen-3",<br> base_url="http://localhost:8000/v1",<br> # Some endpoints don't require an API key, but the client expects a non-empty string<br> api_key="NotRequired",<br>})<br>

Extra Body Parameters

Some OpenAI API-compatible providers require additional, provider-specific parameters in the request body. Use the extra_body parameter on OpenAIConfig to pass these through directly to the API call.

This is useful for enabling features like extended thinking on self-hosted or third-party models:

<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br> <br>from autogen.beta.config import OpenAIConfig<br># NVIDIA NIM<br>nemotron = OpenAIConfig(<br> model="nvidia/nemotron-3-super-120b-a12b",<br> base_url="https://integrate.api.nvidia.com/v1",<br> extra_body={"chat_template_kwargs": {"thinking": True}},<br>})<br>

Reusing and Overriding Configurations

Model configurations are immutable. If you need to reuse a configuration for multiple agents with slight variations (e.g., changing the model version or adjusting the temperature), use the .copy() method. This creates a new updated instance without mutating the original configuration.

<br> 1<br> 2<br> 3<br> 4<br> 5<br> 6<br> 7<br> 8<br> 9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br> <br>from autogen.agent import Agent<br>from autogen.beta.config import OpenAIConfig<br>base_config = OpenAIConfig(model="gpt-5")<br>agent1 = Agent(<br> "Assistant",<br> # Create a new configuration with updated temperature<br> config=base_config.copy(temperature=0.2),<br>)<br>agent2 = Agent(<br> "AnotherAssistant",<br> # Create a new configuration with updated model and temperature<br> config=base_config.copy(model="gpt-5-mini", temperature=0.8),<br>)<br>

Delaying Model Configuration

In many use cases, you may want to separate the logic of defining your agent (tools, system messages, instructions) from configuring the specific model it uses. This allows you to construct an agent once and dynamically provide the model configuration later during execution.

You can accomplish this by passing the configuration to the .ask() method when interacting with the agent. This is especially useful for applications like web servers where the user might bring their own API key or choose a different model on the fly.

<br> 1<br> 2<br> 3<br> 4<br> 5<br> 6<br> 7<br> 8<br> 9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br> <br>from autogen.agent import Agent<br>from autogen.beta.config import OpenAIConfig<br># Define an agent without an initial model config,<br># or with a default one you plan to override later<br>agent = Agent(<br> "Assistant",<br> prompt="You are a helpful assistant.",<br> # other tools and settings...<br>)<br># Ask the agent, passing the explicit model configuration<br>response = await agent.ask(<br> "Hello!",<br> config=OpenAIConfig(<br> model="gpt-5",<br> api_key="sk-user-specific-key"<br> )<br>)<br>

Warning

Providing a configuration or client directly to the ask() method completely overrides the original model configuration assigned to the agent for that specific turn.