When you let AI agents spawn sub agents, you immediately inherit a resource management problem. Each sub agent needs an email account, credentials, and state. If you’re not careful, you end up with hundreds of orphaned accounts clogging your system. The OpenClaw plugin in AgenticMail solves this with automatic provisioning and aggressive garbage collection.
Auto Provisioning via before_agent_start
The core idea is simple: sub agents shouldn’t have to ask for resources. When a parent agent decides to spin up a sub agent for a specific task (say, researching a topic or drafting a response), the OpenClaw plugin intercepts the launch through a before_agent_start hook.
This hook fires before the sub agent’s main loop begins. It checks whether the sub agent already has a provisioned email account. If not, it creates one on the spot: generates a temporary address, sets up the mailbox, creates scoped API credentials, and wires everything into the sub agent’s context. By the time the sub agent’s code starts executing, it has a fully functional email identity ready to go.
The temporary accounts follow a naming convention that makes them easy to identify and track. They’re clearly marked as ephemeral, which matters later when garbage collection runs.
The 15 Minute GC Interval
Here’s where things get interesting. The garbage collector runs every 15 minutes and scans for stale temporary accounts. The staleness threshold is 2 hours of inactivity. If a temporary account hasn’t sent or received any email, and the owning sub agent hasn’t made any API calls in that window, the account is considered stale and eligible for eviction.
Eviction isn’t just deleting the account. The GC follows a sequence:
- Check if the sub agent process is still alive
- If alive, send a graceful shutdown signal and wait briefly
- Archive any remaining emails in the temporary mailbox
- Revoke the scoped API credentials
- Remove the account from the active roster
The 2 hour threshold was chosen through trial and error. Too short and you’d evict agents that were legitimately waiting for a slow external reply. Too long and you’d accumulate dozens of idle accounts burning memory. Two hours turned out to be the sweet spot for most workflows.
Coordination Threads
Sub agents don’t operate in isolation. They need to communicate results back to their parent agent and sometimes to each other. OpenClaw manages this through coordination threads, which are essentially shared email threads that all agents in a task group can read and write to.
When a sub agent is provisioned, it’s automatically subscribed to the coordination thread for its task group. Any findings, status updates, or completion signals go into this thread. The parent agent monitors it and can synthesize results from multiple sub agents working in parallel.
This is much cleaner than having sub agents send individual reports to the parent. The thread acts as a shared workspace where the full context of a multi agent task is visible to everyone involved. If a sub agent needs to reference what another sub agent found, it’s right there in the thread.
Email Push Notifications via SSE Watchers
Rather than having agents poll their mailboxes, OpenClaw sets up Server Sent Events (SSE) watchers for each provisioned account. When a new email arrives, the SSE stream pushes a notification to the sub agent immediately. This keeps latency low and avoids the overhead of constant polling.
The SSE watchers are tied to the account lifecycle. When GC evicts an account, it also tears down the associated SSE connection. This prevents resource leaks where a watcher keeps listening on a mailbox that no longer has an active agent behind it.
One detail that took some debugging: SSE connections need proper keepalive handling. If the connection drops silently (which happens more than you’d think), the agent stops getting notifications but doesn’t know it. The watcher now sends periodic heartbeat events and reconnects automatically if the stream goes quiet for too long.
Why This Matters
The whole point of multi agent architectures is that you can break complex tasks into smaller pieces and let specialized agents handle each one. But if the overhead of managing those agents is high, you’ll never actually benefit from the parallelism. OpenClaw makes sub agent provisioning essentially free from the parent agent’s perspective. Spin one up, let it work, and trust that the system will clean up after it.
Source Code
The OpenClaw plugin is where all the lifecycle orchestration lives: the before_agent_start hook for auto provisioning, the 15 minute garbage collection interval, coordination thread management, and SSE watcher setup.
View the full source on GitHub
The combination of automatic provisioning, aggressive GC, and coordination threads means you can treat sub agents as disposable workers without worrying about resource leaks or orphaned state. That’s the foundation that makes AgenticMail’s multi agent workflows practical rather than theoretical.