Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Microsoft Autogen AssistantAgent Init Swarm

From Leeroopedia
Knowledge Sources
Domains Multi-Agent Systems, Swarm Intelligence, Agent Design, Task Routing
Last Updated 2026-02-11 00:00 GMT

Overview

Concrete tool for creating specialized agents with handoff capabilities in a swarm pattern, provided by Microsoft AutoGen.

Description

The AssistantAgent constructor configures an LLM-backed agent that can participate in swarm-style workflows. When the handoffs parameter is provided, the agent is equipped with handoff tools that the LLM can invoke to transfer control to another agent. Each handoff target becomes a callable tool (named transfer_to_{target}) that the model can select during generation. The system_message parameter defines the agent's specialized role and behavioral constraints, while description provides external metadata for team-level reasoning.

During initialization, handoff strings are automatically converted to HandoffBase objects. The constructor validates that all handoff tool names are unique and do not collide with regular tool names. Handoff tools are stored separately from regular tools but are presented to the model alongside them during inference.

Usage

Import and instantiate AssistantAgent whenever you need an agent that can participate in a swarm by handing off tasks to other named agents. Typical usage involves creating multiple specialized agents, each with a different system message and a complementary set of handoff targets, then assembling them into a Swarm team.

Code Reference

Source Location

  • Repository: Microsoft AutoGen
  • File: python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py (Lines 724-859)

Signature

def __init__(
    self,
    name: str,
    model_client: ChatCompletionClient,
    *,
    tools: List[BaseTool[Any, Any] | Callable[..., Any] | Callable[..., Awaitable[Any]]] | None = None,
    workbench: Workbench | Sequence[Workbench] | None = None,
    handoffs: List[HandoffBase | str] | None = None,
    model_context: ChatCompletionContext | None = None,
    description: str = "An agent that provides assistance with ability to use tools.",
    system_message: str | None = "You are a helpful AI assistant. Solve tasks using your tools. Reply with TERMINATE when the task has been completed.",
    model_client_stream: bool = False,
    reflect_on_tool_use: bool | None = None,
    max_tool_iterations: int = 1,
    tool_call_summary_format: str = "{result}",
    tool_call_summary_formatter: Callable[[FunctionCall, FunctionExecutionResult], str] | None = None,
    output_content_type: type[BaseModel] | None = None,
    output_content_type_format: str | None = None,
    memory: Sequence[Memory] | None = None,
    metadata: Dict[str, str] | None = None,
) -> None

Import

from autogen_agentchat.agents import AssistantAgent

I/O Contract

Inputs

Name Type Required Description
name str Yes Unique identifier for the agent within the team.
model_client ChatCompletionClient Yes The LLM client used for generating responses and tool calls.
handoffs str] | None No List of handoff targets. Strings are auto-converted to HandoffBase with default settings. Each entry creates a transfer tool the LLM can invoke.
system_message None No The system prompt that specializes this agent's behavior and role. Defaults to a generic assistant prompt.
description str No Human-readable description of the agent's capabilities. Used by team-level components for identification.
tools Callable] | None No Regular tools available to the agent. Cannot overlap with handoff tool names.
model_context None No Context manager for conversation history. Defaults to UnboundedChatCompletionContext.
reflect_on_tool_use None No Whether to generate a follow-up response after tool execution. Defaults to False unless output_content_type is set.
max_tool_iterations int No Maximum number of tool call iterations per turn. Must be >= 1. Defaults to 1.
memory None No Memory modules for the agent to use during conversation.
metadata None No Arbitrary key-value metadata attached to the agent.

Outputs

Name Type Description
AssistantAgent instance AssistantAgent A configured agent ready to participate in a Swarm team. Produces TextMessage, HandoffMessage, ToolCallSummaryMessage, or StructuredMessage depending on configuration.

Usage Examples

Basic Example

from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient

model_client = OpenAIChatCompletionClient(model="gpt-4o")

# Create a triage agent that can hand off to specialists
triage_agent = AssistantAgent(
    name="triage",
    model_client=model_client,
    handoffs=["billing", "technical_support"],
    system_message=(
        "You are a triage agent. Determine whether the user's issue "
        "is related to billing or technical support, then transfer "
        "to the appropriate specialist."
    ),
    description="Routes incoming requests to the correct specialist.",
)

# Create a billing specialist (no further handoffs)
billing_agent = AssistantAgent(
    name="billing",
    model_client=model_client,
    handoffs=["triage"],
    system_message=(
        "You are a billing specialist. Help users with invoices, "
        "payments, and subscription issues. If the issue is not "
        "billing-related, transfer back to triage."
    ),
    description="Handles billing and payment inquiries.",
)

# Create a technical support specialist
tech_agent = AssistantAgent(
    name="technical_support",
    model_client=model_client,
    handoffs=["triage"],
    system_message=(
        "You are a technical support agent. Help users with "
        "software bugs, configuration, and troubleshooting. "
        "If the issue is not technical, transfer back to triage."
    ),
    description="Handles technical support inquiries.",
)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment