with or async with on PraisonAIAgents to close agents, memory stores, and connections when a workflow finishes.
Quick Start
How It Works
| Entry point | Method | Description |
|---|---|---|
with team: | __enter__ / __exit__ | Sync — calls close() on exit |
async with team: | __aenter__ / __aexit__ | Async — prefers aclose() when available |
| Manual | team.close() | Explicit cleanup for long-running workers |
Common Patterns
Shared memory cleanup
Long-running worker shutdown
Per-request isolation in servers
Best Practices
Prefer context managers over manual close()
Prefer context managers over manual close()
with and async with guarantee cleanup even when exceptions occur.Cleanup is best-effort
Cleanup is best-effort
close() logs warnings on failure but does not raise — cleanup failures will not mask the original error.Do not reuse a team after exiting its block
Do not reuse a team after exiting its block
Create a new
PraisonAIAgents instance for each batch or request.Create one team per request in servers
Create one team per request in servers
Avoid sharing a global workflow across HTTP requests — isolate resources per call.
Related
Advanced Memory System
Shared memory stores that benefit from automatic cleanup
MongoDB Memory
MongoDB-backed memory with connection lifecycle support

