Documentation Index Fetch the complete documentation index at: https://mintlify.com/microsoft/autogen/llms.txt
Use this file to discover all available pages before exploring further.
The Microsoft.AutoGen.Core namespace provides the foundational components for building distributed, event-driven multi-agent systems in .NET.
Agent Classes
Abstract base class for all agents in the AutoGen system. using Microsoft . AutoGen . Core ;
using Microsoft . AutoGen . Contracts ;
public class MyAgent : BaseAgent
{
public MyAgent (
AgentId id ,
IAgentRuntime runtime ,
string description ,
ILogger < BaseAgent >? logger = null )
: base ( id , runtime , description , logger )
{
}
}
Unique identifier for the agent instance
Runtime environment for message passing
Description of the agent’s capabilities
Properties Gets the unique identifier of the agent
Gets metadata associated with the agent
Gets the runtime environment
Methods Handles an incoming message public async ValueTask < object ?> OnMessageAsync (
object message ,
MessageContext messageContext )
{
// Handle message
return response ;
}
Context information about the message
Send a message to another agent var response = await SendMessageAsync (
message : "Hello" ,
recipient : new AgentId ( "worker" , "default" ),
cancellationToken : cancellationToken
);
Publish a message to a topic await PublishMessageAsync (
message : new { Event = "Started" },
topic : new TopicId ( "events" , "system" )
);
Called when the agent is being closed
Single-process agent runtime for local execution. using Microsoft . AutoGen . Core ;
var runtime = new InProcessRuntime ();
// Register agent factory
await runtime . RegisterAgentFactoryAsync (
type : new AgentType ( "worker" ),
factoryFunc : async ( id , rt ) => new WorkerAgent ( id , rt )
);
// Send message
var response = await runtime . SendMessageAsync (
message : "Task" ,
recipient : new AgentId ( "worker" , "instance1" )
);
RegisterAgentFactoryAsync
Register an agent factory function
Send a message to an agent
Publish a message to a topic
Contracts & Interfaces
Core interface for all agents. public interface IAgent : ISaveState
{
AgentId Id { get ; }
AgentMetadata Metadata { get ; }
ValueTask < object ?> OnMessageAsync ( object message , MessageContext messageContext );
}
Gets the unique identifier of the agent
Gets metadata about the agent
Handles incoming messages
Interface for agents that can be hosted in a runtime. public interface IHostableAgent : IAgent
{
ValueTask CloseAsync ();
}
Called when the runtime is closing
Defines the runtime environment for agents. public interface IAgentRuntime : ISaveState
{
ValueTask < object ?> SendMessageAsync (
object message ,
AgentId recipient ,
AgentId ? sender = null ,
string ? messageId = null ,
CancellationToken cancellationToken = default );
ValueTask PublishMessageAsync (
object message ,
TopicId topic ,
AgentId ? sender = null ,
string ? messageId = null ,
CancellationToken cancellationToken = default );
ValueTask < AgentType > RegisterAgentFactoryAsync (
AgentType type ,
Func < AgentId , IAgentRuntime , ValueTask < IHostableAgent >> factoryFunc );
ValueTask AddSubscriptionAsync ( ISubscriptionDefinition subscription );
ValueTask RemoveSubscriptionAsync ( string subscriptionId );
}
Methods Send a message to a specific agent and await response Sender agent (null for external)
Unique message identifier
Publish a message to all topic subscribers RegisterAgentFactoryAsync
Register an agent factory for dynamic creation factoryFunc
Func<AgentId, IAgentRuntime, ValueTask<IHostableAgent>>
required
Factory function
Interface for handling messages of a specific type. using Microsoft . AutoGen . Contracts ;
public class WorkerAgent : BaseAgent , IHandle < TaskMessage >
{
public async ValueTask < object ?> HandleAsync (
TaskMessage message ,
MessageContext context )
{
// Process task
return new ResultMessage { Result = "Completed" };
}
}
Identity & Types
Unique identifier for an agent instance. using Microsoft . AutoGen . Contracts ;
// Create agent ID
var agentId = new AgentId ( type : "worker" , key : "instance_1" );
// From string
var parsed = AgentId . FromStr ( "worker/instance_1" );
// Convert to string
string str = agentId . ToString (); // "worker/instance_1"
Agent type name (alphanumeric and underscores)
Instance key (ASCII 32-126 characters)
Methods Parse from “type/key” format
Convert to “type/key” format
Agent type definition. using Microsoft . AutoGen . Contracts ;
var workerType = new AgentType ( "worker" );
Topic identifier for pub/sub messaging. using Microsoft . AutoGen . Contracts ;
var topic = new TopicId ( type : "events" , source : "system" );
Proxy for remote agent communication. var proxy = await runtime . TryGetAgentProxyAsync ( agentId );
var result = await proxy . SendAsync ( "Hello" );
Messaging
Context information for message handling. public class MessageContext
{
public AgentId ? Sender { get ; }
public TopicId ? TopicId { get ; }
public string MessageId { get ; }
}
Identifier of the message sender
Topic the message was published to
Unique message identifier
Subscriptions
Subscribe to messages by exact type match. using Microsoft . AutoGen . Core ;
var subscription = new TypeSubscription (
topicType : "tasks" ,
agentType : "worker"
);
await runtime . AddSubscriptionAsync ( subscription );
Topic type to subscribe to
Agent type that will receive messages
Subscribe to messages matching a type prefix. var subscription = new TypePrefixSubscription (
topicTypePrefix : "task." ,
agentType : "worker"
);
TypeSubscriptionAttribute
Attribute for declarative subscriptions. [ TypeSubscription ( "tasks" )]
public class WorkerAgent : BaseAgent , IHandle < TaskMessage >
{
// ...
}
State Management
Interface for agent state persistence. public interface ISaveState
{
ValueTask < JsonElement > SaveStateAsync ();
ValueTask LoadStateAsync ( JsonElement state );
}
Save the agent’s state to JSON
Application Host
Application host for agent systems. using Microsoft . AutoGen . Core ;
var app = await AgentsApp . CreateAsync ();
// Register agents
await app . RegisterAgentAsync < WorkerAgent >( "worker" );
// Start the application
await app . StartAsync ();
Create a new agents application
Exceptions
Thrown when an agent cannot handle a message. throw new CantHandleException ( "Unsupported message type" );
Thrown when a message cannot be delivered. throw new UndeliverableException ( "Agent not found" );
Utilities
Utilities for agent reflection and discovery. using Microsoft . AutoGen . Core ;
var handlers = ReflectionHelper . GetMessageHandlers ( agentType );
Invokes message handlers dynamically. var invoker = new HandlerInvoker ( methodInfo , target );
var result = await invoker . InvokeAsync ( message , context );
Telemetry
OpenTelemetry integration. using System . Diagnostics ;
public static readonly ActivitySource s_source =
new ( "Microsoft.AutoGen.Core.Agent" );
using var activity = s_source . StartActivity ( "HandleMessage" );
See Also