
How-Tos
How to run a first AG2 agent
Install AG2, set a provider key, and run a first ConversableAgent that replies for up to three turns.
Searcher → Analyst → Writer → Editor · subagentic-20261011-2000
The AG2 quick start is a first agent workflow, not a multi-agent setup. The page says you can be up and running in 3 minutes: set up the environment, then build one poetic assistant that answers in rhyme. You install a provider extra, export that provider's key, paste a short script, and run it. The conversation lasts for up to 3 turns, or until you type exit.
Match the Python range
AG2 requires Python version >= 3.10, < 3.14. The quick start recommends a virtual environment so packages stay contained, and it points to Python's venv documentation. Those venv commands are not printed on the quick start page, so take them from the Python docs rather than guessing flags.
The page also says to have AG2 installed before you continue, then shows a provider install it describes as 30 seconds of work.
Install the extra, import from autogen
The install name on this page is ag2. The import name in the sample is autogen. Use the extra for the provider you will actually call.
OpenAI:
pip install "ag2[openai]"
Gemini:
pip install "ag2[gemini]"
Azure OpenAI uses the same extra as OpenAI:
pip install "ag2[openai]"
The script you paste does not import a package named ag2. It imports ConversableAgent and LLMConfig from autogen. That is the import the current quick start uses after the extra is installed.
Set the key the config will read
Set the LLM API key, and any provider-specific endpoint, as environment variables before you run the file. The page shows macOS and Linux export commands and Windows setx commands. Replace the key placeholder in those commands with your own key. How to create that key is not on the quick start page.
OpenAI on macOS or Linux:
export OPENAI_API_KEY="YOUR_API_KEY"
OpenAI on Windows:
setx OPENAI_API_KEY "YOUR_API_KEY"
Gemini on macOS or Linux:
export GEMINI_API_KEY="YOUR_API_KEY"
Gemini on Windows:
setx GEMINI_API_KEY "YOUR_API_KEY"
Azure OpenAI needs a key and an endpoint. On macOS or Linux:
export AZURE_OPENAI_API_KEY="YOUR_API_KEY"
export AZURE_OPENAI_ENDPOINT="https://YOUR-RESOURCE.openai.azure.com/"
On Windows:
setx AZURE_OPENAI_API_KEY "YOUR_API_KEY"
setx AZURE_OPENAI_ENDPOINT "https://YOUR-RESOURCE.openai.azure.com/"
The endpoint value in those commands is a placeholder URL. Substitute your own Azure resource endpoint, and keep the rest of each command as printed. The export form puts an equals sign between the name and the value. The setx form separates the name and the value with a space.
Paste first_agent.py
Create a Python script called first_agent.py and paste the code for your provider. The page says the agent behaviour is identical across providers and only the LLMConfig differs. Pasting the whole provider tab is safer than mixing comments from one tab with a config from another.
This is the OpenAI sample, unchanged:
import os
from autogen import ConversableAgent, LLMConfig
# 1. Define our LLM configuration for OpenAI's gpt-5-nano
# uses the OPENAI_API_KEY environment variable
llm_config = LLMConfig(
{"api_type": "openai", "model": "gpt-5-nano", "api_key": os.environ["OPENAI_API_KEY"]}
)
# 2. Create our LLM agent
my_agent = ConversableAgent(
name="helpful_agent",
system_message="You are a poetic AI assistant, respond in rhyme.",
llm_config=llm_config,
)
# 3. Run the agent with a prompt and process the response
response = my_agent.run(
message="In one sentence, what's the big deal about AI?",
max_turns=3,
user_input=True,
)
response.process()
The comments split the file into three steps. LLMConfig selects the OpenAI model named in that block and reads the OpenAI key from the environment. ConversableAgent is named helpful_agent, receives that config, and is given a system message that tells it to respond in rhyme. Then the run call takes the opening message written in the script, limits the conversation to three turns, and asks for user input. The last line calls process() on what run() returned.
For Gemini, keep the import, the agent, and the run-then-process lines. Replace the configuration section with the Gemini tab's comment and block:
# 1. Define our LLM configuration for Google Gemini
# uses the GEMINI_API_KEY environment variable
llm_config = LLMConfig(
{"api_type": "google", "model": "gemini-2.5-flash", "api_key": os.environ["GEMINI_API_KEY"]}
)
That config uses the Gemini key. The model name in the quick start sample is gemini-2.5-flash.
For Azure OpenAI, the comment in the quick start says the model value must match your Azure deployment name, not the underlying model name. Use this configuration section:
# 1. Define our LLM configuration for Azure OpenAI
# "model" must match your Azure *deployment name*, not the underlying model name
llm_config = LLMConfig(
{
"api_type": "azure",
"model": "my-gpt-deployment",
"api_key": os.environ["AZURE_OPENAI_API_KEY"],
"base_url": os.environ["AZURE_OPENAI_ENDPOINT"],
"api_version": "2025-01-01",
}
)
Replace the sample deployment name in that block with your own. The sample reads both Azure environment variables, passes the endpoint as the base URL field, and sets the API version shown above. Leave the agent and the two calls at the bottom as they appear in the OpenAI file.
Keep run(), then process()
The quick start states the split directly: run() prepares the conversation, process() executes it. The two-step pattern gives you a chance to inspect or modify the workflow before it runs.
This first file does not change anything between the two calls. It still needs both. The turn cap in the sample is three. Asking for user input is what lets the terminal prompt you once process() executes the prepared conversation.
Run it and take a turn
In your terminal, run:
python first_agent.py
If everything is set up correctly, the agent replies to the initial message in rhyme, then prompts you. The page lists three choices:
- Type a reply, and the agent will respond in rhyme to your message.
- Press Enter to send an empty message to the agent, and see how it creatively responds.
- Type exit to end the conversation.
The interaction continues for up to 3 turns, or until you exit.
The quick start also prints an example of that exchange. The rhyme is example output, not a required result:
user (to helpful_agent):
In one sentence, what's the big deal about AI?
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
helpful_agent (to user):
AI transforms our world, enhancing life's parade,
With insights and solutions, it helps plans cascade.
--------------------------------------------------------------------------------
Replying as user. Provide feedback to helpful_agent. Press enter to skip and use auto-reply, or type 'exit' to end the conversation:
The transcript prompt says to press enter to skip and use auto-reply, or type exit to end. The bullet list on the same page describes Enter as sending an empty message. Follow the prompt your terminal prints. The stop conditions the page documents are three turns or exit.
Watch it without installing
If you want to see AG2 before a local install, the quick start points to the AG2 Playground for multi-agent workflows in the browser, with no setup required. That is a separate path from the local script.
What to do next
Run the script with the command above, send one reply, then type exit so you see both a turn and a stop. After that, stay on the quick start's What's Next list: Overview for the core building blocks, LLM Configuration for other model providers (the page names Gemini and Anthropic there), and Introducing Group Chat when you want multiple agents to collaborate. Those guides are linked from the quick start; this walkthrough does not add steps they do not show.