Skip to main content
Message passing is the foundation of communication in AutoGen Core. Agents communicate exclusively through asynchronous messages, following the Actor model with no shared state.

Message Types

Any Python object can be a message. Common patterns:

Dataclass Messages

Use dataclasses for messages. They’re simple, type-safe, and work well with serialization.

Pydantic Models

MessageContext

Every message handler receives a MessageContext with metadata about the message:

Using MessageContext

MessageHandlerContext

MessageHandlerContext provides access to the current agent’s ID from within a message handler:
MessageHandlerContext.agent_id() must be called from within a message handler. It raises RuntimeError if called outside a handler context.

Topics and Publishing

TopicId

TopicId identifies a publish-subscribe topic:
TopicId components:
string
required
Event type following CloudEvents spec. Must match pattern: ^[\w\-\.\:\=]+$Examples: task.completed, user.login, system.error
string
required
Context where the event happened. Can be any string.Examples: worker-1, api-gateway, user-123

DefaultTopicId

DefaultTopicId is a predefined topic for simple pub-sub:

Publishing Messages

From an agent:
From the runtime:

Subscriptions

Subscriptions define which agents receive messages published to topics.

Subscription Protocol

TypeSubscription

TypeSubscription matches topics by type and creates agent instances per source:
TypeSubscription creates separate agent instances for each source. Use when you want per-source state isolation.
Example:

DefaultSubscription

DefaultSubscription subscribes an agent to the default topic:

TypePrefixSubscription

TypePrefixSubscription matches topics by type prefix (internal use):
TypePrefixSubscription is mainly used internally for direct message routing. For application-level subscriptions, use TypeSubscription or DefaultSubscription.

Subscription Decorators

Message Routing Patterns

Pattern 1: Direct RPC

Pattern 2: Broadcast Events

Pattern 3: Per-Source Routing

Cancellation

Checking Cancellation in Handler

Message Serialization

Messages must be serializable for distributed runtime:

Custom Serializer

Known Serializers

Complete Example

Next Steps

Event-Driven Architecture

Learn about event handlers and message routing

Distributed Runtime

Scale message passing across processes

Agent Runtime

Understand runtime operations

Core Overview

Return to Core API overview