ManagedAgent is deprecated as of PR #1550. New code should use HostedAgent for Anthropic-hosted runs (this page) or LocalAgent for local loops. Existing imports continue to work but emit a DeprecationWarning for non-Anthropic providers.When context compaction is enabled, managed persistence surfaces the compaction checkpoint automatically — resume replays the compacted working history (summary + tail). See Compacted Session Resume.
Quick Start
1
Basic Usage
Run
gpt-4o-mini conversations with SQLite persistence in 5 lines:2
Session Resume
Continue conversations after restarts:
How It Works
Database Backends
- SQLite
- PostgreSQL
- MySQL
- Redis
- MongoDB
- ClickHouse
- JSON Files
Zero external dependencies, file-based storage:Prerequisites: None (built into Python)
Session Metadata Persistence
HostedAgent automatically persists both chat messages and session metadata to your database, ensuring complete session recovery after process restarts.What Survives Process Restarts
When using database persistence, these session components automatically survive crashes, restarts, and deployments:Complete Session Resume Example
Metadata Fields Preserved
The following metadata fields are automatically persisted and restored:
These fields enable cost tracking, usage analytics, and compute resource management across process boundaries.
Configuration Options
HostedAgent API Reference
Complete HostedAgent configuration options
PraisonDB Reference
Database adapter configuration options
Clearing & Deleting Sessions
TheDbSessionAdapter now properly purges persisted messages from the database, not just the in-memory cache, ensuring that cleared sessions stay clear even after restarts.
Clear vs Delete Sessions
clear_session() and delete_session() now purge persisted messages from the underlying conversation store.
Common Patterns
Session Resume Pattern
Session Resume Pattern
The most common pattern for persistent managed agents:
Multi-Backend Pattern
Multi-Backend Pattern
Use different backends for different data types:
Session ID Management
Session ID Management
Best practices for session identification:
Best Practices
Session Management
Session Management
- Use meaningful session IDs (user-based, not random)
- Implement session rotation for long conversations
- Store session metadata for debugging
- Concurrent metadata writes are safe with
DefaultSessionStore(locked read-modify-write); for custom stores, implementupdate_session_metadataor your own equivalent to avoid stale-copy overwrites
Database Selection
Database Selection
- SQLite: Development, single-user apps, file-based persistence
- PostgreSQL: Production apps, complex queries, ACID compliance
- MySQL: Existing MySQL infrastructure, compatibility requirements
- Redis: High-speed state, session caching, temporary data
- MongoDB: Document-based state, flexible schemas
- ClickHouse: Analytics, large-scale logging, data warehousing
- JSON Files: Prototyping, zero dependencies, simple use cases
Performance Optimization
Performance Optimization
- Use connection pooling for database connections
- Implement message compaction for long sessions
- Cache frequently accessed session data
- Use async database operations when possible
- Monitor database performance metrics
Error Handling
Error Handling
- Implement retry logic for transient database failures
- Handle session corruption gracefully
- Log database errors for debugging
- Provide fallback behavior when persistence fails
- Test database connection before agent creation
Related
Hosted Agent
Run entire agent loops on Anthropic’s managed runtime
Local Agent
Run agent loops locally with any LLM
Managed CLI
Terminal commands for managing Anthropic-hosted resources
Session Management
Advanced session handling techniques

