Skip to content

Usage

Minimal example

This is the canonical quickstart from the official AutoGen docs. Save it as run_task.py and run it with your OPENAI_API_KEY set.

python
import asyncio
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.teams.magentic_one import MagenticOne
from autogen_agentchat.ui import Console

async def main():
    client = OpenAIChatCompletionClient(model="gpt-4o")
    m1 = MagenticOne(client=client)
    result = await Console(m1.run_stream(task="What is the current top story on Hacker News?"))
    print(result)

asyncio.run(main())

Run it:

bash
OPENAI_API_KEY=sk-... python run_task.py

The Console helper streams each agent's turn to stdout as it happens. The final result object contains the task output and a full message history.

How the orchestration works

  1. You pass a plain-English task string to m1.run_stream(task="...").
  2. The Orchestrator decomposes the task into a step-by-step plan.
  3. It delegates steps to the appropriate agents: WebSurfer browses, FileSurfer reads files, Coder writes Python, ComputerTerminal executes it.
  4. The Orchestrator tracks progress and re-plans if an agent gets stuck.
  5. The final answer is returned when the Orchestrator determines the task is complete.

Swapping in MagenticOneGroupChat

If you already have an AutoGen workflow using SelectorGroupChat, you can drop in MagenticOneGroupChat as a direct replacement:

python
from autogen_ext.teams.magentic_one import MagenticOneGroupChat

team = MagenticOneGroupChat(
    participants=[web_surfer, file_surfer, coder, terminal],
    model_client=client
)

Safety wrapper for code execution

The MagenticOne helper class accepts approval functions so a human or automated check can gate code before the ComputerTerminal runs it:

python
m1 = MagenticOne(
    client=client,
    code_execution_approval_func=lambda code: input(f"Allow?\n{code}\n(y/n): ") == "y"
)