Skip to main content
The Agent Runtime is the execution environment that manages agent lifecycle, message routing, and system-level operations. It provides the infrastructure for agents to send messages, publish events, and maintain state.

AgentRuntime Protocol

The AgentRuntime protocol defines the interface all runtime implementations must provide:

SingleThreadedAgentRuntime

SingleThreadedAgentRuntime is a development-focused runtime that processes all messages in a single asyncio event loop.
SingleThreadedAgentRuntime is designed for development and standalone applications. For production deployments with high throughput or distributed requirements, use the distributed runtime.

Initialization

Parameters:
List[InterventionHandler]
default:"None"
List of handlers that can intercept and modify messages before delivery. Useful for logging, validation, or message transformation.
TracerProvider
default:"None"
OpenTelemetry tracer provider for distributed tracing. Set AUTOGEN_DISABLE_RUNTIME_TRACING=true to disable.
bool
default:"True"
If True, exceptions in event handlers won’t stop the runtime. Exceptions are raised on next process_next() or stop() call. RPC handler exceptions are always propagated.

Lifecycle Management

Starting the Runtime

Stopping the Runtime

Three ways to stop the runtime:
Use stop_when_idle() in most cases. It ensures all queued messages are processed before shutdown.

Cleanup

This calls stop() if the runtime is running, then calls close() on all instantiated agents.

Agent Registration

Factory Registration

Register a factory function that creates agents on-demand:
Factory function signature:
Two-argument factories factory(runtime: AgentRuntime, agent_id: AgentId) are deprecated. Use AgentInstantiationContext instead.

Instance Registration

Register a pre-created agent instance:
All instances of the same agent type must be of the same class. Mixing factories and instances for the same type is not allowed.

Message Operations

Sending Direct Messages

Parameters:
Any
required
The message object to send. Must be serializable.
AgentId
required
The agent that should receive and process the message.
AgentId | None
default:"None"
The sending agent’s ID. Should only be None for external (non-agent) senders.
CancellationToken | None
default:"None"
Token to cancel the operation if needed.
str | None
default:"None"
Unique message identifier. Auto-generated UUID if not provided.

Publishing to Topics

publish_message() is fire-and-forget. It doesn’t wait for responses or confirm delivery to subscribers.

Subscription Management

See Message Passing for details on subscription types.

State Persistence

Save State

Load State

Per-Agent State Operations

Advanced Operations

Manual Message Processing

process_next() raises unhandled exceptions. Cannot be called again after an exception is raised. Use start() for automatic message processing.

Accessing Agent Instances

Accessing agent instances directly breaks the Actor model. Use only for debugging or special cases.

Agent Lookup

Message Serialization

Complete Example

Runtime Monitoring

Next Steps

Message Passing

Learn about messages, contexts, topics, and subscriptions

Event-Driven Architecture

Understand event handlers and message routing

Distributed Runtime

Scale across processes and languages

Core Overview

Return to Core API overview