Skip to article frontmatterSkip to article content

Runtime

what is Runtime environment

In essence, think of a framework is a set of tools that supports a programming environment, often designed to solve business use cases or complete specific tasks.

Within this environment, we create a runtime environment — a setup that allows a program to run and supports one or more AI agent actor models.

These agents manage their own memory, state, and properties, and they interact with themselves, other agents, and external systems using messaging protocols.

Now, since we have already created Agents, first step is to attach these AI Agents to this run time environment. We call this process of attaching these AI Agents to a run time environment as registering Agents.

In object-oriented programming, when you create an object from a class—known as instantiating a class, you produce an instance of that class.

For example, if you have a class called Car, you might instantiate it as myCar. Once instantiated, you can call methods like myCar.startEngine() or myCar.accelerate() to update the object’s state.

Similarly, registering an AI agent (which follows the actor model) to a runtime environment is akin to this instantiation process.

In the actor model, an AI agent is an entity capable of receiving and processing messages. When you register the agent to the runtime environment, you effectively set it up, making it available to operate—much like creating an object in object-oriented programming.

However, there’s a crucial difference:

This means that, after registration, the AI agent remains idle until it receives a message. Only then does it spring into action, processing the message and potentially updating its state or performing tasks. This behavior aligns with the actor model’s design, which is often used in concurrent or distributed systems, where independent actors communicate asynchronously through message passing.

new Runtime environment

import inspect

from autogen_core import SingleThreadedAgentRuntime
# create a standalong single thread runtime
runtime = SingleThreadedAgentRuntime()

method = PTOAgent.register

# Print the signature of register method
sig = inspect.signature(method)
print(f"Signature: {sig}")

# Print the doc of register method
doc = method.__doc__
print(f"Docstring:\n{doc}")

Let’s attach Agents to this new runtime environment.

from autogen_core import SingleThreadedAgentRuntime
runtime = SingleThreadedAgentRuntime()
## will discuss register() in detail later
await PTOAgent.register(runtime, "AgType_ERP_PTO_Agent", lambda: PTOAgent())
await TaskAgent.register(runtime, "AgType_ERP_Task_Agent", lambda: TaskAgent())

Running the Single-Threaded Agent Runtime

runtime.start()
# ... Send messages, publish messages, etc.
await runtime.stop()  # This will return immediately but will not cancel
# any in-progress message handling.

runtime.start()
# ... Send messages, publish messages, etc.
await runtime.stop_when_idle()  # This will block until the runtime is idle.

await runtime.close()