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]" |
| 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.
Tip
AG2 Beta is designed to be async and streaming-first, so for the best user experience it is recommended to enable streaming on the model provider configurations for models that support it. As shown above, Streaming has been set to True in each config.
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).
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.
Tip
If you are running a self-hosted server via HTTPS without a valid SSL certificate (e.g., a local self-signed certificate), you can disable SSL checks by passing a custom httpx.AsyncClient with verify=False to the configuration:
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.
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.
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.