ag2-quickstart
Skill11 starsChanged 4 months agoApache-2.0View on GitHub
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.
- Reads credentials
- Installs packages
In this repository27 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ag2-a2aSkill
- ag2-acpSkill
- ag2-add-custom-toolSkill
- ag2-ag-uiSkill
- ag2-eval-comparisonSkill
- ag2-evaluationSkill
- ag2-hitlSkill
- ag2-knowledge-and-memorySkill
- ag2-liveSkill
- ag2-mcpSkill
- ag2-middlewareSkill
- ag2-multimodal-inputSkill
- ag2-network-discussionSkill
- ag2-network-governanceSkill
- ag2-network-quickstartSkill
- ag2-network-tools-and-viewsSkill
- ag2-network-workflowSkill
- ag2-observers-and-alertsSkill
- ag2-overviewSkill
- ag2-shell-toolSkill
- ag2-structured-outputSkill
- ag2-subagent-delegationSkill
- ag2-telemetrySkill
- ag2-testingSkill
- ag2-use-builtin-toolsSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 96
If api_key= is omitted, the config reads the standard env var — OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY (or GOOGLE_API_KEY), etc.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Quickstart: build your first AG2 Agent
When to use
- The user is starting from a blank file and wants a working AG2 agent.
- The user is unsure which provider config to use.
- The user wants to chain follow-up turns without losing conversation context.
- A larger task needs the basic Agent setup as its skeleton — start here, then layer the relevant feature skill on top.
Prerequisites
Required 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,tracing). Run the install:pip install "ag2[openai]" # swap in the provider/extra you usedIf you cannot run commands, state the exact
pip installcommand. This is part of finishing the task, not an optional note.For a multi-file project (more than a throwaway script), also drop a
requirements.txtpinningag2with the extras you used (e.g.ag2[openai]>=0.14.0) so the environment is reproducible.
Install the right provider extra and have a key for it. Each *Config requires its provider SDK — without the matching extra you'll see ImportError: ... requires optional dependencies. Install with pip install "ag2[<provider>]".
| Provider | Install | Env var | Config class |
| OpenAI | pip install "ag2[openai]" | OPENAI_API_KEY | OpenAIConfig, OpenAIResponsesConfig |
| Anthropic | pip install "ag2[anthropic]" | ANTHROPIC_API_KEY | AnthropicConfig |
| Gemini (API key) | pip install "ag2[gemini]" | GEMINI_API_KEY (or GOOGLE_API_KEY) | GeminiConfig |
| Vertex AI (Gemini) | pip install "ag2[gemini]" | service-account / ADC | VertexAIConfig |
| Ollama (local) | pip install "ag2[ollama]" | — | OllamaConfig |
| DashScope (Qwen) | pip install "ag2[dashscope]" | DASHSCOPE_API_KEY | DashScopeConfig |
Load env vars from a project-root .env with python-dotenv so scripts pick up keys without exporting them in your shell:
from dotenv import load_dotenv
load_dotenv() # reads .env at project rootQuick sanity-check before debugging weird import errors — make sure you're running against the ag2 you think:
python -c "import sys, ag2; from importlib.metadata import version; print(sys.executable); print('ag2', version('ag2'))"60-second recipe
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(...) starts a new turn and returns an AgentReply. AgentReply.ask(...) continues the same conversation, preserving its context and history. The reply text is in reply.body; for typed output see the ag2-structured-output skill (reply.content()).
Picking a provider
Each provider has its own config class in ag2.config. All accept model=, optional api_key=, and (where supported) streaming=True. Streaming is recommended — AG2 is async- and streaming-first.
from 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)If api_key= is omitted, the config reads the standard env var — OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY (or GOOGLE_API_KEY), etc.
For OpenAI-compatible endpoints (vLLM, LM Studio, Together, NVIDIA NIM, etc.) use OpenAIConfig with base_url= set:
config = OpenAIConfig(
model="qwen-3",
base_url="http://localhost:8000/v1",
api_key="NotRequired", # pragma: allowlist secret
)Multi-turn — chain 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() keeps the prior turns in scope so the LLM remembers the constraints. Calling agent.ask(...) again instead would start a fresh conversation. See assets/multi_turn.py for the full travel-planner example.
Reusing model configs
Configs are immutable. Use .copy(...) to fork one with overrides:
base = OpenAIConfig(model="gpt-5")
hot = base.copy(temperature=0.8)
cheap = base.copy(model="gpt-5-mini")You can also override the model per ask — useful when the user brings their own API key per request:
agent = Agent("assistant", prompt="Help.")
reply = await agent.ask("Hello!", config=OpenAIConfig(model="gpt-5", api_key="sk-...")) # pragma: allowlist secretThe per-ask config completely replaces the agent's config for that turn.
Going deeper
- Working starter (single-turn):
assets/hello_agent.py(mirrorscode_examples/01). - Multi-turn starter:
assets/multi_turn.py(mirrorscode_examples/03). - Full provider reference, including
VertexAIConfigauth,extra_body, customhttpxclient, env-var fallback table:website/docs/user-guide/model_configuration.mdx. - Agent communication API surface (events, observing, HITL):
website/docs/user-guide/agents.mdx. - Static, dynamic, per-turn prompts:
website/docs/user-guide/system_prompts.mdx.
Common pitfalls
- Forgetting to
await— every method onAgent/AgentReplyis async. Wrap inasyncio.run(main())for scripts. - Calling
agent.ask()twice expecting context to carry — it doesn't; usereply.ask()instead. - Hardcoding API keys — prefer env-var fallback (
OPENAI_API_KEY, etc.) so configs commit cleanly. - Skipping
streaming=True— AG2 is streaming-first; you'll get a worse user experience without it on supported providers. - Per-ask
config=is total override, not a partial merge — be deliberate about which knobs you set.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

