Event-Driven Agents

Build reactive systems that respond to real-time events and triggers

The Event Loop

The event loop is the heartbeat of event-driven systems. It continuously checks for new events in the queue, dispatches them to handlers, and waits for completion—all without blocking.

This pattern enables agents to handle asynchronous operations efficiently, processing events as they arrive rather than waiting idly between tasks.

Interactive: Event Loop Visualizer

Watch how an event loop processes tasks one at a time, demonstrating the continuous check-and-execute cycle.

Loop Iteration: 0
1
Check new emails
2s duration
PENDING
2
Process payment
3s duration
PENDING
3
Generate report
4s duration
PENDING
4
Send notifications
2s duration
PENDING

Loop Lifecycle

1. Check Queue
Look for pending events in the queue
2. Dispatch Event
Send event to appropriate handler function
3. Execute Handler
Run the handler logic (async operations allowed)
4. Repeat
Loop back to step 1 continuously

Key Characteristics

🔄
Non-Blocking
Never waits synchronously for completion
📝
Single-Threaded
One event processed at a time (usually)
Efficient
No resource waste while waiting

💡 Key Insight

The event loop enables concurrency without parallelism. While only one event is processed at a time, the loop doesn't block waiting for I/O operations (like API calls or database queries). During those waits, it can handle other events—giving the illusion of doing multiple things simultaneously.