Loading...
Loading...
Build a minimal AG2 `Agent` end to end — pick a model provider, set a prompt, call `agent.ask()`, then continue the conversation with `reply.ask()` (multi-turn). Use when the user is starting a new AG2 project, has no working `Agent` yet, or needs the multi-turn chaining pattern. Covers `OpenAIConfig`, `AnthropicConfig`, `GeminiConfig`, `OllamaConfig` etc., and env-var fallback for API keys.
npx skill4agent add ag2ai/ag2-skills ag2-quickstartRequired step: install dependencies before finishing. After writing AG2 code, install the matching provider extra (plus any feature extra the task needs, e.g.,ag-ui). Run the install:tracingbashpip install "ag2[openai]" # swap in the provider/extra you usedIf you cannot run commands, state the exactcommand. This is part of finishing the task, not an optional note.pip installFor a multi-file project (more than a throwaway script), also drop apinningrequirements.txtwith the extras you used (e.g.ag2) so the environment is reproducible.ag2[openai]>=0.14.0
*ConfigImportError: ... requires optional dependencies. Install with pip install "ag2[<provider>]"| Provider | Install | Env var | Config class |
|---|---|---|---|
| OpenAI | | | |
| Anthropic | | | |
| Gemini (API key) | | | |
| Vertex AI (Gemini) | | service-account / ADC | |
| Ollama (local) | | — | |
| DashScope (Qwen) | | | |
.envpython-dotenvfrom dotenv import load_dotenv
load_dotenv() # reads .env at project rootpython -c "import sys, ag2; from importlib.metadata import version; print(sys.executable); print('ag2', version('ag2'))"import asyncio
from ag2 import Agent
from ag2.config import OpenAIConfig
async def main() -> None:
agent = Agent(
"assistant",
prompt="You are a helpful assistant. Reply in one sentence.",
config=OpenAIConfig(model="gpt-4o-mini"),
)
# First turn
reply = await agent.ask("What is the capital of France?")
print(reply.body)
# Continue the same conversation — context is preserved
reply = await reply.ask("And of Germany?")
print(reply.body)
asyncio.run(main())Agent.ask(...)AgentReplyAgentReply.ask(...)reply.bodyag2-structured-outputreply.content()ag2.configmodel=api_key=streaming=Truefrom ag2.config import OpenAIConfig # gpt-4o, gpt-5-*, o-series, etc.
from ag2.config import OpenAIResponsesConfig # OpenAI Responses API (image gen, file_id support)
from ag2.config import AnthropicConfig # claude-sonnet-4-6, claude-opus-4-7, etc.
from ag2.config import GeminiConfig # Gemini Developer API (api_key)
from ag2.config import VertexAIConfig # Gemini on Google Vertex AI (project + location)
from ag2.config import OllamaConfig # local Ollama
from ag2.config import DashScopeConfig # Alibaba Qwen
config = AnthropicConfig(model="claude-sonnet-4-6", streaming=True)api_key=OPENAI_API_KEYANTHROPIC_API_KEYGEMINI_API_KEYGOOGLE_API_KEYOpenAIConfigbase_url=config = OpenAIConfig(
model="qwen-3",
base_url="http://localhost:8000/v1",
api_key="NotRequired", # pragma: allowlist secret
)reply.ask()agent = Agent("planner", prompt="...", config=config)
reply = await agent.ask("Plan a 5-day Japan trip in late April.")
reply = await reply.ask("Budget is $2500 per person, two travellers.")
reply = await reply.ask("Prefer trains. Day-by-day itinerary.")
print(reply.body)reply.ask()agent.ask(...)assets/multi_turn.py.copy(...)base = OpenAIConfig(model="gpt-5")
hot = base.copy(temperature=0.8)
cheap = base.copy(model="gpt-5-mini")agent = Agent("assistant", prompt="Help.")
reply = await agent.ask("Hello!", config=OpenAIConfig(model="gpt-5", api_key="sk-...")) # pragma: allowlist secretassets/hello_agent.pycode_examples/01assets/multi_turn.pycode_examples/03VertexAIConfigextra_bodyhttpxwebsite/docs/user-guide/model_configuration.mdxwebsite/docs/user-guide/agents.mdxwebsite/docs/user-guide/system_prompts.mdxawaitAgentAgentReplyasyncio.run(main())agent.ask()reply.ask()OPENAI_API_KEYstreaming=Trueconfig=