Understanding AI Agents and Multi‑Agent Systems: Frameworks, Design Principles, and Code Samples

This article provides a comprehensive overview of AI agents and multi‑agent systems, covering definitions, workflow vs. agent architectures, key differences, popular frameworks such as Swarm, AutoGen, and Magentic‑One, design principles, communication protocols, and practical code examples for building and orchestrating intelligent agents.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
Understanding AI Agents and Multi‑Agent Systems: Frameworks, Design Principles, and Code Samples

Introduction

Agents are applications that can perceive, understand their environment, and use tools to achieve goals. Two architectural categories are discussed: workflow systems, where a human plans and LLMs act as nodes, and agent systems, where LLMs make decisions about task execution.

Key Differences Between Workflow and Agent Systems

Execution Path : Fixed in workflows, dynamic in agent systems.

Decision Method : Rule‑based in workflows, LLM‑driven inference in agents.

Determinism : High for workflows, lower for agents.

Predictability : Strong for workflows, weaker for agents.

Adaptability : Low for workflows, high for agents.

Complexity : Relatively simple for workflows, relatively complex for agents.

Maintenance Cost : Lower for workflows, higher for agents.

Use Cases : Repetitive, well‑defined tasks for workflows; uncertain, creative tasks for agents.

Why Multi‑Agent Systems?

Single agents face context window limits and tool‑selection challenges as tasks grow in complexity. Multi‑agent collaboration enables dynamic task decomposition, specialization, and coordinated information exchange, often leading to emergent intelligence that surpasses the capabilities of any single agent.

Benefits of Modular Agent Design

Modularity simplifies development, testing, and maintenance.

Specialization allows expert agents to excel in specific domains.

Explicit control over inter‑agent communication improves reliability.

Multi‑Agent Frameworks

Two representative frameworks are highlighted:

Swarm (OpenAI)

Swarm is an experimental, lightweight framework designed for educational purposes. It introduces core concepts such as agents, handoffs, routines, and tool calls. Agents are defined with a name, instructions, and optional functions, and can transfer control to another agent via a handoff.

from swarm import Swarm, Agent
client = Swarm()

def transfer_to_agent_b():
    return agent_b

agent_a = Agent(name="Agent A", instructions="You are a helpful agent.", functions=[transfer_to_agent_b])
agent_b = Agent(name="Agent B", instructions="Only speak in Haikus.")

response = client.run(agent=agent_a, messages=[{"role": "user", "content": "I want to talk to agent B."}])
print(response.messages[-1]["content"])

Swarm supports streaming responses and provides several example agents (basic, triage_agent, weather_agent, airline, support_bot, personal_shopper).

AutoGen

AutoGen offers a unified interface for customizable, interactive multi‑agent workflows, focusing on code generation and collaborative coding tasks.

Magentic‑One (based on Microsoft AutoGen)

Magentic‑One extends AutoGen with a ledger‑based orchestrator that tracks task progress, detects loops, and dynamically replans. It includes specialized agents (Coder, WebSurfer, FileSurfer, UserProxy, Executor, LedgerOrchestrator) and supports Docker‑based secure code execution.

import asyncio
from autogen_magentic_one import LedgerOrchestrator, DockerCommandLineCodeExecutor

async def main():
    async with DockerCommandLineCodeExecutor(work_dir="./logs") as executor:
        orchestrator = LedgerOrchestrator(agents=[agent1, agent2], model_client=your_model_client)
        result = await orchestrator.run("Your task description")

asyncio.run(main())

Core Components of Magentic‑One

LedgerOrchestrator : Maintains a ledger of task facts, progress, and decides the next active agent.

Agents : Specialized agents with defined responsibilities (e.g., coding, web browsing).

Code Executor : Docker‑based sandbox for safe execution of arbitrary code.

Design Principles and Communication Protocols

Both Swarm and Magentic‑One rely on message types such as RequestReplyMessage (LLM response), BroadcastMessage (agent output broadcast), ResetMessage (clear history), and DeactivateMessage (ignore further input). Handoffs allow an agent to transfer control to another, while tool calls enable direct execution of Python functions.

Typical Workflow

Initialize task and assemble a team of agents.

Iteratively process messages: agents generate thoughts, take actions, observe results, and update the ledger.

Detect loops or stalls and trigger replanning if needed.

When the request is satisfied, synthesize a final answer.

Conclusion

OpenAI Swarm provides a simple, lightweight platform for experimenting with multi‑agent coordination, while Magentic‑One offers a more robust, production‑oriented framework with dynamic planning, ledger tracking, and secure code execution. Both illustrate how modern LLM‑driven agents can be orchestrated to solve complex, multi‑step problems.

AI agentsSwarmmulti-agent systemsAutoGencode executionLLM OrchestrationMagnetic-One
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.