Resumable SSE + Graceful Drain
The problem
Two related production scenarios:- Flaky network. A mobile client streaming an agent response loses signal mid-stream. When it reconnects, you want to resume from where it dropped, not restart from scratch.
- Graceful K8s rollout. A pod receives SIGTERM. You don’t want to drop in-flight agent runs on the floor; you want each one to finish its current step, persist a checkpoint, and exit cleanly so the next pod can pick up.
Resumable SSE
Concept
Every event emitted during an agent run is recorded in an in-memory ring buffer keyed byrunId. Each event gets a monotonically increasing numeric id. When a client reconnects with the standard Last-Event-ID HTTP header, the server replays any events the client missed before resuming.
InMemoryEventLog
API
Default singleton
defaultEventLog is exported as a process-wide shared instance so multiple endpoints can record / replay against the same buffer without coordinating:
formatSSEEvent(ev)
Renders an SSEEvent for the wire:
ev.event is set, an event: <name> line is also emitted.
Full reference handler
Storage requirements
InMemoryEventLog is in-process only. If you have multiple instances behind a load balancer, the client needs to reconnect to the same instance OR you need a shared backend.
Implementing a Redis-backed SSEEventLog is straightforward — just match the interface (record, since, all, finalize, drop). On the roadmap as RedisEventLog.
Graceful Drain
Concept
When SIGTERM hits, you want to:- Stop accepting new requests.
- Let in-flight requests finish their current LLM call / tool roundtrip.
- Persist a resumable checkpoint.
- Exit.
DrainController is a small primitive for cooperative shutdown.
DrainController
API
Agent.requestDrain() that automatically saves a checkpoint) is on the roadmap. For now, wire it yourself.
Full drain flow
Workflow.replay(checkpointId).
See also
- Workflow Time Travel — checkpoint + replay for stateful workflows
- Vercel UI Stream — for non-resumable browser clients (Vercel AI SDK protocol)
@agentium/transport— full transport package overview