AgentRuntime Protocol
TheAgentRuntime 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.
Initialization
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:Cleanup
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:Two-argument factories
factory(runtime: AgentRuntime, agent_id: AgentId) are deprecated. Use AgentInstantiationContext instead.Instance Registration
Register a pre-created agent instance:Message Operations
Sending Direct Messages
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
State Persistence
Save State
Load State
Per-Agent State Operations
Advanced Operations
Manual Message Processing
Accessing Agent Instances
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