with or async with on PraisonAIAgents to close agents, memory stores, and connections when a workflow finishes.
Quick Start
1
Simple Usage
2
With Configuration
Async usage with shared memory inside a FastAPI endpoint:
How It Works
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

