Core Actor API Reference
Actor
Base class for all actors (Level 5). Use this for infrastructure components that need direct message-passing control. For AI agents, prefer AgentActor.
Methods to override
on_receive(message)
Handle an incoming message. Return value is sent back as reply for ask() calls.
on_started()
Called after creation, before receiving messages.
on_stopped()
Called on graceful shutdown. Release resources here.
on_restart(error)
Called on the new instance before resuming after a supervision-triggered restart.
supervisor_strategy()
Override to customize how this actor supervises its children.
Default: OneForOneStrategy(max_restarts=3, within_seconds=60).
stop_policy()
Override to customize automatic lifecycle management.
Default: StopMode.NEVER (actor never auto-stops).
from everything_is_an_actor import StopMode, AfterMessage, AfterIdle, StopPolicy
# Auto-stop after one message
def stop_policy(self) -> StopPolicy:
return StopMode.ONE_TIME
# Auto-stop after receiving specific message
def stop_policy(self) -> StopPolicy:
return AfterMessage(message="shutdown")
# Auto-stop after idle timeout
def stop_policy(self) -> StopPolicy:
return AfterIdle(seconds=60.0)
Properties (via context)
| Property | Type | Description |
|---|---|---|
context.self_ref |
ActorRef |
Reference to this actor |
context.parent |
ActorRef \| None |
Parent actor reference |
context.children |
dict[str, ActorRef] |
Child actor references |
context.system |
ActorSystem |
The containing system |
Spawning children
ActorRef
Lightweight handle to a running actor.
Methods
tell(message)
Send a message without waiting for a reply (fire-and-forget).
ask(message, timeout)
Send a message and wait for the reply.
Raises asyncio.TimeoutError if the actor doesn't respond within timeout seconds.
is_alive
Returns True if the actor is still running.
free_ask(message)
Lift an ask operation into the Free monad for composable workflows.
free_tell(message)
Lift a tell operation into the Free monad for composable workflows.
free_stop()
Lift a stop operation into the Free monad for composable workflows.
ActorSystem
Top-level container for root-level actors.
Methods
spawn(actor_cls, name, ...)
async def spawn(
actor_cls: type[Actor],
name: str,
*,
mailbox_size: int = 256,
mailbox: Mailbox | None = None,
middlewares: list[Middleware] | None = None,
) -> ActorRef
Spawn a root-level actor.
get_actor(path)
Get actor ref by path. Returns None if not found.
Path format: /system-name/actor-name/.../actor-name
Example: /app/workers/collector
ask(path, message, timeout)
Shorthand for get_actor(path) + ref.ask(message).
Raises ValueError if actor not found.
shutdown(timeout)
Gracefully stop all actors.
dead_letters
Messages that could not be delivered.
Supervision strategies
OneForOneStrategy
Restart only the failing child.
from everything_is_an_actor import OneForOneStrategy
OneForOneStrategy(max_restarts=3, within_seconds=60)
AllForOneStrategy
Restart all siblings when any one fails.
from everything_is_an_actor import AllForOneStrategy
AllForOneStrategy(max_restarts=3, within_seconds=60)
Directive
from everything_is_an_actor import Directive
Directive.restart # default
Directive.resume
Directive.stop
Directive.escalate
Stop Policy ADT
StopMode
class StopMode(Enum):
NEVER = auto() # Never auto-stop (default)
ONE_TIME = auto() # Stop after processing one message
AfterMessage
AfterIdle
StopPolicy
Union type: StopMode | AfterMessage | AfterIdle